chiark / gitweb /
Include sys/time.h in a few places, necessary for older libc versions.
[disorder] / lib / log.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2004-2008 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 *
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 */
460b9539 32
33#define NO_MEMORY_ALLOCATION
34/* because the memory allocation functions report errors */
35
05b75f8d 36#include "common.h"
460b9539 37
460b9539 38#include <errno.h>
39#include <syslog.h>
40#include <sys/time.h>
9d7a6129 41#include <time.h>
460b9539 42
43#include "log.h"
44#include "disorder.h"
45#include "printf.h"
46
14ad73b9 47/** @brief Definition of a log output */
460b9539 48struct log_output {
14ad73b9 49 /** @brief Function to call */
460b9539 50 void (*fn)(int pri, const char *msg, void *user);
14ad73b9 51 /** @brief User data */
460b9539 52 void *user;
53};
54
14ad73b9
RK
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(). */
460b9539 59void (*exitfn)(int) attribute((noreturn)) = exit;
14ad73b9
RK
60
61/** @brief Debug flag */
460b9539 62int debugging;
14ad73b9
RK
63
64/** @brief Program name */
460b9539 65const char *progname;
14ad73b9
RK
66
67/** @brief Filename for debug messages */
460b9539 68const char *debug_filename;
14ad73b9 69
9d7a6129
RK
70/** @brief Set to include timestamps in log messages */
71int logdate;
72
14ad73b9 73/** @brief Line number for debug messages */
460b9539 74int debug_lineno;
14ad73b9
RK
75
76/** @brief Pointer to chosen log output structure */
460b9539 77struct log_output *log_default = &log_stderr;
78
14ad73b9 79/** @brief Filename to debug for */
460b9539 80static const char *debug_only;
81
14ad73b9
RK
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 */
460b9539 88static 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
cb9a695c
RK
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 }
460b9539 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
14ad73b9 114/** @brief Log to a file
840f3008
RK
115 * @param pri Message priority (as per syslog)
116 * @param msg Messagge to log
14ad73b9
RK
117 * @param user The @c FILE @c * to log to or NULL for @c stderr
118 */
460b9539 119static 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(progname)
127 fprintf(fp, "%s: ", progname);
9d7a6129
RK
128 if(logdate) {
129 char timebuf[64];
130 struct tm *tm;
131 gettimeofday(&tv, 0);
132 tm = localtime(&tv.tv_sec);
133 strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M:%S %Z", tm);
134 fprintf(fp, "%s: ", timebuf);
135 }
460b9539 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
14ad73b9 158/** @brief Log to syslog */
460b9539 159static 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
14ad73b9 167/** @brief Log output that writes to @c stderr */
460b9539 168struct log_output log_stderr = { logfp, 0 };
14ad73b9
RK
169
170/** @brief Log output that sends to syslog */
460b9539 171struct log_output log_syslog = { logsyslog, 0 };
172
14ad73b9 173/** @brief Format and log a message */
460b9539 174static 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
14ad73b9 181/** @brief Format and log a message */
460b9539 182static 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
14ad73b9 190/** @brief Format and log a message
840f3008
RK
191 * @param pri Message priority (as per syslog)
192 * @param fmt Format string
14ad73b9 193 * @param errno_value Errno value to include as a string, or 0
840f3008 194 * @param ap Argument list
14ad73b9 195 */
460b9539 196void 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
093ec810
RK
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 */
214void 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 */
225void disorder_error(int errno_value, const char *msg, ...) {
226 va_list ap;
460b9539 227
093ec810
RK
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 */
234void 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}
460b9539 241
14ad73b9 242/** @brief Log a debug message */
093ec810 243void disorder_debug(const char *msg, ...) {
460b9539 244 va_list ap;
245
246 va_start(ap, msg);
247 vlogger(LOG_DEBUG, msg, ap);
248 va_end(ap);
249}
250
14ad73b9 251/** @brief Set the program name from @c argc */
460b9539 252void set_progname(char **argv) {
253 if((progname = strrchr(argv[0], '/')))
254 ++progname;
255 else
256 progname = argv[0];
257}
258
259/*
260Local Variables:
261c-basic-offset:2
262comment-column:40
263End:
264*/