2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
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.
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.
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
20 /** @file lib/log.c @brief Errors and logging
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.
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
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.
35 #define NO_MEMORY_ALLOCATION
36 /* because the memory allocation functions report errors */
51 /** @brief Definition of a log output */
53 /** @brief Function to call */
54 void (*fn)(int pri, const char *msg, void *user);
55 /** @brief User data */
59 /** @brief Function to call on a fatal error
61 * This is normally @c exit() but in the presence of @c fork() it
62 * sometimes gets set to @c _exit(). */
63 void (*exitfn)(int) attribute((noreturn)) = exit;
65 /** @brief Debug flag */
68 /** @brief Program name */
71 /** @brief Filename for debug messages */
72 const char *debug_filename;
74 /** @brief Line number for debug messages */
77 /** @brief Pointer to chosen log output structure */
78 struct log_output *log_default = &log_stderr;
80 /** @brief Filename to debug for */
81 static const char *debug_only;
83 /** @brief Construct log line, encoding special characters
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.
89 static void format(char buffer[], size_t bufsize, const char *fmt, va_list ap) {
95 if(byte_vsnprintf(t, sizeof t, fmt, ap) < 0)
96 strcpy(t, "[byte_vsnprintf failed]");
98 while((ch = (unsigned char)*p++)) {
99 if(ch >= ' ' && ch <= 126) {
100 if(n < bufsize) buffer[n++] = ch;
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);
113 /** @brief Log to a file
114 * @param pri Message priority (as per syslog)
115 * @param msg Messagge to log
116 * @param user The @c FILE @c * to log to or NULL for @c stderr
118 static void logfp(int pri, const char *msg, void *user) {
120 FILE *fp = user ? user : stderr;
121 /* ...because stderr is not a constant so we can't initialize log_stderr
126 fprintf(fp, "%s: ", progname);
128 fputs("ERROR: ", fp);
129 else if(pri < LOG_DEBUG)
133 if(!(debug_only = getenv("DISORDER_DEBUG_ONLY")))
136 gettimeofday(&tv, 0);
138 while(!strncmp(p, "../", 3)) p += 3;
139 if(*debug_only && strcmp(p, debug_only))
141 fprintf(fp, "%llu.%06lu: %s:%d: ",
142 (unsigned long long)tv.tv_sec, (unsigned long)tv.tv_usec,
149 /** @brief Log to syslog */
150 static void logsyslog(int pri, const char *msg,
151 void attribute((unused)) *user) {
153 syslog(pri, "%s", msg);
155 syslog(pri, "%s:%d: %s", debug_filename, debug_lineno, msg);
158 /** @brief Log output that writes to @c stderr */
159 struct log_output log_stderr = { logfp, 0 };
161 /** @brief Log output that sends to syslog */
162 struct log_output log_syslog = { logsyslog, 0 };
164 /** @brief Format and log a message */
165 static void vlogger(int pri, const char *fmt, va_list ap) {
168 format(buffer, sizeof buffer, fmt, ap);
169 log_default->fn(pri, buffer, log_default->user);
172 /** @brief Format and log a message */
173 static void logger(int pri, const char *fmt, ...) {
177 vlogger(pri, fmt, ap);
181 /** @brief Format and log a message
182 * @param pri Message priority (as per syslog)
183 * @param fmt Format string
184 * @param errno_value Errno value to include as a string, or 0
185 * @param ap Argument list
187 void elog(int pri, int errno_value, const char *fmt, va_list ap) {
191 vlogger(pri, fmt, ap);
193 memset(buffer, 0, sizeof buffer);
194 byte_vsnprintf(buffer, sizeof buffer, fmt, ap);
195 buffer[sizeof buffer - 1] = 0;
196 logger(pri, "%s: %s", buffer, strerror(errno_value));
200 #define disorder_fatal fatal
201 #define disorder_error error
202 #define disorder_info info
204 /* shared implementation of vararg functions */
205 #include "log-impl.h"
207 /** @brief Log a debug message */
208 void debug(const char *msg, ...) {
212 vlogger(LOG_DEBUG, msg, ap);
216 /** @brief Set the program name from @c argc */
217 void set_progname(char **argv) {
218 if((progname = strrchr(argv[0], '/')))