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