2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file lib/log.c @brief Errors and logging
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.
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
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.
33 #define NO_MEMORY_ALLOCATION
34 /* because the memory allocation functions report errors */
46 /** @brief Definition of a log output */
48 /** @brief Function to call */
49 void (*fn)(int pri, const char *msg, void *user);
50 /** @brief User data */
54 /** @brief Function to call on a fatal error
56 * This is normally @c exit() but in the presence of @c fork() it
57 * sometimes gets set to @c _exit(). */
58 void (*exitfn)(int) attribute((noreturn)) = exit;
60 /** @brief Debug flag */
63 /** @brief Program name */
66 /** @brief Filename for debug messages */
67 const char *debug_filename;
69 /** @brief Line number for debug messages */
72 /** @brief Pointer to chosen log output structure */
73 struct log_output *log_default = &log_stderr;
75 /** @brief Filename to debug for */
76 static const char *debug_only;
78 /** @brief Construct log line, encoding special characters
80 * We might be receiving things in any old encoding, or binary rubbish
81 * in no encoding at all, so escape anything we don't like the look
82 * of. We limit the log message to a kilobyte.
84 static void format(char buffer[], size_t bufsize, const char *fmt, va_list ap) {
90 if(byte_vsnprintf(t, sizeof t, fmt, ap) < 0) {
91 strcpy(t, "[byte_vsnprintf failed: ");
92 strncat(t, fmt, sizeof t - strlen(t) - 1);
95 while((ch = (unsigned char)*p++)) {
96 if(ch >= ' ' && ch <= 126) {
97 if(n < bufsize) buffer[n++] = ch;
99 if(n < bufsize) buffer[n++] = '\\';
100 if(n < bufsize) buffer[n++] = '0' + ((ch >> 6) & 7);
101 if(n < bufsize) buffer[n++] = '0' + ((ch >> 3) & 7);
102 if(n < bufsize) buffer[n++] = '0' + ((ch >> 0) & 7);
110 /** @brief Log to a file
111 * @param pri Message priority (as per syslog)
112 * @param msg Messagge to log
113 * @param user The @c FILE @c * to log to or NULL for @c stderr
115 static void logfp(int pri, const char *msg, void *user) {
117 FILE *fp = user ? user : stderr;
118 /* ...because stderr is not a constant so we can't initialize log_stderr
123 fprintf(fp, "%s: ", progname);
125 fputs("ERROR: ", fp);
126 else if(pri < LOG_DEBUG)
130 if(!(debug_only = getenv("DISORDER_DEBUG_ONLY")))
133 gettimeofday(&tv, 0);
135 while(!strncmp(p, "../", 3)) p += 3;
136 if(*debug_only && strcmp(p, debug_only))
138 fprintf(fp, "%llu.%06lu: %s:%d: ",
139 (unsigned long long)tv.tv_sec, (unsigned long)tv.tv_usec,
146 /** @brief Log to syslog */
147 static void logsyslog(int pri, const char *msg,
148 void attribute((unused)) *user) {
150 syslog(pri, "%s", msg);
152 syslog(pri, "%s:%d: %s", debug_filename, debug_lineno, msg);
155 /** @brief Log output that writes to @c stderr */
156 struct log_output log_stderr = { logfp, 0 };
158 /** @brief Log output that sends to syslog */
159 struct log_output log_syslog = { logsyslog, 0 };
161 /** @brief Format and log a message */
162 static void vlogger(int pri, const char *fmt, va_list ap) {
165 format(buffer, sizeof buffer, fmt, ap);
166 log_default->fn(pri, buffer, log_default->user);
169 /** @brief Format and log a message */
170 static void logger(int pri, const char *fmt, ...) {
174 vlogger(pri, fmt, ap);
178 /** @brief Format and log a message
179 * @param pri Message priority (as per syslog)
180 * @param fmt Format string
181 * @param errno_value Errno value to include as a string, or 0
182 * @param ap Argument list
184 void elog(int pri, int errno_value, const char *fmt, va_list ap) {
188 vlogger(pri, fmt, ap);
190 memset(buffer, 0, sizeof buffer);
191 byte_vsnprintf(buffer, sizeof buffer, fmt, ap);
192 buffer[sizeof buffer - 1] = 0;
193 logger(pri, "%s: %s", buffer, strerror(errno_value));
197 /** @brief Log an error and quit
199 * If @c ${DISORDER_FATAL_ABORT} is defined (as anything) then the process
200 * is aborted, so you can get a backtrace.
202 void disorder_fatal(int errno_value, const char *msg, ...) {
206 elog(LOG_CRIT, errno_value, msg, ap);
208 if(getenv("DISORDER_FATAL_ABORT")) abort();
209 exitfn(EXIT_FAILURE);
212 /** @brief Log an error */
213 void disorder_error(int errno_value, const char *msg, ...) {
217 elog(LOG_ERR, errno_value, msg, ap);
221 /** @brief Log an informational message */
222 void disorder_info(const char *msg, ...) {
226 elog(LOG_INFO, 0, msg, ap);
230 /** @brief Log a debug message */
231 void disorder_debug(const char *msg, ...) {
235 vlogger(LOG_DEBUG, msg, ap);
239 /** @brief Set the program name from @c argc */
240 void set_progname(char **argv) {
241 if((progname = strrchr(argv[0], '/')))