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