]> mj.ucw.cz Git - libucw.git/blob - lib/log.c
Moved endianity settings etc. to the per-CPU section.
[libucw.git] / lib / log.c
1 /*
2  *      UCW 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 void
103 assert_failed(char *assertion, char *file, int line)
104 {
105   log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
106   abort();
107 }
108
109 void
110 assert_failed_noinfo(void)
111 {
112   die("Internal error: Assertion failed.");
113 }
114
115 static byte *
116 log_basename(byte *n)
117 {
118   byte *p = n;
119
120   while (*n)
121     if (*n++ == '/')
122       p = n;
123   return p;
124 }
125
126 void
127 log_init(byte *argv0)
128 {
129   if (argv0)
130     {
131       strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
132       log_progname[sizeof(log_progname)-1] = 0;
133       log_title = log_progname;
134     }
135 }