chiark / gitweb /
Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/disorder
[disorder] / lib / log.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
cca89d7c 3 * Copyright (C) 2004-9, 2013 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
14ad73b9
RK
18/** @file lib/log.c @brief Errors and logging
19 *
2e9ba080
RK
20 * All messages are initially emitted by one of the four functions
21 * below. disorder_debug() is generally invoked via D() so that
22 * mostly you just do a test rather than a complete subroutine call.
14ad73b9
RK
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 *
2e9ba080
RK
28 * disorder_fatal() will call exitfn() with a nonzero status. The
29 * default value is exit(), but it should be set to _exit() anywhere
30 * but the 'main line' of the program, to guarantee that exit() gets
31 * called at most once.
14ad73b9 32 */
460b9539 33
34#define NO_MEMORY_ALLOCATION
35/* because the memory allocation functions report errors */
36
05b75f8d 37#include "common.h"
460b9539 38
460b9539 39#include <errno.h>
cca89d7c
RK
40#include <sys/types.h>
41#if HAVE_SYSLOG_H
42# include <syslog.h>
43#endif
44#if HAVE_SYS_TIME_H
45# include <sys/time.h>
46#endif
47#if HAVE_SYS_SOCKET_H
48# include <sys/socket.h>
49#endif
50#if HAVE_NETDB_H
51# include <netdb.h>
52#endif
9d7a6129 53#include <time.h>
460b9539 54
55#include "log.h"
56#include "disorder.h"
57#include "printf.h"
58
14ad73b9 59/** @brief Definition of a log output */
460b9539 60struct log_output {
14ad73b9 61 /** @brief Function to call */
460b9539 62 void (*fn)(int pri, const char *msg, void *user);
14ad73b9 63 /** @brief User data */
460b9539 64 void *user;
65};
66
14ad73b9
RK
67/** @brief Function to call on a fatal error
68 *
69 * This is normally @c exit() but in the presence of @c fork() it
70 * sometimes gets set to @c _exit(). */
460b9539 71void (*exitfn)(int) attribute((noreturn)) = exit;
14ad73b9
RK
72
73/** @brief Debug flag */
460b9539 74int debugging;
14ad73b9
RK
75
76/** @brief Program name */
460b9539 77const char *progname;
14ad73b9
RK
78
79/** @brief Filename for debug messages */
460b9539 80const char *debug_filename;
14ad73b9 81
9d7a6129
RK
82/** @brief Set to include timestamps in log messages */
83int logdate;
84
14ad73b9 85/** @brief Line number for debug messages */
460b9539 86int debug_lineno;
14ad73b9
RK
87
88/** @brief Pointer to chosen log output structure */
460b9539 89struct log_output *log_default = &log_stderr;
90
14ad73b9 91/** @brief Filename to debug for */
460b9539 92static const char *debug_only;
93
14ad73b9
RK
94/** @brief Construct log line, encoding special characters
95 *
96 * We might be receiving things in any old encoding, or binary rubbish
97 * in no encoding at all, so escape anything we don't like the look
98 * of. We limit the log message to a kilobyte.
99 */
460b9539 100static void format(char buffer[], size_t bufsize, const char *fmt, va_list ap) {
101 char t[1024];
102 const char *p;
103 int ch;
104 size_t n = 0;
105
cb9a695c
RK
106 if(byte_vsnprintf(t, sizeof t, fmt, ap) < 0) {
107 strcpy(t, "[byte_vsnprintf failed: ");
108 strncat(t, fmt, sizeof t - strlen(t) - 1);
109 }
460b9539 110 p = t;
111 while((ch = (unsigned char)*p++)) {
112 if(ch >= ' ' && ch <= 126) {
113 if(n < bufsize) buffer[n++] = ch;
114 } else {
115 if(n < bufsize) buffer[n++] = '\\';
116 if(n < bufsize) buffer[n++] = '0' + ((ch >> 6) & 7);
117 if(n < bufsize) buffer[n++] = '0' + ((ch >> 3) & 7);
118 if(n < bufsize) buffer[n++] = '0' + ((ch >> 0) & 7);
119 }
120 }
121 if(n >= bufsize)
122 n = bufsize - 1;
123 buffer[n] = 0;
124}
125
14ad73b9 126/** @brief Log to a file
840f3008
RK
127 * @param pri Message priority (as per syslog)
128 * @param msg Messagge to log
14ad73b9
RK
129 * @param user The @c FILE @c * to log to or NULL for @c stderr
130 */
460b9539 131static void logfp(int pri, const char *msg, void *user) {
132 struct timeval tv;
133 FILE *fp = user ? user : stderr;
134 /* ...because stderr is not a constant so we can't initialize log_stderr
135 * sanely */
136 const char *p;
137
9d7a6129
RK
138 if(logdate) {
139 char timebuf[64];
140 struct tm *tm;
9e42afcd 141 time_t t;
9d7a6129 142 gettimeofday(&tv, 0);
9e42afcd
RK
143 t = tv.tv_sec;
144 tm = localtime(&t);
9d7a6129
RK
145 strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M:%S %Z", tm);
146 fprintf(fp, "%s: ", timebuf);
5bb00bdd
RK
147 }
148 if(progname)
149 fprintf(fp, "%s: ", progname);
460b9539 150 if(pri <= LOG_ERR)
151 fputs("ERROR: ", fp);
152 else if(pri < LOG_DEBUG)
153 fputs("INFO: ", fp);
154 else {
155 if(!debug_only) {
156 if(!(debug_only = getenv("DISORDER_DEBUG_ONLY")))
157 debug_only = "";
158 }
159 gettimeofday(&tv, 0);
160 p = debug_filename;
161 while(!strncmp(p, "../", 3)) p += 3;
162 if(*debug_only && strcmp(p, debug_only))
163 return;
164 fprintf(fp, "%llu.%06lu: %s:%d: ",
165 (unsigned long long)tv.tv_sec, (unsigned long)tv.tv_usec,
166 p, debug_lineno);
167 }
168 fputs(msg, fp);
169 fputc('\n', fp);
9e42afcd 170 fflush(fp);
460b9539 171}
172
cca89d7c 173#if HAVE_SYSLOG_H
14ad73b9 174/** @brief Log to syslog */
460b9539 175static void logsyslog(int pri, const char *msg,
176 void attribute((unused)) *user) {
177 if(pri < LOG_DEBUG)
178 syslog(pri, "%s", msg);
179 else
180 syslog(pri, "%s:%d: %s", debug_filename, debug_lineno, msg);
181}
cca89d7c 182#endif
460b9539 183
14ad73b9 184/** @brief Log output that writes to @c stderr */
460b9539 185struct log_output log_stderr = { logfp, 0 };
14ad73b9 186
cca89d7c 187#if HAVE_SYSLOG_H
14ad73b9 188/** @brief Log output that sends to syslog */
460b9539 189struct log_output log_syslog = { logsyslog, 0 };
cca89d7c 190#endif
460b9539 191
14ad73b9 192/** @brief Format and log a message */
460b9539 193static void vlogger(int pri, const char *fmt, va_list ap) {
194 char buffer[1024];
195
196 format(buffer, sizeof buffer, fmt, ap);
197 log_default->fn(pri, buffer, log_default->user);
198}
199
14ad73b9 200/** @brief Format and log a message */
460b9539 201static void logger(int pri, const char *fmt, ...) {
202 va_list ap;
203
204 va_start(ap, fmt);
205 vlogger(pri, fmt, ap);
206 va_end(ap);
207}
208
14ad73b9 209/** @brief Format and log a message
840f3008 210 * @param pri Message priority (as per syslog)
92db088e 211 * @param ec Error class
840f3008 212 * @param fmt Format string
14ad73b9 213 * @param errno_value Errno value to include as a string, or 0
840f3008 214 * @param ap Argument list
14ad73b9 215 */
92db088e 216void elog(int pri, enum error_class ec, int errno_value, const char *fmt, va_list ap) {
460b9539 217 char buffer[1024];
92db088e 218 char errbuf[1024];
460b9539 219
220 if(errno_value == 0)
221 vlogger(pri, fmt, ap);
222 else {
223 memset(buffer, 0, sizeof buffer);
224 byte_vsnprintf(buffer, sizeof buffer, fmt, ap);
225 buffer[sizeof buffer - 1] = 0;
92db088e
RK
226 logger(pri, "%s: %s", buffer,
227 format_error(ec, errno_value, errbuf, sizeof errbuf));
460b9539 228 }
229}
230
093ec810
RK
231/** @brief Log an error and quit
232 *
233 * If @c ${DISORDER_FATAL_ABORT} is defined (as anything) then the process
234 * is aborted, so you can get a backtrace.
235 */
236void disorder_fatal(int errno_value, const char *msg, ...) {
237 va_list ap;
238
239 va_start(ap, msg);
92db088e
RK
240 elog(LOG_CRIT, ec_errno, errno_value, msg, ap);
241 va_end(ap);
242 if(getenv("DISORDER_FATAL_ABORT")) abort();
243 exitfn(EXIT_FAILURE);
244}
245
246/** @brief Log an error and quit
247 *
248 * If @c ${DISORDER_FATAL_ABORT} is defined (as anything) then the process
249 * is aborted, so you can get a backtrace.
250 */
251void disorder_fatal_ec(enum error_class ec, int errno_value, const char *msg, ...) {
252 va_list ap;
253
254 va_start(ap, msg);
255 elog(LOG_CRIT, ec, errno_value, msg, ap);
093ec810
RK
256 va_end(ap);
257 if(getenv("DISORDER_FATAL_ABORT")) abort();
258 exitfn(EXIT_FAILURE);
259}
260
261/** @brief Log an error */
262void disorder_error(int errno_value, const char *msg, ...) {
263 va_list ap;
460b9539 264
093ec810 265 va_start(ap, msg);
92db088e
RK
266 elog(LOG_ERR, ec_errno, errno_value, msg, ap);
267 va_end(ap);
268}
269
270/** @brief Log an error */
271void disorder_error_ec(enum error_class ec, int errno_value, const char *msg, ...) {
272 va_list ap;
273
274 va_start(ap, msg);
275 elog(LOG_ERR, ec, errno_value, msg, ap);
093ec810
RK
276 va_end(ap);
277}
278
279/** @brief Log an informational message */
280void disorder_info(const char *msg, ...) {
281 va_list ap;
282
283 va_start(ap, msg);
92db088e 284 elog(LOG_INFO, ec_none, 0, msg, ap);
093ec810
RK
285 va_end(ap);
286}
460b9539 287
14ad73b9 288/** @brief Log a debug message */
093ec810 289void disorder_debug(const char *msg, ...) {
460b9539 290 va_list ap;
291
292 va_start(ap, msg);
293 vlogger(LOG_DEBUG, msg, ap);
294 va_end(ap);
295}
296
14ad73b9 297/** @brief Set the program name from @c argc */
460b9539 298void set_progname(char **argv) {
299 if((progname = strrchr(argv[0], '/')))
300 ++progname;
301 else
302 progname = argv[0];
303}
304
92db088e
RK
305/** @brief Format an error string
306 * @param ec Error class
307 * @param err Error code (interpretation defined by @p ec)
308 * @param buffer Output buffer
309 * @param bufsize Size of output buffer
310 * @return Pointer to error string
311 *
312 * The return value may or may not be @p buffer.
313 */
9e42afcd 314#if !_WIN32
92db088e 315#pragma GCC diagnostic ignored "-Wunused-parameter"
9e42afcd 316#endif
92db088e 317const char *format_error(enum error_class ec, int err, char buffer[], size_t bufsize) {
9e42afcd
RK
318#if _WIN32
319 size_t n;
320 switch(ec) {
321 default:
322 if(!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
323 NULL,
324 err,
325 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
326 buffer,
327 bufsize,
328 NULL))
329 disorder_fatal(0, "FormatMessage failed");
330 n = strlen(buffer);
331 while(n > 0 && isspace((unsigned char)buffer[n-1]))
332 --n;
333 buffer[n] = 0;
334 return buffer;
335 case ec_errno:
336 strerror_s(buffer, bufsize, err);
337 return buffer;
338 case ec_none:
339 return "(none)";
340 }
341#else
92db088e
RK
342 switch(ec) {
343 default:
344 return strerror(err);
345 case ec_getaddrinfo:
346 return gai_strerror(err);
347 case ec_none:
348 return "(none)";
349 }
9e42afcd 350#endif
92db088e
RK
351}
352
460b9539 353/*
354Local Variables:
355c-basic-offset:2
356comment-column:40
357End:
358*/