]> mj.ucw.cz Git - libucw.git/blob - lib/log.c
4d91b315bec14b43f40ae8d903bb3aecbc96efcc
[libucw.git] / lib / log.c
1 /*
2  *      UCW Library -- Logging
3  *
4  *      (c) 1997--2006 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 <string.h>
15 #include <unistd.h>
16 #include <sys/time.h>
17 #include <time.h>
18 #include <alloca.h>
19
20 static char log_progname[32];
21 char *log_filename;
22 char *log_title;
23 int log_pid;
24 int log_precise_timings;
25 void (*log_die_hook)(void);
26 void (*log_switch_hook)(struct tm *tm);
27
28 void
29 vmsg(unsigned int cat, const char *fmt, va_list args)
30 {
31   struct timeval tv;
32   struct tm tm;
33   byte *buf, *p;
34   int buflen = 256;
35   int l, l0, r;
36   va_list args2;
37
38   gettimeofday(&tv, NULL);
39   if (!localtime_r(&tv.tv_sec, &tm))
40     bzero(&tm, sizeof(tm));
41
42   if (log_switch_hook)
43     log_switch_hook(&tm);
44   while (1)
45     {
46       p = buf = alloca(buflen);
47       *p++ = cat;
48       /* We cannot use strftime() here, because it's not re-entrant */
49       p += sprintf(p, " %4d-%02d-%02d %02d:%02d:%02d", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
50                    tm.tm_hour, tm.tm_min, tm.tm_sec);
51       if (log_precise_timings)
52         p += sprintf(p, ".%06d", (int)tv.tv_usec);
53       *p++ = ' ';
54       if (log_title)
55         {
56           if (log_pid)
57             p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
58           else
59             p += sprintf(p, "[%s] ", log_title);
60         }
61       else
62         {
63           if (log_pid)
64             p += sprintf(p, "[%d] ", log_pid);
65         }
66       l0 = p - buf + 1;
67       r = buflen - l0;
68       va_copy(args2, args);
69       l = vsnprintf(p, r, fmt, args2);
70       va_end(args2);
71       if (l < 0)
72         l = r;
73       else if (l < r)
74         {
75           while (*p)
76             {
77               if (*p < 0x20 && *p != '\t')
78                 *p = 0x7f;
79               p++;
80             }
81           *p = '\n';
82           write(2, buf, l + l0);
83           return;
84         }
85       buflen = l + l0 + 1;
86     }
87 }
88
89 void
90 msg(unsigned int cat, const char *fmt, ...)
91 {
92   va_list args;
93
94   va_start(args, fmt);
95   vmsg(cat, fmt, args);
96   va_end(args);
97 }
98
99 void
100 safe_vmsg(unsigned int cat, const char *fmt, va_list args)
101 {
102   byte *buf, *p;
103   int buflen = 256;
104   int l, l0, r;
105   va_list args2;
106
107   while (1)
108     {
109       p = buf = alloca(buflen);
110       *p++ = cat;
111       p += sprintf(p, " \?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?");
112       if (log_precise_timings)
113         p += sprintf(p, ".\?\?\?\?\?\?");
114       *p++ = ' ';
115       if (log_title)
116         {
117           if (log_pid)
118             p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
119           else
120             p += sprintf(p, "[%s] ", log_title);
121         }
122       else
123         {
124           if (log_pid)
125             p += sprintf(p, "[%d] ", log_pid);
126         }
127       l0 = p - buf + 1;
128       r = buflen - l0;
129       va_copy(args2, args);
130       l = vsnprintf(p, r, fmt, args2);
131       va_end(args2);
132       if (l < 0)
133         l = r;
134       else if (l < r)
135         {
136           while (*p)
137             {
138               if (*p < 0x20 && *p != '\t')
139                 *p = 0x7f;
140               p++;
141             }
142           *p = '\n';
143           write(2, buf, l + l0);
144           return;
145         }
146       buflen = l + l0 + 1;
147     }
148 }
149
150 void
151 safe_msg(unsigned int cat, const char *fmt, ...)
152 {
153   va_list args;
154
155   va_start(args, fmt);
156   safe_vmsg(cat, fmt, args);
157   va_end(args);
158 }
159
160 void
161 die(const char *fmt, ...)
162 {
163   va_list args;
164
165   va_start(args, fmt);
166   vmsg(L_FATAL, fmt, args);
167   va_end(args);
168   if (log_die_hook)
169     log_die_hook();
170 #ifdef DEBUG_DIE_BY_ABORT
171   abort();
172 #else
173   exit(1);
174 #endif
175 }
176
177 void
178 assert_failed(const char *assertion, const char *file, int line)
179 {
180   msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
181   abort();
182 }
183
184 void
185 assert_failed_noinfo(void)
186 {
187   die("Internal error: Assertion failed.");
188 }
189
190 static const char *
191 log_basename(const char *n)
192 {
193   const char *p = n;
194
195   while (*n)
196     if (*n++ == '/')
197       p = n;
198   return p;
199 }
200
201 void
202 log_init(const char *argv0)
203 {
204   if (argv0)
205     {
206       strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
207       log_progname[sizeof(log_progname)-1] = 0;
208       log_title = log_progname;
209     }
210 }