libubox: replace strtok with _r version.
[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
33 static const char *ulog_default_ident(void)
34 {
35 FILE *self;
36 static char line[64];
37 char *p = NULL;
38 char *sbuf;
39
40 if ((self = fopen("/proc/self/status", "r")) != NULL) {
41 while (fgets(line, sizeof(line), self)) {
42 if (!strncmp(line, "Name:", 5)) {
43 strtok_r(line, "\t\n", &sbuf);
44 p = strtok_r(NULL, "\t\n", &sbuf);
45 break;
46 }
47 }
48 fclose(self);
49 }
50
51 return p;
52 }
53
54 static void ulog_defaults(void)
55 {
56 char *env;
57
58 if (_ulog_initialized)
59 return;
60
61 env = getenv("PREINIT");
62
63 if (_ulog_channels < 0) {
64 if (env && !strcmp(env, "1"))
65 _ulog_channels = ULOG_KMSG;
66 else if (isatty(1))
67 _ulog_channels = ULOG_STDIO;
68 else
69 _ulog_channels = ULOG_SYSLOG;
70 }
71
72 if (_ulog_facility < 0) {
73 if (env && !strcmp(env, "1"))
74 _ulog_facility = LOG_DAEMON;
75 else if (isatty(1))
76 _ulog_facility = LOG_USER;
77 else
78 _ulog_facility = LOG_DAEMON;
79 }
80
81 if (_ulog_ident == NULL && _ulog_channels != ULOG_STDIO)
82 _ulog_ident = ulog_default_ident();
83
84 if (_ulog_channels & ULOG_SYSLOG)
85 openlog(_ulog_ident, 0, _ulog_facility);
86
87 _ulog_initialized = 1;
88 }
89
90 static void ulog_kmsg(int priority, const char *fmt, va_list ap)
91 {
92 FILE *kmsg;
93
94 if ((kmsg = fopen("/dev/kmsg", "r+")) != NULL) {
95 fprintf(kmsg, "<%u>", priority);
96
97 if (_ulog_ident)
98 fprintf(kmsg, "%s: ", _ulog_ident);
99
100 vfprintf(kmsg, fmt, ap);
101 fclose(kmsg);
102 }
103 }
104
105 static void ulog_stdio(int priority, const char *fmt, va_list ap)
106 {
107 FILE *out = stderr;
108
109 if (_ulog_ident)
110 fprintf(out, "%s: ", _ulog_ident);
111
112 vfprintf(out, fmt, ap);
113 }
114
115 static void ulog_syslog(int priority, const char *fmt, va_list ap)
116 {
117 vsyslog(priority, fmt, ap);
118 }
119
120 void ulog_open(int channels, int facility, const char *ident)
121 {
122 ulog_close();
123
124 _ulog_channels = channels;
125 _ulog_facility = facility;
126 _ulog_ident = ident;
127 }
128
129 void ulog_close(void)
130 {
131 if (!_ulog_initialized)
132 return;
133
134 if (_ulog_channels & ULOG_SYSLOG)
135 closelog();
136
137 _ulog_initialized = 0;
138 }
139
140 void ulog_threshold(int threshold)
141 {
142 _ulog_threshold = threshold;
143 }
144
145 void ulog(int priority, const char *fmt, ...)
146 {
147 va_list ap;
148
149 if (priority > _ulog_threshold)
150 return;
151
152 ulog_defaults();
153
154 if (_ulog_channels & ULOG_KMSG)
155 {
156 va_start(ap, fmt);
157 ulog_kmsg(priority, fmt, ap);
158 va_end(ap);
159 }
160
161 if (_ulog_channels & ULOG_STDIO)
162 {
163 va_start(ap, fmt);
164 ulog_stdio(priority, fmt, ap);
165 va_end(ap);
166 }
167
168 if (_ulog_channels & ULOG_SYSLOG)
169 {
170 va_start(ap, fmt);
171 ulog_syslog(priority, fmt, ap);
172 va_end(ap);
173 }
174 }