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;
23 static int log_params;
24 static int log_name_size;
25 static int log_switching;
34 log_switch(struct tm *tm)
37 char name[log_name_size];
40 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);
61 vlog(unsigned int cat, const char *msg, va_list args)
63 time_t tim = time(NULL);
64 struct tm *tm = localtime(&tim);
72 p = buf = alloca(buflen);
74 p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
78 p += sprintf(p, "[%s (%d)] ", log_progname, log_pid);
80 p += sprintf(p, "[%s] ", log_progname);
85 p += sprintf(p, "[%d] ", log_pid);
89 l = vsnprintf(p, r, msg, args);
96 if (*p < 0x20 && *p != '\t')
101 write(2, buf, l + l0);
109 log_msg(unsigned int cat, const char *msg, ...)
114 vlog(cat, msg, args);
124 vlog(L_FATAL, msg, args);
131 assert_failed(char *assertion, char *file, int line)
133 log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
140 die("Internal error: Assertion failed.");
145 log_basename(byte *n)
156 log_init(byte *argv0)
160 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
161 log_progname[sizeof(log_progname)-1] = 0;
170 time_t tim = time(NULL);
171 struct tm *tm = localtime(&tim);
173 xfree(log_name_patt);
179 log_name_patt = stralloc(name);
180 log_params = !!strchr(name, '%');
181 log_name_size = strlen(name) + 64; /* 63 is an upper bound on expansion of % escapes */
182 log_name = xmalloc(log_name_size);