add a base64 implementation (based on FreeBSD code)
[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", "w")) != 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 (priority == LOG_INFO || priority == LOG_NOTICE)
109                 out = stdout;
110
111         if (_ulog_ident)
112                 fprintf(out, "%s: ", _ulog_ident);
113
114         vfprintf(out, fmt, ap);
115 }
116
117 static void ulog_syslog(int priority, const char *fmt, va_list ap)
118 {
119         vsyslog(priority, fmt, ap);
120 }
121
122 void ulog_open(int channels, int facility, const char *ident)
123 {
124         ulog_close();
125
126         _ulog_channels = channels;
127         _ulog_facility = facility;
128         _ulog_ident = ident;
129 }
130
131 void ulog_close(void)
132 {
133         if (!_ulog_initialized)
134                 return;
135
136         if (_ulog_channels & ULOG_SYSLOG)
137                 closelog();
138
139         _ulog_initialized = 0;
140 }
141
142 void ulog_threshold(int threshold)
143 {
144         _ulog_threshold = threshold;
145 }
146
147 void ulog(int priority, const char *fmt, ...)
148 {
149         va_list ap;
150
151         if (priority > _ulog_threshold)
152                 return;
153
154         ulog_defaults();
155
156         if (_ulog_channels & ULOG_KMSG)
157         {
158                 va_start(ap, fmt);
159                 ulog_kmsg(priority, fmt, ap);
160                 va_end(ap);
161         }
162
163         if (_ulog_channels & ULOG_STDIO)
164         {
165                 va_start(ap, fmt);
166                 ulog_stdio(priority, fmt, ap);
167                 va_end(ap);
168         }
169
170         if (_ulog_channels & ULOG_SYSLOG)
171         {
172                 va_start(ap, fmt);
173                 ulog_syslog(priority, fmt, ap);
174                 va_end(ap);
175         }
176 }