• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/libubox/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 __attribute__((format(printf, 2, 0)))
 91 static void ulog_kmsg(int priority, const char *fmt, va_list ap)
 92 {
 93         FILE *kmsg;
 94 
 95         if ((kmsg = fopen("/dev/kmsg", "r+")) != NULL) {
 96                 fprintf(kmsg, "<%u>", priority);
 97 
 98                 if (_ulog_ident)
 99                         fprintf(kmsg, "%s: ", _ulog_ident);
100 
101                 vfprintf(kmsg, fmt, ap);
102                 fclose(kmsg);
103         }
104 }
105 
106 __attribute__((format(printf, 2, 0)))
107 static void ulog_stdio(int priority, const char *fmt, va_list ap)
108 {
109         FILE *out = stderr;
110 
111         if (_ulog_ident)
112                 fprintf(out, "%s: ", _ulog_ident);
113 
114         vfprintf(out, fmt, ap);
115 }
116 
117 __attribute__((format(printf, 2, 0)))
118 static void ulog_syslog(int priority, const char *fmt, va_list ap)
119 {
120         vsyslog(priority, fmt, ap);
121 }
122 
123 void ulog_open(int channels, int facility, const char *ident)
124 {
125         ulog_close();
126 
127         _ulog_channels = channels;
128         _ulog_facility = facility;
129         _ulog_ident = ident;
130 }
131 
132 void ulog_close(void)
133 {
134         if (!_ulog_initialized)
135                 return;
136 
137         if (_ulog_channels & ULOG_SYSLOG)
138                 closelog();
139 
140         _ulog_initialized = 0;
141 }
142 
143 void ulog_threshold(int threshold)
144 {
145         _ulog_threshold = threshold;
146 }
147 
148 void ulog(int priority, const char *fmt, ...)
149 {
150         va_list ap;
151 
152         if (priority > _ulog_threshold)
153                 return;
154 
155         ulog_defaults();
156 
157         if (_ulog_channels & ULOG_KMSG)
158         {
159                 va_start(ap, fmt);
160                 ulog_kmsg(priority, fmt, ap);
161                 va_end(ap);
162         }
163 
164         if (_ulog_channels & ULOG_STDIO)
165         {
166                 va_start(ap, fmt);
167                 ulog_stdio(priority, fmt, ap);
168                 va_end(ap);
169         }
170 
171         if (_ulog_channels & ULOG_SYSLOG)
172         {
173                 va_start(ap, fmt);
174                 ulog_syslog(priority, fmt, ap);
175                 va_end(ap);
176         }
177 }
178 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt