00ec7cd3a2a4f8039321fba3be74f3ae64bc7532
[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 #include <time.h>
42
43 #include "log.h"
44 #include "disorder.h"
45 #include "printf.h"
46
47 /** @brief Definition of a log output */
48 struct log_output {
49 /** @brief Function to call */
50 void (*fn)(int pri, const char *msg, void *user);
51 /** @brief User data */
52 void *user;
53 };
54
55 /** @brief Function to call on a fatal error
56 *
57 * This is normally @c exit() but in the presence of @c fork() it
58 * sometimes gets set to @c _exit(). */
59 void (*exitfn)(int) attribute((noreturn)) = exit;
60
61 /** @brief Debug flag */
62 int debugging;
63
64 /** @brief Program name */
65 const char *progname;
66
67 /** @brief Filename for debug messages */
68 const char *debug_filename;
69
70 /** @brief Set to include timestamps in log messages */
71 int logdate;
72
73 /** @brief Line number for debug messages */
74 int debug_lineno;
75
76 /** @brief Pointer to chosen log output structure */
77 struct log_output *log_default = &log_stderr;
78
79 /** @brief Filename to debug for */
80 static const char *debug_only;
81
82 /** @brief Construct log line, encoding special characters
83 *
84 * We might be receiving things in any old encoding, or binary rubbish
85 * in no encoding at all, so escape anything we don't like the look
86 * of. We limit the log message to a kilobyte.
87 */
88 static void format(char buffer[], size_t bufsize, const char *fmt, va_list ap) {
89 char t[1024];
90 const char *p;
91 int ch;
92 size_t n = 0;
93
94 if(byte_vsnprintf(t, sizeof t, fmt, ap) < 0) {
95 strcpy(t, "[byte_vsnprintf failed: ");
96 strncat(t, fmt, sizeof t - strlen(t) - 1);
97 }
98 p = t;
99 while((ch = (unsigned char)*p++)) {
100 if(ch >= ' ' && ch <= 126) {
101 if(n < bufsize) buffer[n++] = ch;
102 } else {
103 if(n < bufsize) buffer[n++] = '\\';
104 if(n < bufsize) buffer[n++] = '0' + ((ch >> 6) & 7);
105 if(n < bufsize) buffer[n++] = '0' + ((ch >> 3) & 7);
106 if(n < bufsize) buffer[n++] = '0' + ((ch >> 0) & 7);
107 }
108 }
109 if(n >= bufsize)
110 n = bufsize - 1;
111 buffer[n] = 0;
112 }
113
114 /** @brief Log to a file
115 * @param pri Message priority (as per syslog)
116 * @param msg Messagge to log
117 * @param user The @c FILE @c * to log to or NULL for @c stderr
118 */
119 static void logfp(int pri, const char *msg, void *user) {
120 struct timeval tv;
121 FILE *fp = user ? user : stderr;
122 /* ...because stderr is not a constant so we can't initialize log_stderr
123 * sanely */
124 const char *p;
125
126 if(logdate) {
127 char timebuf[64];
128 struct tm *tm;
129 gettimeofday(&tv, 0);
130 tm = localtime(&tv.tv_sec);
131 strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M:%S %Z", tm);
132 fprintf(fp, "%s: ", timebuf);
133 }
134 if(progname)
135 fprintf(fp, "%s: ", progname);
136 if(pri <= LOG_ERR)
137 fputs("ERROR: ", fp);
138 else if(pri < LOG_DEBUG)
139 fputs("INFO: ", fp);
140 else {
141 if(!debug_only) {
142 if(!(debug_only = getenv("DISORDER_DEBUG_ONLY")))
143 debug_only = "";
144 }
145 gettimeofday(&tv, 0);
146 p = debug_filename;
147 while(!strncmp(p, "../", 3)) p += 3;
148 if(*debug_only && strcmp(p, debug_only))
149 return;
150 fprintf(fp, "%llu.%06lu: %s:%d: ",
151 (unsigned long long)tv.tv_sec, (unsigned long)tv.tv_usec,
152 p, debug_lineno);
153 }
154 fputs(msg, fp);
155 fputc('\n', fp);
156 }
157
158 /** @brief Log to syslog */
159 static void logsyslog(int pri, const char *msg,
160 void attribute((unused)) *user) {
161 if(pri < LOG_DEBUG)
162 syslog(pri, "%s", msg);
163 else
164 syslog(pri, "%s:%d: %s", debug_filename, debug_lineno, msg);
165 }
166
167 /** @brief Log output that writes to @c stderr */
168 struct log_output log_stderr = { logfp, 0 };
169
170 /** @brief Log output that sends to syslog */
171 struct log_output log_syslog = { logsyslog, 0 };
172
173 /** @brief Format and log a message */
174 static void vlogger(int pri, const char *fmt, va_list ap) {
175 char buffer[1024];
176
177 format(buffer, sizeof buffer, fmt, ap);
178 log_default->fn(pri, buffer, log_default->user);
179 }
180
181 /** @brief Format and log a message */
182 static void logger(int pri, const char *fmt, ...) {
183 va_list ap;
184
185 va_start(ap, fmt);
186 vlogger(pri, fmt, ap);
187 va_end(ap);
188 }
189
190 /** @brief Format and log a message
191 * @param pri Message priority (as per syslog)
192 * @param fmt Format string
193 * @param errno_value Errno value to include as a string, or 0
194 * @param ap Argument list
195 */
196 void elog(int pri, int errno_value, const char *fmt, va_list ap) {
197 char buffer[1024];
198
199 if(errno_value == 0)
200 vlogger(pri, fmt, ap);
201 else {
202 memset(buffer, 0, sizeof buffer);
203 byte_vsnprintf(buffer, sizeof buffer, fmt, ap);
204 buffer[sizeof buffer - 1] = 0;
205 logger(pri, "%s: %s", buffer, strerror(errno_value));
206 }
207 }
208
209 /** @brief Log an error and quit
210 *
211 * If @c ${DISORDER_FATAL_ABORT} is defined (as anything) then the process
212 * is aborted, so you can get a backtrace.
213 */
214 void disorder_fatal(int errno_value, const char *msg, ...) {
215 va_list ap;
216
217 va_start(ap, msg);
218 elog(LOG_CRIT, errno_value, msg, ap);
219 va_end(ap);
220 if(getenv("DISORDER_FATAL_ABORT")) abort();
221 exitfn(EXIT_FAILURE);
222 }
223
224 /** @brief Log an error */
225 void disorder_error(int errno_value, const char *msg, ...) {
226 va_list ap;
227
228 va_start(ap, msg);
229 elog(LOG_ERR, errno_value, msg, ap);
230 va_end(ap);
231 }
232
233 /** @brief Log an informational message */
234 void disorder_info(const char *msg, ...) {
235 va_list ap;
236
237 va_start(ap, msg);
238 elog(LOG_INFO, 0, msg, ap);
239 va_end(ap);
240 }
241
242 /** @brief Log a debug message */
243 void disorder_debug(const char *msg, ...) {
244 va_list ap;
245
246 va_start(ap, msg);
247 vlogger(LOG_DEBUG, msg, ap);
248 va_end(ap);
249 }
250
251 /** @brief Set the program name from @c argc */
252 void set_progname(char **argv) {
253 if((progname = strrchr(argv[0], '/')))
254 ++progname;
255 else
256 progname = argv[0];
257 }
258
259 /*
260 Local Variables:
261 c-basic-offset:2
262 comment-column:40
263 End:
264 */