2 * Sherlock Library -- Logging
4 * (c) 1997--2002 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.
21 static char log_progname[32], *log_name_patt, *log_name;
24 static int log_params;
25 static int log_name_size;
35 do_log_switch(struct tm *tm)
38 char name[log_name_size];
41 log_name[0] && !log_params)
44 l = strftime(name, log_name_size, log_name_patt, tm);
45 if (l < 0 || l >= log_name_size)
46 die("Error formatting log file name: %m");
47 if (strcmp(name, log_name))
49 strcpy(log_name, name);
50 fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
52 die("Unable to open log file %s: %m", name);
65 time_t tim = time(NULL);
66 do_log_switch(localtime(&tim));
70 internal_log_switch(struct tm *tm)
77 vlog_msg(unsigned int cat, const char *msg, va_list args)
79 time_t tim = time(NULL);
80 struct tm *tm = localtime(&tim);
85 internal_log_switch(tm);
88 p = buf = alloca(buflen);
90 p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
94 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
96 p += sprintf(p, "[%s] ", log_title);
101 p += sprintf(p, "[%d] ", log_pid);
105 l = vsnprintf(p, r, msg, args);
112 if (*p < 0x20 && *p != '\t')
117 write(2, buf, l + l0);
125 log_msg(unsigned int cat, const char *msg, ...)
130 vlog_msg(cat, msg, args);
140 vlog_msg(L_FATAL, msg, args);
142 #ifdef DEBUG_DIE_BY_ABORT
151 assert_failed(char *assertion, char *file, int line)
153 log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
160 die("Internal error: Assertion failed.");
165 log_basename(byte *n)
176 log_init(byte *argv0)
180 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
181 log_progname[sizeof(log_progname)-1] = 0;
182 log_title = log_progname;
192 xfree(log_name_patt);
198 log_name_patt = xstrdup(name);
199 log_params = !!strchr(name, '%');
200 log_name_size = strlen(name) + 64; /* 63 is an upper bound on expansion of % escapes */
201 log_name = xmalloc(log_name_size);
205 open("/dev/null", O_RDWR, 0);