2 * UCW Library -- Logging
4 * (c) 1997--2008 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
20 static char log_progname[32];
24 int log_precise_timings;
25 void (*log_die_hook)(void);
26 void (*log_switch_hook)(struct tm *tm);
29 vmsg(unsigned int cat, const char *fmt, va_list args)
39 if (!(cat & L_SIGHANDLER))
41 /* CAVEAT: These calls are not safe in signal handlers. */
42 gettimeofday(&tv, NULL);
43 if (localtime_r(&tv.tv_sec, &tm))
51 p = buf = alloca(buflen);
55 p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S", &tm);
56 if (log_precise_timings)
57 p += sprintf(p, ".%06d", (int)tv.tv_usec);
61 p += sprintf(p, " \?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?");
62 if (log_precise_timings)
63 p += sprintf(p, ".\?\?\?\?\?\?");
69 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
71 p += sprintf(p, "[%s] ", log_title);
76 p += sprintf(p, "[%d] ", log_pid);
81 l = vsnprintf(p, r, fmt, args2);
89 if (*p < 0x20 && *p != '\t')
94 write(2, buf, l + l0);
102 msg(unsigned int cat, const char *fmt, ...)
107 vmsg(cat, fmt, args);
112 die(const char *fmt, ...)
117 vmsg(L_FATAL, fmt, args);
121 #ifdef DEBUG_DIE_BY_ABORT
129 assert_failed(const char *assertion, const char *file, int line)
131 msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
136 assert_failed_noinfo(void)
138 die("Internal error: Assertion failed.");
142 log_basename(const char *n)
153 log_init(const char *argv0)
157 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
158 log_progname[sizeof(log_progname)-1] = 0;
159 log_title = log_progname;