]> mj.ucw.cz Git - libucw.git/blob - lib/log.c
log() is now signal-safe.
[libucw.git] / lib / log.c
1 /*
2  *      UCW Library -- Logging
3  *
4  *      (c) 1997--2005 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 <time.h>
17 #include <alloca.h>
18
19 static char log_progname[32];
20 char *log_filename;
21 char *log_title;
22 int log_pid;
23 void (*log_die_hook)(void);
24 void (*log_switch_hook)(struct tm *tm);
25
26 void
27 vlog_msg(unsigned int cat, const char *msg, va_list args)
28 {
29   time_t tim = time(NULL);
30   struct tm tm;
31   byte *buf, *p;
32   int buflen = 256;
33   int l, l0, r;
34   va_list args2;
35
36   if (!localtime_r(&tim, &tm))
37     bzero(&tm, sizeof(tm));
38
39   if (log_switch_hook)
40     log_switch_hook(&tm);
41   while (1)
42     {
43       p = buf = alloca(buflen);
44       *p++ = cat;
45       p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", &tm);
46       if (log_title)
47         {
48           if (log_pid)
49             p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
50           else
51             p += sprintf(p, "[%s] ", log_title);
52         }
53       else
54         {
55           if (log_pid)
56             p += sprintf(p, "[%d] ", log_pid);
57         }
58       l0 = p - buf + 1;
59       r = buflen - l0;
60       va_copy(args2, args);
61       l = vsnprintf(p, r, msg, args2);
62       va_end(args2);
63       if (l < 0)
64         l = r;
65       else if (l < r)
66         {
67           while (*p)
68             {
69               if (*p < 0x20 && *p != '\t')
70                 *p = 0x7f;
71               p++;
72             }
73           *p = '\n';
74           write(2, buf, l + l0);
75           return;
76         }
77       buflen = l + l0 + 1;
78     }
79 }
80
81 void
82 log_msg(unsigned int cat, const char *msg, ...)
83 {
84   va_list args;
85
86   va_start(args, msg);
87   vlog_msg(cat, msg, args);
88   va_end(args);
89 }
90
91 void
92 die(const char *msg, ...)
93 {
94   va_list args;
95
96   va_start(args, msg);
97   vlog_msg(L_FATAL, msg, args);
98   va_end(args);
99   if (log_die_hook)
100     log_die_hook();
101 #ifdef DEBUG_DIE_BY_ABORT
102   abort();
103 #else
104   exit(1);
105 #endif
106 }
107
108 void
109 assert_failed(char *assertion, char *file, int line)
110 {
111   log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
112   abort();
113 }
114
115 void
116 assert_failed_noinfo(void)
117 {
118   die("Internal error: Assertion failed.");
119 }
120
121 static byte *
122 log_basename(byte *n)
123 {
124   byte *p = n;
125
126   while (*n)
127     if (*n++ == '/')
128       p = n;
129   return p;
130 }
131
132 void
133 log_init(byte *argv0)
134 {
135   if (argv0)
136     {
137       strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
138       log_progname[sizeof(log_progname)-1] = 0;
139       log_title = log_progname;
140     }
141 }