]> mj.ucw.cz Git - libucw.git/blob - lib/log.c
0c91bcd15ec109fb49e4631647eb2cafdef99850
[libucw.git] / lib / log.c
1 /*
2  *      Sherlock Library -- Logging
3  *
4  *      (c) 1997--2004 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 <stdarg.h>
15 #include <string.h>
16 #include <unistd.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 void (*log_die_hook)(void);
25 void (*log_switch_hook)(struct tm *tm);
26
27 void
28 vlog_msg(unsigned int cat, const char *msg, va_list args)
29 {
30   time_t tim = time(NULL);
31   struct tm *tm = localtime(&tim);
32   byte *buf, *p;
33   int buflen = 256;
34   int l, l0, r;
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       l = vsnprintf(p, r, msg, args);
58       if (l < 0)
59         l = r;
60       else if (l < r)
61         {
62           while (*p)
63             {
64               if (*p < 0x20 && *p != '\t')
65                 *p = 0x7f;
66               p++;
67             }
68           *p = '\n';
69           write(2, buf, l + l0);
70           return;
71         }
72       buflen = l + l0 + 1;
73     }
74 }
75
76 void
77 log_msg(unsigned int cat, const char *msg, ...)
78 {
79   va_list args;
80
81   va_start(args, msg);
82   vlog_msg(cat, msg, args);
83   va_end(args);
84 }
85
86 void
87 die(byte *msg, ...)
88 {
89   va_list args;
90
91   va_start(args, msg);
92   vlog_msg(L_FATAL, msg, args);
93   va_end(args);
94   if (log_die_hook)
95     log_die_hook();
96 #ifdef DEBUG_DIE_BY_ABORT
97   abort();
98 #else
99   exit(1);
100 #endif
101 }
102
103 #ifdef DEBUG
104 void
105 assert_failed(char *assertion, char *file, int line)
106 {
107   log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
108   abort();
109 }
110 #else
111 void
112 assert_failed(void)
113 {
114   die("Internal error: Assertion failed.");
115 }
116 #endif
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 }