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.
20 static char *log_progname, *log_name_patt, *log_name;
22 static int log_params;
23 static int log_name_size;
24 static int log_switching;
33 log_switch(struct tm *tm)
36 char name[log_name_size];
39 log_name[0] && !log_params ||
43 l = strftime(name, log_name_size, log_name_patt, tm);
44 if (l < 0 || l >= log_name_size)
45 die("Error formatting log file name: %m");
46 if (strcmp(name, log_name))
48 strcpy(log_name, name);
49 fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
51 die("Unable to open log file %s: %m", name);
60 vlog(unsigned int cat, const char *msg, va_list args)
62 time_t tim = time(NULL);
63 struct tm *tm = localtime(&tim);
71 p = buf = alloca(buflen);
73 p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
77 p += sprintf(p, "[%s (%d)] ", log_progname, log_pid);
79 p += sprintf(p, "[%s] ", log_progname);
84 p += sprintf(p, "[%d] ", log_pid);
88 l = vsnprintf(p, r, msg, args);
95 if (*p < 0x20 && *p != '\t')
100 write(2, buf, l + l0);
108 log(unsigned int cat, const char *msg, ...)
113 vlog(cat, msg, args);
123 vlog(L_FATAL, msg, args);
130 assert_failed(char *assertion, char *file, int line)
132 log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
139 die("Internal error: Assertion failed.");
144 log_basename(byte *n)
155 log_init(byte *argv0)
158 log_progname = log_basename(argv0);
166 time_t tim = time(NULL);
167 struct tm *tm = localtime(&tim);
169 xfree(log_name_patt);
175 log_name_patt = stralloc(name);
176 log_params = !!strchr(name, '%');
177 log_name_size = strlen(name) + 64; /* 63 is an upper bound on expansion of % escapes */
178 log_name = xmalloc(log_name_size);