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.
56 * When stream #0 is requested, fall back to log_stream_default.
60 log_stream_by_flags(uns flags)
62 int n = LS_GET_STRNUM(flags);
63 if (n < 0 || n >= log_streams_after || log_streams.ptr[n]->regnum == -1)
64 return (n ? NULL : (struct log_stream *) &log_stream_default);
65 return log_streams.ptr[n];
71 vmsg(uns cat, const char *fmt, va_list args)
82 struct log_stream *ls = log_stream_by_flags(cat);
84 /* Check the stream existence */
87 msg((LS_INTERNAL_MASK&cat)|L_WARN, "No log_stream with number %d! Logging to the default log.", LS_GET_STRNUM(cat));
88 ls = (struct log_stream *) &log_stream_default;
91 /* Get the current time */
92 if (!(cat & LSFLAG_SIGHANDLER))
94 /* CAVEAT: These calls are not safe in signal handlers. */
95 gettimeofday(&tv, NULL);
96 if (localtime_r(&tv.tv_sec, &tm))
100 /* Generate time strings */
103 strftime(stime, sizeof(stime), "%Y-%m-%d %H:%M:%S", &tm);
104 snprintf(sutime, sizeof(sutime), ".%06d", (int)tv.tv_usec);
108 snprintf(stime, sizeof(stime), "\?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?");
109 snprintf(sutime, sizeof(sutime), ".\?\?\?\?\?\?");
112 /* Generate the message string */
113 va_copy(args2, args);
114 len = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args2);
116 if (len < (int) sizeof(msgbuf))
121 vsnprintf(m, len+1, fmt, args);
124 /* Remove non-printable characters and newlines */
128 if (*p < 0x20 && *p != '\t')
133 /* Pass the message to the log_stream */
134 if (log_pass_msg(0, ls, stime, sutime, m, cat))
136 /* Error (such as infinite loop) occurred */
137 log_pass_msg(0, (struct log_stream *) &log_stream_default, stime, sutime, m, cat);
145 log_pass_msg(int depth, struct log_stream *ls, const char *stime, const char *sutime, const char *m, uns cat)
149 /* Check recursion depth */
150 if (depth > LS_MAX_DEPTH)
152 log_pass_msg(0, (struct log_stream *) &log_stream_default, stime, sutime,
153 "Loop in the log_stream system detected.", L_ERROR | (cat & LS_INTERNAL_MASK));
157 /* Filter by level and hook function */
158 if (!((1 << LS_GET_LEVEL(cat)) & ls->levels))
160 if (ls->filter && ls->filter(ls, m, cat))
163 /* Pass the message to substreams */
164 CLIST_FOR_EACH(simp_node *, s, ls->substreams)
165 if (log_pass_msg(depth+1, s->p, stime, sutime, m, cat))
168 /* Will pass to the handler of this stream... is there any? */
172 /* Upper bound on message length */
173 int len = strlen(m) + strlen(stime) + strlen(sutime) + 32;
175 len += strlen(log_title);
177 len += strlen(ls->name);
179 /* Get a buffer and format the message */
180 char *free_buf = NULL;
185 buf = free_buf = xmalloc(len);
188 /* Level (2 chars) */
189 if (ls->msgfmt & LSFMT_LEVEL)
191 *p++ = LS_LEVEL_LETTER(LS_GET_LEVEL(cat));
195 /* Time (|stime| + |sutime| + 1 chars) */
196 if (ls->msgfmt & LSFMT_TIME)
198 const char *q = (char *)stime;
201 if (ls->msgfmt & LSFMT_USEC)
210 /* Process name, PID ( |log_title| + 6 + (|PID|<=10) chars ) */
211 if ((ls->msgfmt & LSFMT_TITLE) && log_title)
213 if (ls->msgfmt & LSFMT_PID)
214 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
216 p += sprintf(p, "[%s] ", log_title);
220 if (ls->msgfmt & LSFMT_PID)
221 p += sprintf(p, "[%d] ", log_pid);
224 /* log_stream name ( |ls->name| + 4 chars ) */
225 if (ls->msgfmt & LSFMT_LOGNAME)
228 p += sprintf(p, "<%s> ", ls->name);
230 p += sprintf(p, "<?> ");
233 /* The message itself ( |m| + 1 chars ) */
240 ls->handler(ls, buf, cat);
248 /*** Utility functions ***/
251 msg(unsigned int cat, const char *fmt, ...)
256 vmsg(cat, fmt, args);
261 die(const char *fmt, ...)
266 vmsg(L_FATAL, fmt, args);
270 #ifdef DEBUG_DIE_BY_ABORT
278 assert_failed(const char *assertion, const char *file, int line)
280 msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
285 assert_failed_noinfo(void)
287 die("Internal error: Assertion failed.");
291 log_basename(const char *n)
302 log_init(const char *argv0)
306 static char log_progname[32];
307 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
308 log_progname[sizeof(log_progname)-1] = 0;
309 log_title = log_progname;
317 msg(L_INFO, "Brum <%300s>", ":-)");