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;
26 static int log_switching;
35 log_switch(struct tm *tm)
38 char name[log_name_size];
41 log_name[0] && !log_params ||
45 l = strftime(name, log_name_size, log_name_patt, tm);
46 if (l < 0 || l >= log_name_size)
47 die("Error formatting log file name: %m");
48 if (strcmp(name, log_name))
50 strcpy(log_name, name);
51 fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
53 die("Unable to open log file %s: %m", name);
62 vlog_msg(unsigned int cat, const char *msg, va_list args)
64 time_t tim = time(NULL);
65 struct tm *tm = localtime(&tim);
73 p = buf = alloca(buflen);
75 p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
79 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
81 p += sprintf(p, "[%s] ", log_title);
86 p += sprintf(p, "[%d] ", log_pid);
90 l = vsnprintf(p, r, msg, args);
97 if (*p < 0x20 && *p != '\t')
102 write(2, buf, l + l0);
110 log_msg(unsigned int cat, const char *msg, ...)
115 vlog_msg(cat, msg, args);
125 vlog_msg(L_FATAL, msg, args);
127 #ifdef DEBUG_DIE_BY_ABORT
136 assert_failed(char *assertion, char *file, int line)
138 log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
145 die("Internal error: Assertion failed.");
150 log_basename(byte *n)
161 log_init(byte *argv0)
165 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
166 log_progname[sizeof(log_progname)-1] = 0;
167 log_title = log_progname;
176 time_t tim = time(NULL);
177 struct tm *tm = localtime(&tim);
179 xfree(log_name_patt);
185 log_name_patt = stralloc(name);
186 log_params = !!strchr(name, '%');
187 log_name_size = strlen(name) + 64; /* 63 is an upper bound on expansion of % escapes */
188 log_name = xmalloc(log_name_size);