libubox
C utility functions for OpenWrt.
ulog.c
Go to the documentation of this file.
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 static struct udebug_buf *udb = NULL;
33 
34 static const char *ulog_default_ident(void)
35 {
36  FILE *self;
37  static char line[64];
38  char *p = NULL;
39  char *sbuf;
40 
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);
46  break;
47  }
48  }
49  fclose(self);
50  }
51 
52  return p;
53 }
54 
55 static void ulog_defaults(void)
56 {
57  char *env;
58 
60  return;
61 
62  env = getenv("PREINIT");
63 
64  if (_ulog_channels < 0) {
65  if (env && !strcmp(env, "1"))
67  else if (isatty(1))
69  else
71  }
72 
73  if (_ulog_facility < 0) {
74  if (env && !strcmp(env, "1"))
75  _ulog_facility = LOG_DAEMON;
76  else if (isatty(1))
77  _ulog_facility = LOG_USER;
78  else
79  _ulog_facility = LOG_DAEMON;
80  }
81 
82  if (_ulog_ident == NULL && _ulog_channels != ULOG_STDIO)
84 
86  openlog(_ulog_ident, 0, _ulog_facility);
87 
89 }
90 
91 __attribute__((format(printf, 2, 0)))
92 static void ulog_kmsg(int priority, const char *fmt, va_list ap)
93 {
94  FILE *kmsg;
95 
96  if ((kmsg = fopen("/dev/kmsg", "r+")) != NULL) {
97  fprintf(kmsg, "<%u>", priority);
98 
99  if (_ulog_ident)
100  fprintf(kmsg, "%s: ", _ulog_ident);
101 
102  vfprintf(kmsg, fmt, ap);
103  fclose(kmsg);
104  }
105 }
106 
107 __attribute__((format(printf, 2, 0)))
108 static void ulog_stdio(int priority, const char *fmt, va_list ap)
109 {
110  FILE *out = stderr;
111 
112  if (_ulog_ident)
113  fprintf(out, "%s: ", _ulog_ident);
114 
115  vfprintf(out, fmt, ap);
116 }
117 
118 __attribute__((format(printf, 2, 0)))
119 static void ulog_syslog(int priority, const char *fmt, va_list ap)
120 {
121  vsyslog(priority, fmt, ap);
122 }
123 
124 void ulog_udebug(struct udebug_buf *_udb)
125 {
126  udb = _udb;
127 }
128 
129 void ulog_open(int channels, int facility, const char *ident)
130 {
131  ulog_close();
132 
133  _ulog_channels = channels;
134  _ulog_facility = facility;
135  _ulog_ident = ident;
136 }
137 
138 void ulog_close(void)
139 {
140  if (!_ulog_initialized)
141  return;
142 
144  closelog();
145 
146  _ulog_initialized = 0;
147 }
148 
149 void ulog_threshold(int threshold)
150 {
151  _ulog_threshold = threshold;
152 }
153 
154 void ulog(int priority, const char *fmt, ...)
155 {
156  va_list ap;
157 
158  if (udb) {
159  va_start(ap, fmt);
161  udebug_entry_vprintf(udb, fmt, ap);
163  va_end(ap);
164  }
165 
166  if (priority > _ulog_threshold)
167  return;
168 
169  ulog_defaults();
170 
172  {
173  va_start(ap, fmt);
174  ulog_kmsg(priority, fmt, ap);
175  va_end(ap);
176  }
177 
179  {
180  va_start(ap, fmt);
181  ulog_stdio(priority, fmt, ap);
182  va_end(ap);
183  }
184 
186  {
187  va_start(ap, fmt);
188  ulog_syslog(priority, fmt, ap);
189  va_end(ap);
190  }
191 }
FILE(GLOB test_cases "test-*.c") MACRO(ADD_FUZZER_TEST name) ADD_EXECUTABLE($
Definition: CMakeLists.txt:1
void udebug_entry_add(struct udebug_buf *buf)
Definition: udebug.c:642
int udebug_entry_vprintf(struct udebug_buf *buf, const char *fmt, va_list ap)
Definition: udebug.c:607
static void udebug_entry_init(struct udebug_buf *buf)
Definition: udebug.h:151
static const char * ulog_default_ident(void)
Definition: ulog.c:34
static int _ulog_initialized
Definition: ulog.c:30
static int _ulog_facility
Definition: ulog.c:28
static struct udebug_buf * udb
Definition: ulog.c:32
static int _ulog_threshold
Definition: ulog.c:29
static int _ulog_channels
Definition: ulog.c:27
static void ulog_defaults(void)
Definition: ulog.c:55
void ulog_open(int channels, int facility, const char *ident)
Definition: ulog.c:129
void ulog_threshold(int threshold)
Definition: ulog.c:149
void ulog_close(void)
Definition: ulog.c:138
__attribute__((format(printf, 2, 0)))
Definition: ulog.c:91
static const char * _ulog_ident
Definition: ulog.c:31
void ulog(int priority, const char *fmt,...)
Definition: ulog.c:154
void ulog_udebug(struct udebug_buf *_udb)
Definition: ulog.c:124
@ ULOG_SYSLOG
Definition: ulog.h:28
@ ULOG_STDIO
Definition: ulog.h:29
@ ULOG_KMSG
Definition: ulog.h:27