+ 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) || sighandler)
+ 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 >= 0 && *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, &log_stream_default, &m);
+ }
+
+ if (m.raw_msg != msgbuf)
+ xfree(m.raw_msg);
+}
+
+static void
+log_report_err(struct log_stream *ls, struct log_msg *m, int err)
+{
+ if (m->flags & L_LOGGER_ERR)
+ return;
+ if (ls->stream_flags & LSFLAG_ERR_REPORTED)
+ return;
+ ls->stream_flags |= LSFLAG_ERR_REPORTED;
+
+ struct log_msg errm = *m;
+ char errbuf[128];
+ char *name = (ls->name ? : "<unnamed>");
+
+ errm.flags = ((ls->stream_flags & LSFLAG_ERR_IS_FATAL) ? L_FATAL : L_ERROR);
+ errm.flags |= L_LOGGER_ERR | (m->flags & LS_CTRL_MASK);
+ errm.raw_msg = errbuf;
+ if (err == EDEADLK)
+ snprintf(errbuf, sizeof(errbuf), "Error logging to %s: Maximum nesting level of log streams exceeded", name);
+ else
+ {
+ errno = err;
+ snprintf(errbuf, sizeof(errbuf), "Error logging to %s: %m", name);
+ }
+ log_pass_msg(0, &log_stream_default, &errm);
+
+ if (ls->stream_flags & LSFLAG_ERR_IS_FATAL)
+ do_die();
+}
+
+/* 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)
+ {
+ log_report_err(ls, m, EDEADLK);
+ return 1;
+ }
+
+ /* Filter by level, type and hook function */
+ if (!((1 << LS_GET_LEVEL(m->flags)) & ls->levels) ||
+ !((1 << LS_GET_TYPE(m->flags)) & ls->types) ||
+ 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;
+
+ /* Will print a message type? */
+ char *type = NULL;
+ if ((ls->msgfmt & LSFMT_TYPE) && LS_GET_TYPE(m->flags))
+ type = log_type_name(m->flags);
+
+ /* 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);
+ if (type)
+ len += strlen(type) + 3;
+
+ /* Get a buffer and format the message */
+ char *free_buf = NULL;
+ if (len <= 256 || (m->flags & L_SIGHANDLER))
+ 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));