any control characters.
The first argument of `msg` can be OR'ed with additional flags. Most notably, you can
-add `L_SIGHANDLER` if you wish to log a message from a signal handler (calling
-time-related functions in libc from signal handlers is generally unsafe, so
-`msg` does not log a timestamp in such cases).
+add `L_SIGHANDLER` if you wish to log a message from a signal handler (see below
+for discussion on signals and reentrancy in general).
By default, all messages are logged to stderr. If you wish to use a log file,
call `log_file(@name)`. All subsequent logging will use this file and stderr
you must use the `L_SIGHANDLER` flag to request it. Otherwise, deadlocks get
ready to happen.
+Messages logged with `L_SIGHANDLER` set are written directly to stderr (which
+is usually an alias for the main log file, at least if you use <<log_file()>>)
+and they do not carry a timestamp. Logging of sighandler messages to general
+log streams or to syslog is therefore not supported.
+
ucw/log.h
---------
!!ucw/log.h
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);
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
{
/* 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);