4 * Copyright (c) 2008 James McKenzie <sympathy@madingley.org>,
9 static char rcsid[] = "$Id: log.c,v 1.17 2011/02/04 16:14:16 james Exp $";
13 * Revision 1.17 2011/02/04 16:14:16 james
14 * *** empty log message ***
16 * Revision 1.16 2010/07/27 14:49:35 james
17 * add support for byte logging
19 * Revision 1.15 2010/07/16 11:04:10 james
20 * ignore tedious return values
22 * Revision 1.14 2008/03/11 17:56:04 james
23 * *** empty log message ***
25 * Revision 1.13 2008/03/11 16:56:29 james
26 * *** empty log message ***
28 * Revision 1.12 2008/03/10 11:49:33 james
29 * *** empty log message ***
31 * Revision 1.11 2008/03/07 13:16:02 james
32 * *** empty log message ***
34 * Revision 1.10 2008/03/07 12:37:04 james
35 * *** empty log message ***
37 * Revision 1.9 2008/03/03 06:20:14 james
38 * *** empty log message ***
40 * Revision 1.8 2008/03/03 06:04:42 james
41 * *** empty log message ***
43 * Revision 1.7 2008/03/03 06:04:18 james
44 * *** empty log message ***
46 * Revision 1.6 2008/03/02 10:37:56 james
47 * *** empty log message ***
49 * Revision 1.5 2008/02/27 01:31:14 james
50 * *** empty log message ***
52 * Revision 1.4 2008/02/27 00:54:16 james
53 * *** empty log message ***
55 * Revision 1.3 2008/02/23 11:48:37 james
56 * *** empty log message ***
58 * Revision 1.2 2008/02/22 14:51:54 james
59 * *** empty log message ***
61 * Revision 1.1 2008/02/14 12:14:50 james
62 * *** empty log message ***
78 static Log *loggers = NULL;
86 for (l = loggers; l; l = l->next) {
94 log_register_handlers (void)
96 struct sigaction sa = { 0 };
98 sa.sa_handler = sighup;
99 sa.sa_flags = SA_RESTART;
100 sigaction (SIGHUP, &sa, NULL);
107 log_register_handlers ();
116 Log **ptr = &loggers;
118 /* Take out of sighup list */
119 while (*ptr && (*ptr != l))
120 ptr = &((*ptr)->next);
126 static void flog_newline(Log *_l,int force)
128 File_Log *l = (File_Log *) _l;
130 if (force || !l->needs_newline) return;
140 flog_sighup (Log * _l)
142 File_Log *l = (File_Log *) _l;
146 log_f (_l, "<sighup received - closing log file>");
149 l->fp = fopen (l->filename, "a+");
150 log_f (_l, "<sighup received - opening log file>");
154 flog_emit_stamp(Log *_l)
156 File_Log *l = (File_Log *) _l;
157 struct timeval tv = { 0 };
160 static const char *days[] = { "Sun", "Mon", "Tue",
161 "Wed", "Thu", "Fri", "Sat"
163 static const char *months[] = {
164 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
173 gettimeofday (&tv, NULL);
177 fprintf (l->fp, "%s %2d %02d:%02d:%02d.%06d ", months[tm->tm_mon],
178 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,(int) tv.tv_usec);
182 static void flog_check_rotate(Log *_l)
184 File_Log *l = (File_Log *) _l;
186 if (l->rotate && rotate_check (l->filename)) {
188 rotate (l->filename);
189 l->fp = fopen (l->filename, "a+");
195 flog_log_bytes (Log * _l, void *_buf,int len)
197 File_Log *l = (File_Log *) _l;
198 uint8_t *buf=(uint8_t *) _buf;
206 flog_check_rotate(_l);
217 flog_log (Log * _l, char *buf)
219 File_Log *l = (File_Log *) _l;
230 flog_check_rotate(_l);
237 flog_close (Log * _l)
239 File_Log *l = (File_Log *) _l;
240 if (l->fp && l->do_close)
248 file_log_new (char *fn, int rotate)
253 l = xmalloc (sizeof (File_Log));
255 if (fn && strcmp (fn, "-")) {
256 l->fp = fopen (fn, "a+");
261 l->sighup = flog_sighup;
269 l->log_bytes = flog_log_bytes;
270 l->close = flog_close;
273 l->filename = strdup (fn);
277 fput_cp (l->fp, 0xffef);
285 log_f (Log * log, char *fmt, ...)
306 n = vsnprintf (buf, size, fmt, ap);
309 if (n > -1 && n < size) {
314 if (n > -1) /* glibc 2.1 */
317 size *= 2; /* twice the old size */
319 buf = xrealloc (buf, size);