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;
25 static int log_params;
26 static int log_filename_size;
28 void (*log_die_hook)(void);
37 do_log_switch(struct tm *tm)
40 char name[log_filename_size];
43 log_filename[0] && !log_params)
46 l = strftime(name, log_filename_size, log_name_patt, tm);
47 if (l < 0 || l >= log_filename_size)
48 die("Error formatting log file name: %m");
49 if (strcmp(name, log_filename))
51 strcpy(log_filename, name);
52 fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
54 die("Unable to open log file %s: %m", name);
67 time_t tim = time(NULL);
68 do_log_switch(localtime(&tim));
72 internal_log_switch(struct tm *tm)
79 vlog_msg(unsigned int cat, const char *msg, va_list args)
81 time_t tim = time(NULL);
82 struct tm *tm = localtime(&tim);
87 internal_log_switch(tm);
90 p = buf = alloca(buflen);
92 p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
96 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
98 p += sprintf(p, "[%s] ", log_title);
103 p += sprintf(p, "[%d] ", log_pid);
107 l = vsnprintf(p, r, msg, args);
114 if (*p < 0x20 && *p != '\t')
119 write(2, buf, l + l0);
127 log_msg(unsigned int cat, const char *msg, ...)
132 vlog_msg(cat, msg, args);
142 vlog_msg(L_FATAL, msg, args);
146 #ifdef DEBUG_DIE_BY_ABORT
155 assert_failed(char *assertion, char *file, int line)
157 log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
164 die("Internal error: Assertion failed.");
169 log_basename(byte *n)
180 log_init(byte *argv0)
184 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
185 log_progname[sizeof(log_progname)-1] = 0;
186 log_title = log_progname;
196 xfree(log_name_patt);
202 log_name_patt = xstrdup(name);
203 log_params = !!strchr(name, '%');
204 log_filename_size = strlen(name) + 64; /* 63 is an upper bound on expansion of % escapes */
205 log_filename = xmalloc(log_filename_size);
209 open("/dev/null", O_RDWR, 0);