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,
46 .msgfmt = LSFMT_DEFAULT,
48 .substreams.head.next = (cnode *) &log_stream_default.substreams.head,
49 .substreams.head.prev = (cnode *) &log_stream_default.substreams.head,
52 /*** Registry of streams and their identifiers ***/
54 struct lsbuf_t log_streams; /* A growing array of pointers to log_streams */
55 int log_streams_after = 0; /* The first never-used index in log_streams.ptr */
58 * Find a stream by its identifier given as LS_SET_STRNUM(flags).
59 * Returns NULL if the stream doesn't exist or it's invalid.
61 * If the log-stream machinery has not been initialized (which is normal for programs
62 * with no fancy logging), the log_streams gbuf is empty and this function only
63 * translates stream #0 to the static log_stream_default.
67 log_stream_by_flags(uns flags)
69 int n = LS_GET_STRNUM(flags);
70 if (n < 0 || n >= log_streams_after || log_streams.ptr[n]->regnum == -1)
71 return (n ? NULL : &log_stream_default);
72 return log_streams.ptr[n];
75 /*** Known message types ***/
77 char **log_type_names;
80 log_type_name(uns flags)
82 uns type = LS_GET_TYPE(flags);
84 if (!log_type_names || !log_type_names[type])
87 return log_type_names[type];
93 vmsg(uns cat, const char *fmt, va_list args)
103 struct log_stream *ls = log_stream_by_flags(cat);
104 struct log_msg m = { .flags = cat };
106 /* Check the stream existence */
109 msg((LS_CTRL_MASK&cat)|L_WARN, "No log_stream with number %d! Logging to the default log.", LS_GET_STRNUM(cat));
110 ls = &log_stream_default;
113 /* Get the current time */
114 if (!(cat & L_SIGHANDLER))
116 /* CAVEAT: These calls are not safe in signal handlers. */
117 gettimeofday(&tv, NULL);
118 if (localtime_r(&tv.tv_sec, &tm))
122 /* Generate time strings */
125 strftime(stime, sizeof(stime), "%Y-%m-%d %H:%M:%S", &tm);
126 snprintf(sutime, sizeof(sutime), ".%06d", (int)tv.tv_usec);
132 m.stime = "\?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?";
133 m.sutime = ".\?\?\?\?\?\?";
136 /* Generate the message string */
137 va_copy(args2, args);
138 len = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args2);
140 if (len < (int) sizeof(msgbuf))
144 m.raw_msg = xmalloc(len+1);
145 vsnprintf(m.raw_msg, len+1, fmt, args);
148 /* Remove non-printable characters and newlines */
152 if (*p < 0x20 && *p != '\t')
157 /* Pass the message to the log_stream */
158 if (log_pass_msg(0, ls, &m))
160 /* Error (such as infinite loop) occurred */
161 log_pass_msg(0, &log_stream_default, &m);
164 if (m.raw_msg != msgbuf)
169 log_report_err(struct log_stream *ls, struct log_msg *m, int err)
171 if (m->flags & L_LOGGER_ERR)
173 if (ls->stream_flags & LSFLAG_ERR_REPORTED)
175 ls->stream_flags |= LSFLAG_ERR_REPORTED;
177 struct log_msg errm = *m;
179 char *name = (ls->name ? : "<unnamed>");
181 errm.flags = ((ls->stream_flags & LSFLAG_ERR_IS_FATAL) ? L_FATAL : L_ERROR);
182 errm.flags |= L_LOGGER_ERR | (m->flags & LS_CTRL_MASK);
183 errm.raw_msg = errbuf;
185 snprintf(errbuf, sizeof(errbuf), "Error logging to %s: Maximum nesting level of log streams exceeded", name);
189 snprintf(errbuf, sizeof(errbuf), "Error logging to %s: %m", name);
191 log_pass_msg(0, &log_stream_default, &errm);
193 if (ls->stream_flags & LSFLAG_ERR_IS_FATAL)
197 /* Maximal depth of log_pass_msg recursion */
198 #define LS_MAX_DEPTH 64
201 log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m)
205 /* Check recursion depth */
206 if (depth > LS_MAX_DEPTH)
208 log_report_err(ls, m, EDEADLK);
212 /* Filter by level, type and hook function */
213 if (!((1 << LS_GET_LEVEL(m->flags)) & ls->levels) ||
214 !((1 << LS_GET_TYPE(m->flags)) & ls->types) ||
215 ls->filter && ls->filter(ls, m))
218 /* Pass the message to substreams */
219 CLIST_FOR_EACH(simp_node *, s, ls->substreams)
220 if (log_pass_msg(depth+1, s->p, m))
223 /* Will pass to the handler of this stream... is there any? */
227 /* Will print a message type? */
229 if ((ls->msgfmt & LSFMT_TYPE) && LS_GET_TYPE(m->flags))
230 type = log_type_name(m->flags);
232 /* Upper bound on message length */
233 int len = strlen(m->raw_msg) + strlen(m->stime) + strlen(m->sutime) + 32;
235 len += strlen(log_title);
237 len += strlen(ls->name);
239 len += strlen(type) + 3;
241 /* Get a buffer and format the message */
242 char *free_buf = NULL;
246 m->m = free_buf = xmalloc(len);
249 /* Level (2 chars) */
250 if (ls->msgfmt & LSFMT_LEVEL)
252 *p++ = LS_LEVEL_LETTER(LS_GET_LEVEL(m->flags));
256 /* Time (|stime| + |sutime| + 1 chars) */
257 if (ls->msgfmt & LSFMT_TIME)
259 const char *q = m->stime;
262 if (ls->msgfmt & LSFMT_USEC)
271 /* Process name, PID ( |log_title| + 6 + (|PID|<=10) chars ) */
272 if ((ls->msgfmt & LSFMT_TITLE) && log_title)
274 if ((ls->msgfmt & LSFMT_PID) && log_pid)
275 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
277 p += sprintf(p, "[%s] ", log_title);
281 if ((ls->msgfmt & LSFMT_PID) && log_pid)
282 p += sprintf(p, "[%d] ", log_pid);
285 /* log_stream name ( |ls->name| + 4 chars ) */
286 if (ls->msgfmt & LSFMT_LOGNAME)
289 p += sprintf(p, "<%s> ", ls->name);
291 p += sprintf(p, "<?> ");
294 /* Message type ( |type| + 3 chars ) */
296 p += sprintf(p, "{%s} ", type);
298 /* The message itself ( |m| + 1 chars ) */
300 const char *q = m->raw_msg;
306 int err = ls->handler(ls, m);
308 log_report_err(ls, m, err);
316 /*** Utility functions ***/
319 msg(unsigned int cat, const char *fmt, ...)
324 vmsg(cat, fmt, args);
331 #ifdef DEBUG_DIE_BY_ABORT
339 die(const char *fmt, ...)
344 vmsg(L_FATAL, fmt, args);
352 assert_failed(const char *assertion, const char *file, int line)
354 msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
359 assert_failed_noinfo(void)
361 die("Internal error: Assertion failed.");
365 log_basename(const char *n)
376 log_init(const char *argv0)
380 static char log_progname[32];
381 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
382 log_progname[sizeof(log_progname)-1] = 0;
383 log_title = log_progname;
399 int type = log_find_type("foo");
401 type = log_register_type("foo");
403 struct log_stream *ls = log_new_syslog("local3", 0);
405 log_add_substream(ls, ls);
406 ls->stream_flags |= LSFLAG_ERR_IS_FATAL;
408 msg(L_INFO | ls->regnum, "Brum <%300s>", ":-)");
409 log_set_format(log_default_stream(), ~0U, LSFMT_USEC | LSFMT_TYPE);
410 msg(L_INFO | type, "Brum <%300s>", ":-)");