2 * This file is part of DisOrder.
3 * Copyright (C) 2004-9, 2013 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
21 * below. disorder_debug() is generally invoked via D() so that
22 * mostly you just do a test 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 * 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.
34 #define NO_MEMORY_ALLOCATION
35 /* because the memory allocation functions report errors */
40 #include <sys/types.h>
45 # include <sys/time.h>
48 # include <sys/socket.h>
59 /** @brief Definition of a log output */
61 /** @brief Function to call */
62 void (*fn
)(int pri
, const char *msg
, void *user
);
63 /** @brief User data */
67 /** @brief Function to call on a fatal error
69 * This is normally @c exit() but in the presence of @c fork() it
70 * sometimes gets set to @c _exit(). */
71 void (*exitfn
)(int) attribute((noreturn
)) = exit
;
73 /** @brief Debug flag */
76 /** @brief Program name */
79 /** @brief Filename for debug messages */
80 const char *debug_filename
;
82 /** @brief Set to include timestamps in log messages */
85 /** @brief Line number for debug messages */
88 /** @brief Pointer to chosen log output structure */
89 struct log_output
*log_default
= &log_stderr
;
91 /** @brief Filename to debug for */
92 static const char *debug_only
;
94 /** @brief Construct log line, encoding special characters
96 * We might be receiving things in any old encoding, or binary rubbish
97 * in no encoding at all, so escape anything we don't like the look
98 * of. We limit the log message to a kilobyte.
100 static void format(char buffer
[], size_t bufsize
, const char *fmt
, va_list ap
) {
106 if(byte_vsnprintf(t
, sizeof t
, fmt
, ap
) < 0) {
107 strcpy(t
, "[byte_vsnprintf failed: ");
108 strncat(t
, fmt
, sizeof t
- strlen(t
) - 1);
111 while((ch
= (unsigned char)*p
++)) {
112 if(ch
>= ' ' && ch
<= 126) {
113 if(n
< bufsize
) buffer
[n
++] = ch
;
115 if(n
< bufsize
) buffer
[n
++] = '\\';
116 if(n
< bufsize
) buffer
[n
++] = '0' + ((ch
>> 6) & 7);
117 if(n
< bufsize
) buffer
[n
++] = '0' + ((ch
>> 3) & 7);
118 if(n
< bufsize
) buffer
[n
++] = '0' + ((ch
>> 0) & 7);
126 /** @brief Log to a file
127 * @param pri Message priority (as per syslog)
128 * @param msg Messagge to log
129 * @param user The @c FILE @c * to log to or NULL for @c stderr
131 static void logfp(int pri
, const char *msg
, void *user
) {
133 FILE *fp
= user ? user
: stderr
;
134 /* ...because stderr is not a constant so we can't initialize log_stderr
142 gettimeofday(&tv
, 0);
145 strftime(timebuf
, sizeof timebuf
, "%Y-%m-%d %H:%M:%S %Z", tm
);
146 fprintf(fp
, "%s: ", timebuf
);
149 fprintf(fp
, "%s: ", progname
);
151 fputs("ERROR: ", fp
);
152 else if(pri
< LOG_DEBUG
)
156 if(!(debug_only
= getenv("DISORDER_DEBUG_ONLY")))
159 gettimeofday(&tv
, 0);
161 while(!strncmp(p
, "../", 3)) p
+= 3;
162 if(*debug_only
&& strcmp(p
, debug_only
))
164 fprintf(fp
, "%llu.%06lu: %s:%d: ",
165 (unsigned long long)tv
.tv_sec
, (unsigned long)tv
.tv_usec
,
174 /** @brief Log to syslog */
175 static void logsyslog(int pri
, const char *msg
,
176 void attribute((unused
)) *user
) {
178 syslog(pri
, "%s", msg
);
180 syslog(pri
, "%s:%d: %s", debug_filename
, debug_lineno
, msg
);
184 /** @brief Log output that writes to @c stderr */
185 struct log_output log_stderr
= { logfp
, 0 };
188 /** @brief Log output that sends to syslog */
189 struct log_output log_syslog
= { logsyslog
, 0 };
192 /** @brief Format and log a message */
193 static void vlogger(int pri
, const char *fmt
, va_list ap
) {
196 format(buffer
, sizeof buffer
, fmt
, ap
);
197 log_default
->fn(pri
, buffer
, log_default
->user
);
200 /** @brief Format and log a message */
201 static void logger(int pri
, const char *fmt
, ...) {
205 vlogger(pri
, fmt
, ap
);
209 /** @brief Format and log a message
210 * @param pri Message priority (as per syslog)
211 * @param ec Error class
212 * @param fmt Format string
213 * @param errno_value Errno value to include as a string, or 0
214 * @param ap Argument list
216 void elog(int pri
, enum error_class ec
, int errno_value
, const char *fmt
, va_list ap
) {
221 vlogger(pri
, fmt
, ap
);
223 memset(buffer
, 0, sizeof buffer
);
224 byte_vsnprintf(buffer
, sizeof buffer
, fmt
, ap
);
225 buffer
[sizeof buffer
- 1] = 0;
226 logger(pri
, "%s: %s", buffer
,
227 format_error(ec
, errno_value
, errbuf
, sizeof errbuf
));
231 /** @brief Log an error and quit
233 * If @c ${DISORDER_FATAL_ABORT} is defined (as anything) then the process
234 * is aborted, so you can get a backtrace.
236 void disorder_fatal(int errno_value
, const char *msg
, ...) {
240 elog(LOG_CRIT
, ec_errno
, errno_value
, msg
, ap
);
242 if(getenv("DISORDER_FATAL_ABORT")) abort();
243 exitfn(EXIT_FAILURE
);
246 /** @brief Log an error and quit
248 * If @c ${DISORDER_FATAL_ABORT} is defined (as anything) then the process
249 * is aborted, so you can get a backtrace.
251 void disorder_fatal_ec(enum error_class ec
, int errno_value
, const char *msg
, ...) {
255 elog(LOG_CRIT
, ec
, errno_value
, msg
, ap
);
257 if(getenv("DISORDER_FATAL_ABORT")) abort();
258 exitfn(EXIT_FAILURE
);
261 /** @brief Log an error */
262 void disorder_error(int errno_value
, const char *msg
, ...) {
266 elog(LOG_ERR
, ec_errno
, errno_value
, msg
, ap
);
270 /** @brief Log an error */
271 void disorder_error_ec(enum error_class ec
, int errno_value
, const char *msg
, ...) {
275 elog(LOG_ERR
, ec
, errno_value
, msg
, ap
);
279 /** @brief Log an informational message */
280 void disorder_info(const char *msg
, ...) {
284 elog(LOG_INFO
, ec_none
, 0, msg
, ap
);
288 /** @brief Log a debug message */
289 void disorder_debug(const char *msg
, ...) {
293 vlogger(LOG_DEBUG
, msg
, ap
);
297 /** @brief Set the program name from @c argc */
298 void set_progname(char **argv
) {
299 if((progname
= strrchr(argv
[0], '/')))
305 /** @brief Format an error string
306 * @param ec Error class
307 * @param err Error code (interpretation defined by @p ec)
308 * @param buffer Output buffer
309 * @param bufsize Size of output buffer
310 * @return Pointer to error string
312 * The return value may or may not be @p buffer.
315 #pragma GCC diagnostic ignored "-Wunused-parameter"
317 const char *format_error(enum error_class ec
, int err
, char buffer
[], size_t bufsize
) {
322 if(!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
|FORMAT_MESSAGE_IGNORE_INSERTS
,
325 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
329 disorder_fatal(0, "FormatMessage failed");
331 while(n
> 0 && isspace((unsigned char)buffer
[n
-1]))
336 strerror_s(buffer
, bufsize
, err
);
344 return strerror(err
);
346 return gai_strerror(err
);