ustream: prevent recursive calls to the read callback
[project/libubox.git] / ulog.c
1 /*
2 * ulog - simple logging functions
3 *
4 * Copyright (C) 2015 Jo-Philipp Wich <jow@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "ulog.h"
20
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 static int _ulog_channels = -1;
28 static int _ulog_facility = -1;
29 static int _ulog_threshold = LOG_DEBUG;
30 static int _ulog_initialized = 0;
31 static const char *_ulog_ident = NULL;
32 static struct udebug_buf *udb = NULL;
33
34 static const char *ulog_default_ident(void)
35 {
36 FILE *self;
37 static char line[64];
38 char *p = NULL;
39 char *sbuf;
40
41 if ((self = fopen("/proc/self/status", "r")) != NULL) {
42 while (fgets(line, sizeof(line), self)) {
43 if (!strncmp(line, "Name:", 5)) {
44 strtok_r(line, "\t\n", &sbuf);
45 p = strtok_r(NULL, "\t\n", &sbuf);
46 break;
47 }
48 }
49 fclose(self);
50 }
51
52 return p;
53 }
54
55 static void ulog_defaults(void)
56 {
57 char *env;
58
59 if (_ulog_initialized)
60 return;
61
62 env = getenv("PREINIT");
63
64 if (_ulog_channels < 0) {
65 if (env && !strcmp(env, "1"))
66 _ulog_channels = ULOG_KMSG;
67 else if (isatty(1))
68 _ulog_channels = ULOG_STDIO;
69 else
70 _ulog_channels = ULOG_SYSLOG;
71 }
72
73 if (_ulog_facility < 0) {
74 if (env && !strcmp(env, "1"))
75 _ulog_facility = LOG_DAEMON;
76 else if (isatty(1))
77 _ulog_facility = LOG_USER;
78 else
79 _ulog_facility = LOG_DAEMON;
80 }
81
82 if (_ulog_ident == NULL && _ulog_channels != ULOG_STDIO)
83 _ulog_ident = ulog_default_ident();
84
85 if (_ulog_channels & ULOG_SYSLOG)
86 openlog(_ulog_ident, 0, _ulog_facility);
87
88 _ulog_initialized = 1;
89 }
90
91 __attribute__((format(printf, 2, 0)))
92 static void ulog_kmsg(int priority, const char *fmt, va_list ap)
93 {
94 FILE *kmsg;
95
96 if ((kmsg = fopen("/dev/kmsg", "r+")) != NULL) {
97 fprintf(kmsg, "<%u>", priority);
98
99 if (_ulog_ident)
100 fprintf(kmsg, "%s: ", _ulog_ident);
101
102 vfprintf(kmsg, fmt, ap);
103 fclose(kmsg);
104 }
105 }
106
107 __attribute__((format(printf, 2, 0)))
108 static void ulog_stdio(int priority, const char *fmt, va_list ap)
109 {
110 FILE *out = stderr;
111
112 if (_ulog_ident)
113 fprintf(out, "%s: ", _ulog_ident);
114
115 vfprintf(out, fmt, ap);
116 }
117
118 __attribute__((format(printf, 2, 0)))
119 static void ulog_syslog(int priority, const char *fmt, va_list ap)
120 {
121 vsyslog(priority, fmt, ap);
122 }
123
124 void ulog_udebug(struct udebug_buf *_udb)
125 {
126 udb = _udb;
127 }
128
129 void ulog_open(int channels, int facility, const char *ident)
130 {
131 ulog_close();
132
133 _ulog_channels = channels;
134 _ulog_facility = facility;
135 _ulog_ident = ident;
136 }
137
138 void ulog_close(void)
139 {
140 if (!_ulog_initialized)
141 return;
142
143 if (_ulog_channels & ULOG_SYSLOG)
144 closelog();
145
146 _ulog_initialized = 0;
147 }
148
149 void ulog_threshold(int threshold)
150 {
151 _ulog_threshold = threshold;
152 }
153
154 void ulog(int priority, const char *fmt, ...)
155 {
156 va_list ap;
157
158 if (udb) {
159 va_start(ap, fmt);
160 udebug_entry_init(udb);
161 udebug_entry_vprintf(udb, fmt, ap);
162 udebug_entry_add(udb);
163 va_end(ap);
164 }
165
166 if (priority > _ulog_threshold)
167 return;
168
169 ulog_defaults();
170
171 if (_ulog_channels & ULOG_KMSG)
172 {
173 va_start(ap, fmt);
174 ulog_kmsg(priority, fmt, ap);
175 va_end(ap);
176 }
177
178 if (_ulog_channels & ULOG_STDIO)
179 {
180 va_start(ap, fmt);
181 ulog_stdio(priority, fmt, ap);
182 va_end(ap);
183 }
184
185 if (_ulog_channels & ULOG_SYSLOG)
186 {
187 va_start(ap, fmt);
188 ulog_syslog(priority, fmt, ap);
189 va_end(ap);
190 }
191 }