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 UNUSED, struct log_msg *m)
32 // This is a completely bare version of the log-file module. Errors are ignored.
33 write(2, m->m, m->m_len);
37 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 : &log_stream_default);
68 return log_streams.ptr[n];
74 vmsg(uns cat, const char *fmt, va_list args)
84 struct log_stream *ls = log_stream_by_flags(cat);
85 struct log_msg m = { .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 = &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);
113 m.stime = "\?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?";
114 m.sutime = ".\?\?\?\?\?\?";
117 /* Generate the message string */
118 va_copy(args2, args);
119 len = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args2);
121 if (len < (int) sizeof(msgbuf))
125 m.raw_msg = xmalloc(len+1);
126 vsnprintf(m.raw_msg, len+1, fmt, args);
129 /* Remove non-printable characters and newlines */
133 if (*p < 0x20 && *p != '\t')
138 /* Pass the message to the log_stream */
139 if (log_pass_msg(0, ls, &m))
141 /* Error (such as infinite loop) occurred */
142 log_pass_msg(0, &log_stream_default, &m);
145 if (m.raw_msg != msgbuf)
149 /* Maximal depth of log_pass_msg recursion */
150 #define LS_MAX_DEPTH 64
153 log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m)
157 /* Check recursion depth */
158 if (depth > LS_MAX_DEPTH)
160 struct log_msg errm = *m;
161 errm.flags = L_ERROR | (m->flags & LS_INTERNAL_MASK);
162 errm.raw_msg = "Loop in the log_stream system detected.";
163 log_pass_msg(0, &log_stream_default, &errm);
166 /* Filter by level and hook function */
167 if (!((1 << LS_GET_LEVEL(m->flags)) & ls->levels))
169 if (ls->filter && ls->filter(ls, m))
172 /* Pass the message to substreams */
173 CLIST_FOR_EACH(simp_node *, s, ls->substreams)
174 if (log_pass_msg(depth+1, s->p, m))
177 /* Will pass to the handler of this stream... is there any? */
181 /* Upper bound on message length */
182 int len = strlen(m->raw_msg) + strlen(m->stime) + strlen(m->sutime) + 32;
184 len += strlen(log_title);
186 len += strlen(ls->name);
188 /* Get a buffer and format the message */
189 char *free_buf = NULL;
193 m->m = free_buf = xmalloc(len);
196 /* Level (2 chars) */
197 if (ls->msgfmt & LSFMT_LEVEL)
199 *p++ = LS_LEVEL_LETTER(LS_GET_LEVEL(m->flags));
203 /* Time (|stime| + |sutime| + 1 chars) */
204 if (ls->msgfmt & LSFMT_TIME)
206 const char *q = m->stime;
209 if (ls->msgfmt & LSFMT_USEC)
218 /* Process name, PID ( |log_title| + 6 + (|PID|<=10) chars ) */
219 if ((ls->msgfmt & LSFMT_TITLE) && log_title)
221 if (ls->msgfmt & LSFMT_PID)
222 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
224 p += sprintf(p, "[%s] ", log_title);
228 if (ls->msgfmt & LSFMT_PID)
229 p += sprintf(p, "[%d] ", log_pid);
232 /* log_stream name ( |ls->name| + 4 chars ) */
233 if (ls->msgfmt & LSFMT_LOGNAME)
236 p += sprintf(p, "<%s> ", ls->name);
238 p += sprintf(p, "<?> ");
241 /* The message itself ( |m| + 1 chars ) */
243 const char *q = m->raw_msg;
257 /*** Utility functions ***/
260 msg(unsigned int cat, const char *fmt, ...)
265 vmsg(cat, fmt, args);
270 die(const char *fmt, ...)
275 vmsg(L_FATAL, fmt, args);
279 #ifdef DEBUG_DIE_BY_ABORT
287 assert_failed(const char *assertion, const char *file, int line)
289 msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
294 assert_failed_noinfo(void)
296 die("Internal error: Assertion failed.");
300 log_basename(const char *n)
311 log_init(const char *argv0)
315 static char log_progname[32];
316 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
317 log_progname[sizeof(log_progname)-1] = 0;
318 log_title = log_progname;
334 struct log_stream *ls = log_new_syslog(LOG_USER, "syslog");
335 msg(L_INFO | ls->regnum, "Brum <%300s>", ":-)");
336 log_set_format(log_default_stream(), ~0U, LSFMT_USEC);
337 msg(L_INFO, "Brum <%300s>", ":-)");