]> mj.ucw.cz Git - libucw.git/blob - lib/log.c
Another debugging switch: dump core on fatal errors.
[libucw.git] / lib / log.c
1 /*
2  *      Sherlock Library -- Logging
3  *
4  *      (c) 1997--2002 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <time.h>
19 #include <alloca.h>
20
21 static char log_progname[32], *log_name_patt, *log_name;
22 static pid_t log_pid;
23 static int log_params;
24 static int log_name_size;
25 static int log_switching;
26
27 void
28 log_fork(void)
29 {
30   log_pid = getpid();
31 }
32
33 static void
34 log_switch(struct tm *tm)
35 {
36   int fd, l;
37   char name[log_name_size];
38
39   if (!log_name_patt ||
40       log_name[0] && !log_params ||
41       log_switching)
42     return;
43   log_switching++;
44   l = strftime(name, log_name_size, log_name_patt, tm);
45   if (l < 0 || l >= log_name_size)
46     die("Error formatting log file name: %m");
47   if (strcmp(name, log_name))
48     {
49       strcpy(log_name, name);
50       fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
51       if (fd < 0)
52         die("Unable to open log file %s: %m", name);
53       close(2);
54       dup(fd);
55       close(fd);
56     }
57   log_switching--;
58 }
59
60 static void
61 vlog(unsigned int cat, const char *msg, va_list args)
62 {
63   time_t tim = time(NULL);
64   struct tm *tm = localtime(&tim);
65   byte *buf, *p;
66   int buflen = 256;
67   int l, l0, r;
68
69   log_switch(tm);
70   while (1)
71     {
72       p = buf = alloca(buflen);
73       *p++ = cat;
74       p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
75       if (log_progname[0])
76         {
77           if (log_pid)
78             p += sprintf(p, "[%s (%d)] ", log_progname, log_pid);
79           else
80             p += sprintf(p, "[%s] ", log_progname);
81         }
82       else
83         {
84           if (log_pid)
85             p += sprintf(p, "[%d] ", log_pid);
86         }
87       l0 = p - buf + 1;
88       r = buflen - l0;
89       l = vsnprintf(p, r, msg, args);
90       if (l < 0)
91         l = r;
92       else if (l < r)
93         {
94           while (*p)
95             {
96               if (*p < 0x20 && *p != '\t')
97                 *p = 0x7f;
98               p++;
99             }
100           *p = '\n';
101           write(2, buf, l + l0);
102           return;
103         }
104       buflen = l + l0 + 1;
105     }
106 }
107
108 void
109 log_msg(unsigned int cat, const char *msg, ...)
110 {
111   va_list args;
112
113   va_start(args, msg);
114   vlog(cat, msg, args);
115   va_end(args);
116 }
117
118 void
119 die(byte *msg, ...)
120 {
121   va_list args;
122
123   va_start(args, msg);
124   vlog(L_FATAL, msg, args);
125   va_end(args);
126 #ifdef DEBUG_DIE_BY_ABORT
127   abort();
128 #else
129   exit(1);
130 #endif
131 }
132
133 #ifdef DEBUG
134 void
135 assert_failed(char *assertion, char *file, int line)
136 {
137   log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
138   abort();
139 }
140 #else
141 void
142 assert_failed(void)
143 {
144   die("Internal error: Assertion failed.");
145 }
146 #endif
147
148 static byte *
149 log_basename(byte *n)
150 {
151   byte *p = n;
152
153   while (*n)
154         if (*n++ == '/')
155           p = n;
156   return p;
157 }
158
159 void
160 log_init(byte *argv0)
161 {
162   if (argv0)
163     {
164       strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
165       log_progname[sizeof(log_progname)-1] = 0;
166     }
167 }
168
169 void
170 log_file(byte *name)
171 {
172   if (name)
173     {
174       time_t tim = time(NULL);
175       struct tm *tm = localtime(&tim);
176       if (log_name_patt)
177         xfree(log_name_patt);
178       if (log_name)
179         {
180           xfree(log_name);
181           log_name = NULL;
182         }
183       log_name_patt = stralloc(name);
184       log_params = !!strchr(name, '%');
185       log_name_size = strlen(name) + 64;        /* 63 is an upper bound on expansion of % escapes */
186       log_name = xmalloc(log_name_size);
187       log_name[0] = 0;
188       log_switch(tm);
189     }
190 }