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 /*** The default log stream, which logs to stderr ***/
31 static int default_log_handler(struct log_stream *ls UNUSED, struct log_msg *m)
33 // This is a completely bare version of the log-file module. Errors are ignored.
34 write(2, m->m, m->m_len);
38 struct log_stream log_stream_default = {
41 .handler = default_log_handler,
43 .msgfmt = LSFMT_DEFAULT,
45 .substreams.head.next = (cnode *) &log_stream_default.substreams.head,
46 .substreams.head.prev = (cnode *) &log_stream_default.substreams.head,
49 /*** Registry of streams and their identifiers ***/
51 struct lsbuf_t log_streams; /* A growing array of pointers to log_streams */
52 int log_streams_after = 0; /* The first never-used index in log_streams.ptr */
55 * Find a stream by its identifier given as LS_SET_STRNUM(flags).
56 * Returns NULL if the stream doesn't exist or it's invalid.
58 * If the log-stream machinery has not been initialized (which is normal for programs
59 * with no fancy logging), the log_streams gbuf is empty and this function only
60 * translates stream #0 to the static log_stream_default.
64 log_stream_by_flags(uns flags)
66 int n = LS_GET_STRNUM(flags);
67 if (n < 0 || n >= log_streams_after || log_streams.ptr[n]->regnum == -1)
68 return (n ? NULL : &log_stream_default);
69 return log_streams.ptr[n];
75 vmsg(uns cat, const char *fmt, va_list args)
85 struct log_stream *ls = log_stream_by_flags(cat);
86 struct log_msg m = { .flags = cat };
88 /* Check the stream existence */
91 msg((LS_CTRL_MASK&cat)|L_WARN, "No log_stream with number %d! Logging to the default log.", LS_GET_STRNUM(cat));
92 ls = &log_stream_default;
95 /* Get the current time */
96 if (!(cat & L_SIGHANDLER))
98 /* CAVEAT: These calls are not safe in signal handlers. */
99 gettimeofday(&tv, NULL);
100 if (localtime_r(&tv.tv_sec, &tm))
104 /* Generate time strings */
107 strftime(stime, sizeof(stime), "%Y-%m-%d %H:%M:%S", &tm);
108 snprintf(sutime, sizeof(sutime), ".%06d", (int)tv.tv_usec);
114 m.stime = "\?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?";
115 m.sutime = ".\?\?\?\?\?\?";
118 /* Generate the message string */
119 va_copy(args2, args);
120 len = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args2);
122 if (len < (int) sizeof(msgbuf))
126 m.raw_msg = xmalloc(len+1);
127 vsnprintf(m.raw_msg, len+1, fmt, args);
130 /* Remove non-printable characters and newlines */
134 if (*p < 0x20 && *p != '\t')
139 /* Pass the message to the log_stream */
140 if (log_pass_msg(0, ls, &m))
142 /* Error (such as infinite loop) occurred */
143 log_pass_msg(0, &log_stream_default, &m);
146 if (m.raw_msg != msgbuf)
150 /* Maximal depth of log_pass_msg recursion */
151 #define LS_MAX_DEPTH 64
154 log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m)
158 /* Check recursion depth */
159 if (depth > LS_MAX_DEPTH)
161 struct log_msg errm = *m;
162 errm.flags = L_ERROR | (m->flags & LS_CTRL_MASK);
163 errm.raw_msg = "Loop in the log_stream system detected.";
164 log_pass_msg(0, &log_stream_default, &errm);
167 /* Filter by level and hook function */
168 if (!((1 << LS_GET_LEVEL(m->flags)) & ls->levels))
170 if (ls->filter && ls->filter(ls, m))
173 /* Pass the message to substreams */
174 CLIST_FOR_EACH(simp_node *, s, ls->substreams)
175 if (log_pass_msg(depth+1, s->p, m))
178 /* Will pass to the handler of this stream... is there any? */
182 /* Upper bound on message length */
183 int len = strlen(m->raw_msg) + strlen(m->stime) + strlen(m->sutime) + 32;
185 len += strlen(log_title);
187 len += strlen(ls->name);
189 /* Get a buffer and format the message */
190 char *free_buf = NULL;
194 m->m = free_buf = xmalloc(len);
197 /* Level (2 chars) */
198 if (ls->msgfmt & LSFMT_LEVEL)
200 *p++ = LS_LEVEL_LETTER(LS_GET_LEVEL(m->flags));
204 /* Time (|stime| + |sutime| + 1 chars) */
205 if (ls->msgfmt & LSFMT_TIME)
207 const char *q = m->stime;
210 if (ls->msgfmt & LSFMT_USEC)
219 /* Process name, PID ( |log_title| + 6 + (|PID|<=10) chars ) */
220 if ((ls->msgfmt & LSFMT_TITLE) && log_title)
222 if (ls->msgfmt & LSFMT_PID)
223 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
225 p += sprintf(p, "[%s] ", log_title);
229 if (ls->msgfmt & LSFMT_PID)
230 p += sprintf(p, "[%d] ", log_pid);
233 /* log_stream name ( |ls->name| + 4 chars ) */
234 if (ls->msgfmt & LSFMT_LOGNAME)
237 p += sprintf(p, "<%s> ", ls->name);
239 p += sprintf(p, "<?> ");
242 /* The message itself ( |m| + 1 chars ) */
244 const char *q = m->raw_msg;
258 /*** Utility functions ***/
261 msg(unsigned int cat, const char *fmt, ...)
266 vmsg(cat, fmt, args);
271 die(const char *fmt, ...)
276 vmsg(L_FATAL, fmt, args);
280 #ifdef DEBUG_DIE_BY_ABORT
288 assert_failed(const char *assertion, const char *file, int line)
290 msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
295 assert_failed_noinfo(void)
297 die("Internal error: Assertion failed.");
301 log_basename(const char *n)
312 log_init(const char *argv0)
316 static char log_progname[32];
317 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
318 log_progname[sizeof(log_progname)-1] = 0;
319 log_title = log_progname;
335 struct log_stream *ls = log_new_syslog("local3", 0);
336 msg(L_INFO | ls->regnum, "Brum <%300s>", ":-)");
337 log_set_format(log_default_stream(), ~0U, LSFMT_USEC);
338 msg(L_INFO, "Brum <%300s>", ":-)");