chiark / gitweb /
Merge from Mark's branch.
[disorder] / lib / log.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #define NO_MEMORY_ALLOCATION
22 /* because the memory allocation functions report errors */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <syslog.h>
31 #include <sys/time.h>
32
33 #include "log.h"
34 #include "disorder.h"
35 #include "printf.h"
36
37 struct log_output {
38   void (*fn)(int pri, const char *msg, void *user);
39   void *user;
40 };
41
42 void (*exitfn)(int) attribute((noreturn)) = exit;
43 int debugging;
44 const char *progname;
45 const char *debug_filename;
46 int debug_lineno;
47 struct log_output *log_default = &log_stderr;
48
49 static const char *debug_only;
50
51 /* we might be receiving things in any old encoding, or binary rubbish in no
52  * encoding at all, so escape anything we don't like the look of */
53 static void format(char buffer[], size_t bufsize, const char *fmt, va_list ap) {
54   char t[1024];
55   const char *p;
56   int ch;
57   size_t n = 0;
58   
59   if(byte_vsnprintf(t, sizeof t, fmt, ap) < 0)
60     strcpy(t, "[byte_vsnprintf failed]");
61   p = t;
62   while((ch = (unsigned char)*p++)) {
63     if(ch >= ' ' && ch <= 126) {
64       if(n < bufsize) buffer[n++] = ch;
65     } else {
66       if(n < bufsize) buffer[n++] = '\\';
67       if(n < bufsize) buffer[n++] = '0' + ((ch >> 6) & 7);
68       if(n < bufsize) buffer[n++] = '0' + ((ch >> 3) & 7);
69       if(n < bufsize) buffer[n++] = '0' + ((ch >> 0) & 7);
70     }
71   }
72   if(n >= bufsize)
73     n = bufsize - 1;
74   buffer[n] = 0;
75 }
76
77 /* log to a file */
78 static void logfp(int pri, const char *msg, void *user) {
79   struct timeval tv;
80   FILE *fp = user ? user : stderr;
81   /* ...because stderr is not a constant so we can't initialize log_stderr
82    * sanely */
83   const char *p;
84   
85   if(progname)
86     fprintf(fp, "%s: ", progname);
87   if(pri <= LOG_ERR)
88     fputs("ERROR: ", fp);
89   else if(pri < LOG_DEBUG)
90     fputs("INFO: ", fp);
91   else {
92     if(!debug_only) {
93       if(!(debug_only = getenv("DISORDER_DEBUG_ONLY")))
94         debug_only = "";
95     }
96     gettimeofday(&tv, 0);
97     p = debug_filename;
98     while(!strncmp(p, "../", 3)) p += 3;
99     if(*debug_only && strcmp(p, debug_only))
100       return;
101     fprintf(fp, "%llu.%06lu: %s:%d: ",
102             (unsigned long long)tv.tv_sec, (unsigned long)tv.tv_usec,
103             p, debug_lineno);
104   }
105   fputs(msg, fp);
106   fputc('\n', fp);
107 }
108
109 /* log to syslog */
110 static void logsyslog(int pri, const char *msg,
111                       void attribute((unused)) *user) {
112   if(pri < LOG_DEBUG)
113     syslog(pri, "%s", msg);
114   else
115     syslog(pri, "%s:%d: %s", debug_filename, debug_lineno, msg);
116 }
117
118 struct log_output log_stderr = { logfp, 0 };
119 struct log_output log_syslog = { logsyslog, 0 };
120
121 /* log to all log outputs */
122 static void vlogger(int pri, const char *fmt, va_list ap) {
123   char buffer[1024];
124
125   format(buffer, sizeof buffer, fmt, ap);
126   log_default->fn(pri, buffer, log_default->user);
127 }
128
129 /* wrapper for vlogger */
130 static void logger(int pri, const char *fmt, ...) {
131   va_list ap;
132
133   va_start(ap, fmt);
134   vlogger(pri, fmt, ap);
135   va_end(ap);
136 }
137
138 /* internals of fatal/error/info */
139 void elog(int pri, int errno_value, const char *fmt, va_list ap) {
140   char buffer[1024];
141
142   if(errno_value == 0)
143     vlogger(pri, fmt, ap);
144   else {
145     memset(buffer, 0, sizeof buffer);
146     byte_vsnprintf(buffer, sizeof buffer, fmt, ap);
147     buffer[sizeof buffer - 1] = 0;
148     logger(pri, "%s: %s", buffer, strerror(errno_value));
149   }
150 }
151
152 #define disorder_fatal fatal
153 #define disorder_error error
154 #define disorder_info info
155
156 /* shared implementation of vararg functions */
157 #include "log-impl.h"
158
159 void debug(const char *msg, ...) {
160   va_list ap;
161
162   va_start(ap, msg);
163   vlogger(LOG_DEBUG, msg, ap);
164   va_end(ap);
165 }
166
167 void set_progname(char **argv) {
168   if((progname = strrchr(argv[0], '/')))
169     ++progname;
170   else
171     progname = argv[0];
172 }
173
174 /*
175 Local Variables:
176 c-basic-offset:2
177 comment-column:40
178 End:
179 */