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);
64 vlog_msg(unsigned int cat, const char *msg, va_list args)
66 time_t tim = time(NULL);
67 struct tm *tm = localtime(&tim);
75 p = buf = alloca(buflen);
77 p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
81 p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
83 p += sprintf(p, "[%s] ", log_title);
88 p += sprintf(p, "[%d] ", log_pid);
92 l = vsnprintf(p, r, msg, args);
99 if (*p < 0x20 && *p != '\t')
104 write(2, buf, l + l0);
112 log_msg(unsigned int cat, const char *msg, ...)
117 vlog_msg(cat, msg, args);
127 vlog_msg(L_FATAL, msg, args);
129 #ifdef DEBUG_DIE_BY_ABORT
138 assert_failed(char *assertion, char *file, int line)
140 log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
147 die("Internal error: Assertion failed.");
152 log_basename(byte *n)
163 log_init(byte *argv0)
167 strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
168 log_progname[sizeof(log_progname)-1] = 0;
169 log_title = log_progname;
178 time_t tim = time(NULL);
179 struct tm *tm = localtime(&tim);
181 xfree(log_name_patt);
187 log_name_patt = stralloc(name);
188 log_params = !!strchr(name, '%');
189 log_name_size = strlen(name) + 64; /* 63 is an upper bound on expansion of % escapes */
190 log_name = xmalloc(log_name_size);
194 open("/dev/null", O_RDWR, 0);