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