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/log-internal.h"
14 #include "ucw/simple-lists.h"
27 void (*log_die_hook)(void);
29 static void NONRET do_die(void);
31 /*** The default log stream, which logs to stderr ***/
33 static int default_log_handler(struct log_stream *ls UNUSED, struct log_msg *m)
35 // This is a completely bare version of the log-file module. Errors are ignored.
36 write(2, m->m, m->m_len);
40 struct log_stream log_stream_default = {
43 .handler = default_log_handler,
45 .msgfmt = LSFMT_DEFAULT,
47 .substreams.head.next = (cnode *) &log_stream_default.substreams.head,
48 .substreams.head.prev = (cnode *) &log_stream_default.substreams.head,
51 /*** Registry of streams and their identifiers ***/
53 struct lsbuf_t log_streams; /* A growing array of pointers to log_streams */
54 int log_streams_after = 0; /* The first never-used index in log_streams.ptr */
57 * Find a stream by its identifier given as LS_SET_STRNUM(flags).
58 * Returns NULL if the stream doesn't exist or it's invalid.
60 * If the log-stream machinery has not been initialized (which is normal for programs
61 * with no fancy logging), the log_streams gbuf is empty and this function only
62 * translates stream #0 to the static log_stream_default.
66 log_stream_by_flags(uns flags)
68 int n = LS_GET_STRNUM(flags);
69 if (n < 0 || n >= log_streams_after || log_streams.ptr[n]->regnum == -1)
70 return (n ? NULL : &log_stream_default);
71 return log_streams.ptr[n];
77 vmsg(uns cat, const char *fmt, va_list args)
87 struct log_stream *ls = log_stream_by_flags(cat);
88 struct log_msg m = { .flags = cat };
90 /* Check the stream existence */
93 msg((LS_CTRL_MASK&cat)|L_WARN, "No log_stream with number %d! Logging to the default log.", LS_GET_STRNUM(cat));
94 ls = &log_stream_default;
97 /* Get the current time */
98 if (!(cat & L_SIGHANDLER))
100 /* CAVEAT: These calls are not safe in signal handlers. */
101 gettimeofday(&tv, NULL);
102 if (localtime_r(&tv.tv_sec, &tm))
106 /* Generate time strings */
109 strftime(stime, sizeof(stime), "%Y-%m-%d %H:%M:%S", &tm);
110 snprintf(sutime, sizeof(sutime), ".%06d", (int)tv.tv_usec);
116 m.stime = "\?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?";
117 m.sutime = ".\?\?\?\?\?\?";
120 /* Generate the message string */
121 va_copy(args2, args);
122 len = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args2);
124 if (len < (int) sizeof(msgbuf))
128 m.raw_msg = xmalloc(len+1);
129 vsnprintf(m.raw_msg, len+1, fmt, args);
132 /* Remove non-printable characters and newlines */
136 if (*p < 0x20 && *p != '\t')
141 /* Pass the message to the log_stream */
142 if (log_pass_msg(0, ls, &m))
144 /* Error (such as infinite loop) occurred */
145 log_pass_msg(0, &log_stream_default, &m);
148 if (m.raw_msg != msgbuf)
153 log_report_err(struct log_stream *ls, struct log_msg *m, int err)
155 if (m->flags & L_LOGGER_ERR)
157 if (ls->stream_flags & LSFLAG_ERR_REPORTED)
159 ls->stream_flags |= LSFLAG_ERR_REPORTED;
161 struct log_msg errm = *m;
163 char *name = (ls->name ? : "<unnamed>");
165 errm.flags = ((ls->stream_flags & LSFLAG_ERR_IS_FATAL) ? L_FATAL : L_ERROR);
166 errm.flags |= L_LOGGER_ERR | (m->flags & LS_CTRL_MASK);
167 errm.raw_msg = errbuf;
169 snprintf(errbuf, sizeof(errbuf), "Error logging to %s: Maximum nesting level of log streams exceeded", name);
173 snprintf(errbuf, sizeof(errbuf), "Error logging to %s: %m", name);
175 log_pass_msg(0, &log_stream_default, &errm);
177 if (ls->stream_flags & LSFLAG_ERR_IS_FATAL)
181 /* Maximal depth of log_pass_msg recursion */
182 #define LS_MAX_DEPTH 64
185 log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m)
189 /* Check recursion depth */
190 if (depth > LS_MAX_DEPTH)
192 log_report_err(ls, m, EDEADLK);
196 /* Filter by level and hook function */
197 if (!((1 << LS_GET_LEVEL(m->flags)) & ls->levels))
199 if (ls->filter && ls->filter(ls, m))
202 /* Pass the message to substreams */
203 CLIST_FOR_EACH(simp_node *, s, ls->substreams)
204 if (log_pass_msg(depth+1, s->p, m))
207 /* Will pass to the handler of this stream... is there any? */
211 /* Upper bound on message length */
212 int len = strlen(m->raw_msg) + strlen(m->stime) + strlen(m->sutime) + 32;
214 len += strlen(log_title);
216 len += strlen(ls->name);
218 /* Get a buffer and format the message */
219 char *free_buf = NULL;
223 m->m = free_buf = xmalloc(len);
226 /* Level (2 chars) */
227 if (ls->msgfmt & LSFMT_LEVEL)
229 *p++ = LS_LEVEL_LETTER(LS_GET_LEVEL(m->flags));
233 /* Time (|stime| + |sutime| + 1 chars) */
234 if (ls->msgfmt & LSFMT_TIME)
236 const char *q = m->stime;
239 if (ls->msgfmt & LSFMT_USEC)
248 /* Process name, PID ( |log_title| + 6 + (|PID|<=10) chars ) */
249 if ((ls->msgfmt & LSFMT_TITLE) && log_title)
251 if ((ls->msgfmt & LSFMT_PID) && log_pid)
252 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
254 p += sprintf(p, "[%s] ", log_title);
258 if (ls->msgfmt & LSFMT_PID)
259 p += sprintf(p, "[%d] ", log_pid);
262 /* log_stream name ( |ls->name| + 4 chars ) */
263 if (ls->msgfmt & LSFMT_LOGNAME)
266 p += sprintf(p, "<%s> ", ls->name);
268 p += sprintf(p, "<?> ");
271 /* The message itself ( |m| + 1 chars ) */
273 const char *q = m->raw_msg;
279 int err = ls->handler(ls, m);
281 log_report_err(ls, m, err);
289 /*** Utility functions ***/
292 msg(unsigned int cat, const char *fmt, ...)
297 vmsg(cat, fmt, args);
304 #ifdef DEBUG_DIE_BY_ABORT
312 die(const char *fmt, ...)
317 vmsg(L_FATAL, fmt, args);
325 assert_failed(const char *assertion, const char *file, int line)
327 msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
332 assert_failed_noinfo(void)
334 die("Internal error: Assertion failed.");
338 log_basename(const char *n)
349 log_init(const char *argv0)
353 static char log_progname[32];
354 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
355 log_progname[sizeof(log_progname)-1] = 0;
356 log_title = log_progname;
372 struct log_stream *ls = log_new_syslog("local3", 0);
374 log_add_substream(ls, ls);
375 ls->stream_flags |= LSFLAG_ERR_IS_FATAL;
377 msg(L_INFO | ls->regnum, "Brum <%300s>", ":-)");
378 log_set_format(log_default_stream(), ~0U, LSFMT_USEC);
379 msg(L_INFO, "Brum <%300s>", ":-)");