]> mj.ucw.cz Git - libucw.git/blob - ucw/log.c
Simplified 'unsigned char' -> 'byte'.
[libucw.git] / ucw / log.c
1 /*
2  *      UCW Library -- Logging
3  *
4  *      (c) 1997--2008 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 "ucw/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   int have_tm = 0;
33   struct tm tm;
34   byte *buf, *p;
35   int buflen = 256;
36   int l, l0, r;
37   va_list args2;
38
39   if (!(cat & L_SIGHANDLER))
40     {
41       /* CAVEAT: These calls are not safe in signal handlers. */
42       gettimeofday(&tv, NULL);
43       if (localtime_r(&tv.tv_sec, &tm))
44         have_tm = 1;
45       if (log_switch_hook)
46         log_switch_hook(&tm);
47     }
48
49   while (1)
50     {
51       p = buf = alloca(buflen);
52       *p++ = cat & 0xff;
53       if (have_tm)
54         {
55           p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S", &tm);
56           if (log_precise_timings)
57             p += sprintf(p, ".%06d", (int)tv.tv_usec);
58         }
59       else
60         {
61           p += sprintf(p, " \?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?");
62           if (log_precise_timings)
63             p += sprintf(p, ".\?\?\?\?\?\?");
64         }
65       *p++ = ' ';
66       if (log_title)
67         {
68           if (log_pid)
69             p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
70           else
71             p += sprintf(p, "[%s] ", log_title);
72         }
73       else
74         {
75           if (log_pid)
76             p += sprintf(p, "[%d] ", log_pid);
77         }
78       l0 = p - buf + 1;
79       r = buflen - l0;
80       va_copy(args2, args);
81       l = vsnprintf(p, r, fmt, args2);
82       va_end(args2);
83       if (l < 0)
84         l = r;
85       else if (l < r)
86         {
87           while (*p)
88             {
89               if (*p < 0x20 && *p != '\t')
90                 *p = 0x7f;
91               p++;
92             }
93           *p = '\n';
94           write(2, buf, l + l0);
95           return;
96         }
97       buflen = l + l0 + 1;
98     }
99 }
100
101 void
102 msg(unsigned int cat, const char *fmt, ...)
103 {
104   va_list args;
105
106   va_start(args, fmt);
107   vmsg(cat, fmt, args);
108   va_end(args);
109 }
110
111 void
112 die(const char *fmt, ...)
113 {
114   va_list args;
115
116   va_start(args, fmt);
117   vmsg(L_FATAL, fmt, args);
118   va_end(args);
119   if (log_die_hook)
120     log_die_hook();
121 #ifdef DEBUG_DIE_BY_ABORT
122   abort();
123 #else
124   exit(1);
125 #endif
126 }
127
128 void
129 assert_failed(const char *assertion, const char *file, int line)
130 {
131   msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
132   abort();
133 }
134
135 void
136 assert_failed_noinfo(void)
137 {
138   die("Internal error: Assertion failed.");
139 }
140
141 static const char *
142 log_basename(const char *n)
143 {
144   const char *p = n;
145
146   while (*n)
147     if (*n++ == '/')
148       p = n;
149   return p;
150 }
151
152 void
153 log_init(const char *argv0)
154 {
155   if (argv0)
156     {
157       strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
158       log_progname[sizeof(log_progname)-1] = 0;
159       log_title = log_progname;
160     }
161 }