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