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 */
47 /** @brief Definition of a log output */
49 /** @brief Function to call */
50 void (*fn)(int pri, const char *msg, void *user);
51 /** @brief User data */
55 /** @brief Function to call on a fatal error
57 * This is normally @c exit() but in the presence of @c fork() it
58 * sometimes gets set to @c _exit(). */
59 void (*exitfn)(int) attribute((noreturn)) = exit;
61 /** @brief Debug flag */
64 /** @brief Program name */
67 /** @brief Filename for debug messages */
68 const char *debug_filename;
70 /** @brief Set to include timestamps in log messages */
73 /** @brief Line number for debug messages */
76 /** @brief Pointer to chosen log output structure */
77 struct log_output *log_default = &log_stderr;
79 /** @brief Filename to debug for */
80 static const char *debug_only;
82 /** @brief Construct log line, encoding special characters
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.
88 static void format(char buffer[], size_t bufsize, const char *fmt, va_list ap) {
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);
99 while((ch = (unsigned char)*p++)) {
100 if(ch >= ' ' && ch <= 126) {
101 if(n < bufsize) buffer[n++] = ch;
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);
114 /** @brief Log to a file
115 * @param pri Message priority (as per syslog)
116 * @param msg Messagge to log
117 * @param user The @c FILE @c * to log to or NULL for @c stderr
119 static void logfp(int pri, const char *msg, void *user) {
121 FILE *fp = user ? user : stderr;
122 /* ...because stderr is not a constant so we can't initialize log_stderr
129 gettimeofday(&tv, 0);
130 tm = localtime(&tv.tv_sec);
131 strftime(timebuf, sizeof timebuf, "%Y-%m-%d %H:%M:%S %Z", tm);
132 fprintf(fp, "%s: ", timebuf);
135 fprintf(fp, "%s: ", progname);
137 fputs("ERROR: ", fp);
138 else if(pri < LOG_DEBUG)
142 if(!(debug_only = getenv("DISORDER_DEBUG_ONLY")))
145 gettimeofday(&tv, 0);
147 while(!strncmp(p, "../", 3)) p += 3;
148 if(*debug_only && strcmp(p, debug_only))
150 fprintf(fp, "%llu.%06lu: %s:%d: ",
151 (unsigned long long)tv.tv_sec, (unsigned long)tv.tv_usec,
158 /** @brief Log to syslog */
159 static void logsyslog(int pri, const char *msg,
160 void attribute((unused)) *user) {
162 syslog(pri, "%s", msg);
164 syslog(pri, "%s:%d: %s", debug_filename, debug_lineno, msg);
167 /** @brief Log output that writes to @c stderr */
168 struct log_output log_stderr = { logfp, 0 };
170 /** @brief Log output that sends to syslog */
171 struct log_output log_syslog = { logsyslog, 0 };
173 /** @brief Format and log a message */
174 static void vlogger(int pri, const char *fmt, va_list ap) {
177 format(buffer, sizeof buffer, fmt, ap);
178 log_default->fn(pri, buffer, log_default->user);
181 /** @brief Format and log a message */
182 static void logger(int pri, const char *fmt, ...) {
186 vlogger(pri, fmt, ap);
190 /** @brief Format and log a message
191 * @param pri Message priority (as per syslog)
192 * @param fmt Format string
193 * @param errno_value Errno value to include as a string, or 0
194 * @param ap Argument list
196 void elog(int pri, int errno_value, const char *fmt, va_list ap) {
200 vlogger(pri, fmt, ap);
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));
209 /** @brief Log an error and quit
211 * If @c ${DISORDER_FATAL_ABORT} is defined (as anything) then the process
212 * is aborted, so you can get a backtrace.
214 void disorder_fatal(int errno_value, const char *msg, ...) {
218 elog(LOG_CRIT, errno_value, msg, ap);
220 if(getenv("DISORDER_FATAL_ABORT")) abort();
221 exitfn(EXIT_FAILURE);
224 /** @brief Log an error */
225 void disorder_error(int errno_value, const char *msg, ...) {
229 elog(LOG_ERR, errno_value, msg, ap);
233 /** @brief Log an informational message */
234 void disorder_info(const char *msg, ...) {
238 elog(LOG_INFO, 0, msg, ap);
242 /** @brief Log a debug message */
243 void disorder_debug(const char *msg, ...) {
247 vlogger(LOG_DEBUG, msg, ap);
251 /** @brief Set the program name from @c argc */
252 void set_progname(char **argv) {
253 if((progname = strrchr(argv[0], '/')))