2 * UCW Library -- Logging
4 * (c) 1997--2009 Martin Mares <mj@ucw.cz>
5 * (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
13 #include "ucw/simple-lists.h"
26 void (*log_die_hook)(void);
28 /*** The default log stream, which logs to stderr ***/
30 static int default_log_handler(struct log_stream *ls, const char *m, uns cat UNUSED)
32 // This is a completely bare version of the log-file module. Errors are ignored.
33 write(ls->idata, m, strlen(m));
37 const struct log_stream log_stream_default = {
40 .handler = default_log_handler,
41 .levels = LS_ALL_LEVELS,
42 .msgfmt = LSFMT_DEFAULT,
44 .substreams.head.next = (cnode *) &log_stream_default.substreams.head,
45 .substreams.head.prev = (cnode *) &log_stream_default.substreams.head,
48 /*** Registry of streams and their identifiers ***/
50 struct lsbuf_t log_streams; /* A growing array of pointers to log_streams */
51 int log_streams_after = 0; /* The first never-used index in log_streams.ptr */
54 * Find a stream by its identifier given as LS_SET_STRNUM(flags).
55 * Returns NULL if the stream doesn't exist or it's invalid.
57 * If the log-stream machinery has not been initialized (which is normal for programs
58 * with no fancy logging), the log_streams gbuf is empty and this function only
59 * translates stream #0 to the static log_stream_default.
63 log_stream_by_flags(uns flags)
65 int n = LS_GET_STRNUM(flags);
66 if (n < 0 || n >= log_streams_after || log_streams.ptr[n]->regnum == -1)
67 return (n ? NULL : (struct log_stream *) &log_stream_default);
68 return log_streams.ptr[n];
74 vmsg(uns cat, const char *fmt, va_list args)
85 struct log_stream *ls = log_stream_by_flags(cat);
87 /* Check the stream existence */
90 msg((LS_INTERNAL_MASK&cat)|L_WARN, "No log_stream with number %d! Logging to the default log.", LS_GET_STRNUM(cat));
91 ls = (struct log_stream *) &log_stream_default;
94 /* Get the current time */
95 if (!(cat & LSFLAG_SIGHANDLER))
97 /* CAVEAT: These calls are not safe in signal handlers. */
98 gettimeofday(&tv, NULL);
99 if (localtime_r(&tv.tv_sec, &tm))
103 /* Generate time strings */
106 strftime(stime, sizeof(stime), "%Y-%m-%d %H:%M:%S", &tm);
107 snprintf(sutime, sizeof(sutime), ".%06d", (int)tv.tv_usec);
111 snprintf(stime, sizeof(stime), "\?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?");
112 snprintf(sutime, sizeof(sutime), ".\?\?\?\?\?\?");
115 /* Generate the message string */
116 va_copy(args2, args);
117 len = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args2);
119 if (len < (int) sizeof(msgbuf))
124 vsnprintf(m, len+1, fmt, args);
127 /* Remove non-printable characters and newlines */
131 if (*p < 0x20 && *p != '\t')
136 /* Pass the message to the log_stream */
137 if (log_pass_msg(0, ls, stime, sutime, m, cat))
139 /* Error (such as infinite loop) occurred */
140 log_pass_msg(0, (struct log_stream *) &log_stream_default, stime, sutime, m, cat);
147 /* Maximal depth of log_pass_msg recursion */
148 #define LS_MAX_DEPTH 64
151 log_pass_msg(int depth, struct log_stream *ls, const char *stime, const char *sutime, const char *m, uns cat)
155 /* Check recursion depth */
156 if (depth > LS_MAX_DEPTH)
158 log_pass_msg(0, (struct log_stream *) &log_stream_default, stime, sutime,
159 "Loop in the log_stream system detected.", L_ERROR | (cat & LS_INTERNAL_MASK));
163 /* Filter by level and hook function */
164 if (!((1 << LS_GET_LEVEL(cat)) & ls->levels))
166 if (ls->filter && ls->filter(ls, m, cat))
169 /* Pass the message to substreams */
170 CLIST_FOR_EACH(simp_node *, s, ls->substreams)
171 if (log_pass_msg(depth+1, s->p, stime, sutime, m, cat))
174 /* Will pass to the handler of this stream... is there any? */
178 /* Upper bound on message length */
179 int len = strlen(m) + strlen(stime) + strlen(sutime) + 32;
181 len += strlen(log_title);
183 len += strlen(ls->name);
185 /* Get a buffer and format the message */
186 char *free_buf = NULL;
191 buf = free_buf = xmalloc(len);
194 /* Level (2 chars) */
195 if (ls->msgfmt & LSFMT_LEVEL)
197 *p++ = LS_LEVEL_LETTER(LS_GET_LEVEL(cat));
201 /* Time (|stime| + |sutime| + 1 chars) */
202 if (ls->msgfmt & LSFMT_TIME)
204 const char *q = (char *)stime;
207 if (ls->msgfmt & LSFMT_USEC)
216 /* Process name, PID ( |log_title| + 6 + (|PID|<=10) chars ) */
217 if ((ls->msgfmt & LSFMT_TITLE) && log_title)
219 if (ls->msgfmt & LSFMT_PID)
220 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
222 p += sprintf(p, "[%s] ", log_title);
226 if (ls->msgfmt & LSFMT_PID)
227 p += sprintf(p, "[%d] ", log_pid);
230 /* log_stream name ( |ls->name| + 4 chars ) */
231 if (ls->msgfmt & LSFMT_LOGNAME)
234 p += sprintf(p, "<%s> ", ls->name);
236 p += sprintf(p, "<?> ");
239 /* The message itself ( |m| + 1 chars ) */
246 ls->handler(ls, buf, cat);
254 /*** Utility functions ***/
257 msg(unsigned int cat, const char *fmt, ...)
262 vmsg(cat, fmt, args);
267 die(const char *fmt, ...)
272 vmsg(L_FATAL, fmt, args);
276 #ifdef DEBUG_DIE_BY_ABORT
284 assert_failed(const char *assertion, const char *file, int line)
286 msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
291 assert_failed_noinfo(void)
293 die("Internal error: Assertion failed.");
297 log_basename(const char *n)
308 log_init(const char *argv0)
312 static char log_progname[32];
313 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
314 log_progname[sizeof(log_progname)-1] = 0;
315 log_title = log_progname;
331 struct log_stream *ls = log_new_syslog(LOG_USER, "syslog");
332 msg(L_INFO | ls->regnum, "Brum <%300s>", ":-)");