chiark / gitweb /
not 4.3 any more
[disorder] / lib / log.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004-2008 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 3 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,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU 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, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file lib/log.c @brief Errors and logging
19  *
20  * All messages are initially emitted by one of the four functions below.
21  * debug() is generally invoked via D() so that mostly you just do a test
22  * rather than a complete subroutine call.
23  *
24  * Messages are dispatched via @ref log_default.  This defaults to @ref
25  * log_stderr.  daemonize() will turn off @ref log_stderr and use @ref
26  * log_syslog instead.
27  *
28  * fatal() will call exitfn() with a nonzero status.  The default value is
29  * exit(), but it should be set to _exit() anywhere but the 'main line' of the
30  * program, to guarantee that exit() gets called at most once.
31  */
32
33 #define NO_MEMORY_ALLOCATION
34 /* because the memory allocation functions report errors */
35
36 #include "common.h"
37
38 #include <errno.h>
39 #include <syslog.h>
40 #include <sys/time.h>
41
42 #include "log.h"
43 #include "disorder.h"
44 #include "printf.h"
45
46 /** @brief Definition of a log output */
47 struct log_output {
48   /** @brief Function to call */
49   void (*fn)(int pri, const char *msg, void *user);
50   /** @brief User data */
51   void *user;
52 };
53
54 /** @brief Function to call on a fatal error
55  *
56  * This is normally @c exit() but in the presence of @c fork() it
57  * sometimes gets set to @c _exit(). */
58 void (*exitfn)(int) attribute((noreturn)) = exit;
59
60 /** @brief Debug flag */
61 int debugging;
62
63 /** @brief Program name */
64 const char *progname;
65
66 /** @brief Filename for debug messages */
67 const char *debug_filename;
68
69 /** @brief Line number for debug messages */
70 int debug_lineno;
71
72 /** @brief Pointer to chosen log output structure */
73 struct log_output *log_default = &log_stderr;
74
75 /** @brief Filename to debug for */
76 static const char *debug_only;
77
78 /** @brief Construct log line, encoding special characters
79  *
80  * We might be receiving things in any old encoding, or binary rubbish
81  * in no encoding at all, so escape anything we don't like the look
82  * of.  We limit the log message to a kilobyte.
83  */
84 static void format(char buffer[], size_t bufsize, const char *fmt, va_list ap) {
85   char t[1024];
86   const char *p;
87   int ch;
88   size_t n = 0;
89   
90   if(byte_vsnprintf(t, sizeof t, fmt, ap) < 0) {
91     strcpy(t, "[byte_vsnprintf failed: ");
92     strncat(t, fmt, sizeof t - strlen(t) - 1);
93   }
94   p = t;
95   while((ch = (unsigned char)*p++)) {
96     if(ch >= ' ' && ch <= 126) {
97       if(n < bufsize) buffer[n++] = ch;
98     } else {
99       if(n < bufsize) buffer[n++] = '\\';
100       if(n < bufsize) buffer[n++] = '0' + ((ch >> 6) & 7);
101       if(n < bufsize) buffer[n++] = '0' + ((ch >> 3) & 7);
102       if(n < bufsize) buffer[n++] = '0' + ((ch >> 0) & 7);
103     }
104   }
105   if(n >= bufsize)
106     n = bufsize - 1;
107   buffer[n] = 0;
108 }
109
110 /** @brief Log to a file
111  * @param pri Message priority (as per syslog)
112  * @param msg Messagge to log
113  * @param user The @c FILE @c * to log to or NULL for @c stderr
114  */
115 static void logfp(int pri, const char *msg, void *user) {
116   struct timeval tv;
117   FILE *fp = user ? user : stderr;
118   /* ...because stderr is not a constant so we can't initialize log_stderr
119    * sanely */
120   const char *p;
121   
122   if(progname)
123     fprintf(fp, "%s: ", progname);
124   if(pri <= LOG_ERR)
125     fputs("ERROR: ", fp);
126   else if(pri < LOG_DEBUG)
127     fputs("INFO: ", fp);
128   else {
129     if(!debug_only) {
130       if(!(debug_only = getenv("DISORDER_DEBUG_ONLY")))
131         debug_only = "";
132     }
133     gettimeofday(&tv, 0);
134     p = debug_filename;
135     while(!strncmp(p, "../", 3)) p += 3;
136     if(*debug_only && strcmp(p, debug_only))
137       return;
138     fprintf(fp, "%llu.%06lu: %s:%d: ",
139             (unsigned long long)tv.tv_sec, (unsigned long)tv.tv_usec,
140             p, debug_lineno);
141   }
142   fputs(msg, fp);
143   fputc('\n', fp);
144 }
145
146 /** @brief Log to syslog */
147 static void logsyslog(int pri, const char *msg,
148                       void attribute((unused)) *user) {
149   if(pri < LOG_DEBUG)
150     syslog(pri, "%s", msg);
151   else
152     syslog(pri, "%s:%d: %s", debug_filename, debug_lineno, msg);
153 }
154
155 /** @brief Log output that writes to @c stderr */
156 struct log_output log_stderr = { logfp, 0 };
157
158 /** @brief Log output that sends to syslog */
159 struct log_output log_syslog = { logsyslog, 0 };
160
161 /** @brief Format and log a message */
162 static void vlogger(int pri, const char *fmt, va_list ap) {
163   char buffer[1024];
164
165   format(buffer, sizeof buffer, fmt, ap);
166   log_default->fn(pri, buffer, log_default->user);
167 }
168
169 /** @brief Format and log a message */
170 static void logger(int pri, const char *fmt, ...) {
171   va_list ap;
172
173   va_start(ap, fmt);
174   vlogger(pri, fmt, ap);
175   va_end(ap);
176 }
177
178 /** @brief Format and log a message
179  * @param pri Message priority (as per syslog)
180  * @param fmt Format string
181  * @param errno_value Errno value to include as a string, or 0
182  * @param ap Argument list
183  */
184 void elog(int pri, int errno_value, const char *fmt, va_list ap) {
185   char buffer[1024];
186
187   if(errno_value == 0)
188     vlogger(pri, fmt, ap);
189   else {
190     memset(buffer, 0, sizeof buffer);
191     byte_vsnprintf(buffer, sizeof buffer, fmt, ap);
192     buffer[sizeof buffer - 1] = 0;
193     logger(pri, "%s: %s", buffer, strerror(errno_value));
194   }
195 }
196
197 #define disorder_fatal fatal
198 #define disorder_error error
199 #define disorder_info info
200
201 /* shared implementation of vararg functions */
202 #include "log-impl.h"
203
204 /** @brief Log a debug message */
205 void debug(const char *msg, ...) {
206   va_list ap;
207
208   va_start(ap, msg);
209   vlogger(LOG_DEBUG, msg, ap);
210   va_end(ap);
211 }
212
213 /** @brief Set the program name from @c argc */
214 void set_progname(char **argv) {
215   if((progname = strrchr(argv[0], '/')))
216     ++progname;
217   else
218     progname = argv[0];
219 }
220
221 /*
222 Local Variables:
223 c-basic-offset:2
224 comment-column:40
225 End:
226 */