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