2 * UCW Library -- Keeping of Log Files
4 * (c) 1997--2006 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.
12 #include "lib/threads.h"
20 static char *log_name_patt;
21 static int log_params;
22 static int log_filename_size;
23 volatile int log_switch_nest;
26 do_log_switch(struct tm *tm)
29 char name[log_filename_size];
33 log_filename[0] && !log_params)
37 l = strftime(name, log_filename_size, log_name_patt, tm);
38 if (l < 0 || l >= log_filename_size)
39 die("Error formatting log file name: %m");
40 if (strcmp(name, log_filename))
42 strcpy(log_filename, name);
43 fd = sh_open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
45 die("Unable to open log file %s: %m", name);
61 time_t tim = time(NULL);
62 return do_log_switch(localtime(&tim));
66 internal_log_switch(struct tm *tm)
84 log_name_patt = xstrdup(name);
85 log_params = !!strchr(name, '%');
86 log_filename_size = strlen(name) + 64; /* 63 is an upper bound on expansion of % escapes */
87 log_filename = xmalloc(log_filename_size);
90 log_switch_hook = internal_log_switch;
102 int main(int argc, char **argv)
105 log_file("/proc/self/fd/1");
106 for (int i=1; i<argc; i++)
107 log(L_INFO, argv[i]);