]> mj.ucw.cz Git - libucw.git/blob - lib/log.c
simplified usage of Log & Die
[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 vlog_msg(unsigned int cat, const char *msg, 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, msg, 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 log_msg(unsigned int cat, const char *msg, ...)
91 {
92   va_list args;
93
94   va_start(args, msg);
95   vlog_msg(cat, msg, args);
96   va_end(args);
97 }
98
99 void
100 die(const char *msg, ...)
101 {
102   va_list args;
103
104   va_start(args, msg);
105   vlog_msg(L_FATAL, msg, args);
106   va_end(args);
107   if (log_die_hook)
108     log_die_hook();
109 #ifdef DEBUG_DIE_BY_ABORT
110   abort();
111 #else
112   exit(1);
113 #endif
114 }
115
116 void
117 assert_failed(char *assertion, char *file, int line)
118 {
119   log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
120   abort();
121 }
122
123 void
124 assert_failed_noinfo(void)
125 {
126   die("Internal error: Assertion failed.");
127 }
128
129 static byte *
130 log_basename(byte *n)
131 {
132   byte *p = n;
133
134   while (*n)
135     if (*n++ == '/')
136       p = n;
137   return p;
138 }
139
140 void
141 log_init(byte *argv0)
142 {
143   if (argv0)
144     {
145       strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
146       log_progname[sizeof(log_progname)-1] = 0;
147       log_title = log_progname;
148     }
149 }