]> mj.ucw.cz Git - libucw.git/blob - lib/log.c
introduced type bitarray_t
[libucw.git] / lib / log.c
1 /*
2  *      Sherlock Library -- Logging
3  *
4  *      (c) 1997--2002 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 <fcntl.h>
17 #include <unistd.h>
18 #include <time.h>
19 #include <alloca.h>
20
21 static char log_progname[32], *log_name_patt, *log_name;
22 static pid_t log_pid;
23 static int log_params;
24 static int log_name_size;
25 static int log_switching;
26
27 void
28 log_fork(void)
29 {
30   log_pid = getpid();
31 }
32
33 static void
34 log_switch(struct tm *tm)
35 {
36   int fd, l;
37   char name[log_name_size];
38
39   if (!log_name_patt ||
40       log_name[0] && !log_params ||
41       log_switching)
42     return;
43   log_switching++;
44   l = strftime(name, log_name_size, log_name_patt, tm);
45   if (l < 0 || l >= log_name_size)
46     die("Error formatting log file name: %m");
47   if (strcmp(name, log_name))
48     {
49       strcpy(log_name, name);
50       fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
51       if (fd < 0)
52         die("Unable to open log file %s: %m", name);
53       close(2);
54       dup(fd);
55       close(fd);
56     }
57   log_switching--;
58 }
59
60 static void
61 vlog(unsigned int cat, const char *msg, va_list args)
62 {
63   time_t tim = time(NULL);
64   struct tm *tm = localtime(&tim);
65   byte *buf, *p;
66   int buflen = 256;
67   int l, l0, r;
68
69   log_switch(tm);
70   while (1)
71     {
72       p = buf = alloca(buflen);
73       *p++ = cat;
74       p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S ", tm);
75       if (log_progname[0])
76         {
77           if (log_pid)
78             p += sprintf(p, "[%s (%d)] ", log_progname, log_pid);
79           else
80             p += sprintf(p, "[%s] ", log_progname);
81         }
82       else
83         {
84           if (log_pid)
85             p += sprintf(p, "[%d] ", log_pid);
86         }
87       l0 = p - buf + 1;
88       r = buflen - l0;
89       l = vsnprintf(p, r, msg, args);
90       if (l < 0)
91         l = r;
92       else if (l < r)
93         {
94           while (*p)
95             {
96               if (*p < 0x20 && *p != '\t')
97                 *p = 0x7f;
98               p++;
99             }
100           *p = '\n';
101           write(2, buf, l + l0);
102           return;
103         }
104       buflen = l + l0 + 1;
105     }
106 }
107
108 void
109 log_msg(unsigned int cat, const char *msg, ...)
110 {
111   va_list args;
112
113   va_start(args, msg);
114   vlog(cat, msg, args);
115   va_end(args);
116 }
117
118 void
119 die(byte *msg, ...)
120 {
121   va_list args;
122
123   va_start(args, msg);
124   vlog(L_FATAL, msg, args);
125   va_end(args);
126   exit(1);
127 }
128
129 #ifdef DEBUG
130 void
131 assert_failed(char *assertion, char *file, int line)
132 {
133   log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
134   abort();
135 }
136 #else
137 void
138 assert_failed(void)
139 {
140   die("Internal error: Assertion failed.");
141 }
142 #endif
143
144 static byte *
145 log_basename(byte *n)
146 {
147   byte *p = n;
148
149   while (*n)
150         if (*n++ == '/')
151           p = n;
152   return p;
153 }
154
155 void
156 log_init(byte *argv0)
157 {
158   if (argv0)
159     {
160       strncpy(log_progname, log_basename(argv0), sizeof(log_progname)-1);
161       log_progname[sizeof(log_progname)-1] = 0;
162     }
163 }
164
165 void
166 log_file(byte *name)
167 {
168   if (name)
169     {
170       time_t tim = time(NULL);
171       struct tm *tm = localtime(&tim);
172       if (log_name_patt)
173         xfree(log_name_patt);
174       if (log_name)
175         {
176           xfree(log_name);
177           log_name = NULL;
178         }
179       log_name_patt = stralloc(name);
180       log_params = !!strchr(name, '%');
181       log_name_size = strlen(name) + 64;        /* 63 is an upper bound on expansion of % escapes */
182       log_name = xmalloc(log_name_size);
183       log_name[0] = 0;
184       log_switch(tm);
185     }
186 }