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