- p = buf = alloca(buflen);
- *p++ = cat & 0xff;
- if (have_tm)
- {
- p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S", &tm);
- if (log_precise_timings)
- p += sprintf(p, ".%06d", (int)tv.tv_usec);
- }
- else
- {
- p += sprintf(p, " \?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?");
- if (log_precise_timings)
- p += sprintf(p, ".\?\?\?\?\?\?");
- }
+ strftime(stime, sizeof(stime), "%Y-%m-%d %H:%M:%S", &tm);
+ snprintf(sutime, sizeof(sutime), ".%06d", (int)tv.tv_usec);
+ m.stime = stime;
+ m.sutime = sutime;
+ }
+ else
+ {
+ m.stime = "\?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?";
+ m.sutime = ".\?\?\?\?\?\?";
+ }
+
+ /* Generate the message string */
+ va_copy(args2, args);
+ len = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args2);
+ va_end(args2);
+ if (len < (int) sizeof(msgbuf))
+ m.raw_msg = msgbuf;
+ else
+ {
+ m.raw_msg = xmalloc(len+1);
+ vsnprintf(m.raw_msg, len+1, fmt, args);
+ }
+
+ /* Remove non-printable characters and newlines */
+ p = m.raw_msg;
+ while (*p)
+ {
+ if (*p < 0x20 && *p != '\t')
+ *p = 0x7f;
+ p++;
+ }
+
+ /* Pass the message to the log_stream */
+ if (log_pass_msg(0, ls, &m))
+ {
+ /* Error (such as infinite loop) occurred */
+ log_pass_msg(0, (struct log_stream *) &log_stream_default, &m);
+ }
+
+ if (m.raw_msg != msgbuf)
+ xfree(m.raw_msg);
+}
+
+/* Maximal depth of log_pass_msg recursion */
+#define LS_MAX_DEPTH 64
+
+int
+log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m)
+{
+ ASSERT(ls);
+
+ /* Check recursion depth */
+ if (depth > LS_MAX_DEPTH)
+ {
+ struct log_msg errm = *m;
+ errm.flags = L_ERROR | (m->flags & LS_INTERNAL_MASK);
+ errm.raw_msg = "Loop in the log_stream system detected.";
+ log_pass_msg(0, (struct log_stream *) &log_stream_default, &errm);
+ }
+
+ /* Filter by level and hook function */
+ if (!((1 << LS_GET_LEVEL(m->flags)) & ls->levels))
+ return 0;
+ if (ls->filter && ls->filter(ls, m))
+ return 0;
+
+ /* Pass the message to substreams */
+ CLIST_FOR_EACH(simp_node *, s, ls->substreams)
+ if (log_pass_msg(depth+1, s->p, m))
+ return 1;
+
+ /* Will pass to the handler of this stream... is there any? */
+ if (!ls->handler)
+ return 0;
+
+ /* Upper bound on message length */
+ int len = strlen(m->raw_msg) + strlen(m->stime) + strlen(m->sutime) + 32;
+ if (log_title)
+ len += strlen(log_title);
+ if (ls->name)
+ len += strlen(ls->name);
+
+ /* Get a buffer and format the message */
+ char *free_buf = NULL;
+ if (len <= 256)
+ m->m = alloca(len);
+ else
+ m->m = free_buf = xmalloc(len);
+ char *p = m->m;
+
+ /* Level (2 chars) */
+ if (ls->msgfmt & LSFMT_LEVEL)
+ {
+ *p++ = LS_LEVEL_LETTER(LS_GET_LEVEL(m->flags));