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