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
;
32 static struct udebug_buf
*udb
= NULL
;
34 static const char *ulog_default_ident(void)
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
);
55 static void ulog_defaults(void)
59 if (_ulog_initialized
)
62 env
= getenv("PREINIT");
64 if (_ulog_channels
< 0) {
65 if (env
&& !strcmp(env
, "1"))
66 _ulog_channels
= ULOG_KMSG
;
68 _ulog_channels
= ULOG_STDIO
;
70 _ulog_channels
= ULOG_SYSLOG
;
73 if (_ulog_facility
< 0) {
74 if (env
&& !strcmp(env
, "1"))
75 _ulog_facility
= LOG_DAEMON
;
77 _ulog_facility
= LOG_USER
;
79 _ulog_facility
= LOG_DAEMON
;
82 if (_ulog_ident
== NULL
&& _ulog_channels
!= ULOG_STDIO
)
83 _ulog_ident
= ulog_default_ident();
85 if (_ulog_channels
& ULOG_SYSLOG
)
86 openlog(_ulog_ident
, 0, _ulog_facility
);
88 _ulog_initialized
= 1;
91 __attribute__((format(printf
, 2, 0)))
92 static void ulog_kmsg(int priority
, const char *fmt
, va_list ap
)
96 if ((kmsg
= fopen("/dev/kmsg", "r+")) != NULL
) {
97 fprintf(kmsg
, "<%u>", priority
);
100 fprintf(kmsg
, "%s: ", _ulog_ident
);
102 vfprintf(kmsg
, fmt
, ap
);
107 __attribute__((format(printf
, 2, 0)))
108 static void ulog_stdio(int priority
, const char *fmt
, va_list ap
)
113 fprintf(out
, "%s: ", _ulog_ident
);
115 vfprintf(out
, fmt
, ap
);
118 __attribute__((format(printf
, 2, 0)))
119 static void ulog_syslog(int priority
, const char *fmt
, va_list ap
)
121 vsyslog(priority
, fmt
, ap
);
124 void ulog_udebug(struct udebug_buf
*_udb
)
129 void ulog_open(int channels
, int facility
, const char *ident
)
133 _ulog_channels
= channels
;
134 _ulog_facility
= facility
;
138 void ulog_close(void)
140 if (!_ulog_initialized
)
143 if (_ulog_channels
& ULOG_SYSLOG
)
146 _ulog_initialized
= 0;
149 void ulog_threshold(int threshold
)
151 _ulog_threshold
= threshold
;
154 void ulog(int priority
, const char *fmt
, ...)
160 udebug_entry_init(udb
);
161 udebug_entry_vprintf(udb
, fmt
, ap
);
162 udebug_entry_add(udb
);
166 if (priority
> _ulog_threshold
)
171 if (_ulog_channels
& ULOG_KMSG
)
174 ulog_kmsg(priority
, fmt
, ap
);
178 if (_ulog_channels
& ULOG_STDIO
)
181 ulog_stdio(priority
, fmt
, ap
);
185 if (_ulog_channels
& ULOG_SYSLOG
)
188 ulog_syslog(priority
, fmt
, ap
);