]> mj.ucw.cz Git - libucw.git/blob - lib/log.c
Added a possibility of logging with microsecond timestamps.
[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       p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S", &tm);
49       if (log_precise_timings)
50         p += sprintf(p, ".%06d", (int)tv.tv_usec);
51       *p++ = ' ';
52       if (log_title)
53         {
54           if (log_pid)
55             p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
56           else
57             p += sprintf(p, "[%s] ", log_title);
58         }
59       else
60         {
61           if (log_pid)
62             p += sprintf(p, "[%d] ", log_pid);
63         }
64       l0 = p - buf + 1;
65       r = buflen - l0;
66       va_copy(args2, args);
67       l = vsnprintf(p, r, msg, args2);
68       va_end(args2);
69       if (l < 0)
70         l = r;
71       else if (l < r)
72         {
73           while (*p)
74             {
75               if (*p < 0x20 && *p != '\t')
76                 *p = 0x7f;
77               p++;
78             }
79           *p = '\n';
80           write(2, buf, l + l0);
81           return;
82         }
83       buflen = l + l0 + 1;
84     }
85 }
86
87 void
88 log_msg(unsigned int cat, const char *msg, ...)
89 {
90   va_list args;
91
92   va_start(args, msg);
93   vlog_msg(cat, msg, args);
94   va_end(args);
95 }
96
97 void
98 die(const char *msg, ...)
99 {
100   va_list args;
101
102   va_start(args, msg);
103   vlog_msg(L_FATAL, msg, args);
104   va_end(args);
105   if (log_die_hook)
106     log_die_hook();
107 #ifdef DEBUG_DIE_BY_ABORT
108   abort();
109 #else
110   exit(1);
111 #endif
112 }
113
114 void
115 assert_failed(char *assertion, char *file, int line)
116 {
117   log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
118   abort();
119 }
120
121 void
122 assert_failed_noinfo(void)
123 {
124   die("Internal error: Assertion failed.");
125 }
126
127 static byte *
128 log_basename(byte *n)
129 {
130   byte *p = n;
131
132   while (*n)
133     if (*n++ == '/')
134       p = n;
135   return p;
136 }
137
138 void
139 log_init(byte *argv0)
140 {
141   if (argv0)
142     {
143       strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
144       log_progname[sizeof(log_progname)-1] = 0;
145       log_title = log_progname;
146     }
147 }