2 * ulog - simple logging functions
4 * Copyright (C) 2015 Jo-Philipp Wich <jow@openwrt.org>
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.
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.
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;
33 static const char *ulog_default_ident(void)
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);
54 static void ulog_defaults(void)
58 if (_ulog_initialized)
61 env = getenv("PREINIT");
63 if (_ulog_channels < 0) {
64 if (env && !strcmp(env, "1"))
65 _ulog_channels = ULOG_KMSG;
67 _ulog_channels = ULOG_STDIO;
69 _ulog_channels = ULOG_SYSLOG;
72 if (_ulog_facility < 0) {
73 if (env && !strcmp(env, "1"))
74 _ulog_facility = LOG_DAEMON;
76 _ulog_facility = LOG_USER;
78 _ulog_facility = LOG_DAEMON;
81 if (_ulog_ident == NULL && _ulog_channels != ULOG_STDIO)
82 _ulog_ident = ulog_default_ident();
84 if (_ulog_channels & ULOG_SYSLOG)
85 openlog(_ulog_ident, 0, _ulog_facility);
87 _ulog_initialized = 1;
90 __attribute__((format(printf, 2, 0)))
91 static void ulog_kmsg(int priority, const char *fmt, va_list ap)
95 if ((kmsg = fopen("/dev/kmsg", "r+")) != NULL) {
96 fprintf(kmsg, "<%u>", priority);
99 fprintf(kmsg, "%s: ", _ulog_ident);
101 vfprintf(kmsg, fmt, ap);
106 __attribute__((format(printf, 2, 0)))
107 static void ulog_stdio(int priority, const char *fmt, va_list ap)
112 fprintf(out, "%s: ", _ulog_ident);
114 vfprintf(out, fmt, ap);
117 __attribute__((format(printf, 2, 0)))
118 static void ulog_syslog(int priority, const char *fmt, va_list ap)
120 vsyslog(priority, fmt, ap);
123 void ulog_open(int channels, int facility, const char *ident)
127 _ulog_channels = channels;
128 _ulog_facility = facility;
132 void ulog_close(void)
134 if (!_ulog_initialized)
137 if (_ulog_channels & ULOG_SYSLOG)
140 _ulog_initialized = 0;
143 void ulog_threshold(int threshold)
145 _ulog_threshold = threshold;
148 void ulog(int priority, const char *fmt, ...)
152 if (priority > _ulog_threshold)
157 if (_ulog_channels & ULOG_KMSG)
160 ulog_kmsg(priority, fmt, ap);
164 if (_ulog_channels & ULOG_STDIO)
167 ulog_stdio(priority, fmt, ap);
171 if (_ulog_channels & ULOG_SYSLOG)
174 ulog_syslog(priority, fmt, ap);