int log_pid;
void (*log_die_hook)(void);
+static void NONRET do_die(void);
+
/*** The default log stream, which logs to stderr ***/
static int default_log_handler(struct log_stream *ls UNUSED, struct log_msg *m)
.use_count = 1000000,
.handler = default_log_handler,
.levels = ~0U,
+ .types = ~0U,
.msgfmt = LSFMT_DEFAULT,
// an empty clist
.substreams.head.next = (cnode *) &log_stream_default.substreams.head,
return log_streams.ptr[n];
}
+/*** Known message types ***/
+
+char **log_type_names;
+
+char *
+log_type_name(uns flags)
+{
+ uns type = LS_GET_TYPE(flags);
+
+ if (!log_type_names || !log_type_names[type])
+ return "default";
+ else
+ return log_type_names[type];
+}
+
/*** Logging ***/
void
char msgbuf[256];
char *p;
int len;
- struct log_stream *ls = log_stream_by_flags(cat);
+ uns sighandler = cat & L_SIGHANDLER;
+ struct log_stream *ls;
struct log_msg m = { .flags = cat };
- /* Check the stream existence */
- if (!ls)
+ /* Find the destination stream */
+ if (sighandler)
+ ls = &log_stream_default;
+ else if (!(ls = log_stream_by_flags(cat)))
{
msg((LS_CTRL_MASK&cat)|L_WARN, "No log_stream with number %d! Logging to the default log.", LS_GET_STRNUM(cat));
ls = &log_stream_default;
}
/* Get the current time */
- if (!(cat & L_SIGHANDLER))
+ if (!sighandler)
{
/* CAVEAT: These calls are not safe in signal handlers. */
gettimeofday(&tv, NULL);
+ m.tv = &tv;
if (localtime_r(&tv.tv_sec, &tm))
m.tm = &tm;
}
va_copy(args2, args);
len = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args2);
va_end(args2);
- if (len < (int) sizeof(msgbuf))
+ if (len < (int) sizeof(msgbuf) || sighandler)
m.raw_msg = msgbuf;
else
{
p = m.raw_msg;
while (*p)
{
- if (*p < 0x20 && *p != '\t')
+ if (*p >= 0 && *p < 0x20 && *p != '\t')
*p = 0x7f;
p++;
}
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
/* Check recursion depth */
if (depth > LS_MAX_DEPTH)
{
- struct log_msg errm = *m;
- errm.flags = L_ERROR | (m->flags & LS_CTRL_MASK);
- errm.raw_msg = "Loop in the log_stream system detected.";
- log_pass_msg(0, &log_stream_default, &errm);
+ log_report_err(ls, m, EDEADLK);
+ return 1;
}
- /* Filter by level and hook function */
- if (!((1 << LS_GET_LEVEL(m->flags)) & ls->levels))
- return 0;
- if (ls->filter && ls->filter(ls, m))
+ /* 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 */
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)
+ if (len <= 256 || (m->flags & L_SIGHANDLER))
m->m = alloca(len);
else
m->m = free_buf = xmalloc(len);
/* Process name, PID ( |log_title| + 6 + (|PID|<=10) chars ) */
if ((ls->msgfmt & LSFMT_TITLE) && log_title)
{
- if (ls->msgfmt & LSFMT_PID)
+ if ((ls->msgfmt & LSFMT_PID) && log_pid)
p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
else
p += sprintf(p, "[%s] ", log_title);
}
else
{
- if (ls->msgfmt & LSFMT_PID)
+ if ((ls->msgfmt & LSFMT_PID) && log_pid)
p += sprintf(p, "[%d] ", log_pid);
}
p += sprintf(p, "<?> ");
}
+ /* Message type ( |type| + 3 chars ) */
+ if (type)
+ p += sprintf(p, "{%s} ", type);
+
/* The message itself ( |m| + 1 chars ) */
{
const char *q = m->raw_msg;
*p++ = '\n';
*p = '\0';
m->m_len = p - m->m;
- ls->handler(ls, m);
+ int err = ls->handler(ls, m);
+ if (err)
+ log_report_err(ls, m, err);
}
if (free_buf)
va_end(args);
}
+static void NONRET
+do_die(void)
+{
+#ifdef DEBUG_DIE_BY_ABORT
+ abort();
+#else
+ exit(1);
+#endif
+}
+
void
die(const char *fmt, ...)
{
va_end(args);
if (log_die_hook)
log_die_hook();
-#ifdef DEBUG_DIE_BY_ABORT
- abort();
-#else
- exit(1);
-#endif
+ do_die();
}
void
int main(void)
{
- struct log_stream *ls = log_new_syslog(LOG_USER, "syslog");
+ int type = log_find_type("foo");
+ ASSERT(type < 0);
+ type = log_register_type("foo");
+
+ struct log_stream *ls = log_new_syslog("local3", 0);
+#if 0
+ log_add_substream(ls, ls);
+ ls->stream_flags |= LSFLAG_ERR_IS_FATAL;
+#endif
msg(L_INFO | ls->regnum, "Brum <%300s>", ":-)");
- log_set_format(log_default_stream(), ~0U, LSFMT_USEC);
- msg(L_INFO, "Brum <%300s>", ":-)");
+ log_set_format(log_default_stream(), ~0U, LSFMT_USEC | LSFMT_TYPE);
+ msg(L_INFO | type, "Brum <%300s>", ":-)");
+ log_close_all();
return 0;
}