Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
5aff007d | 3 | * Copyright (C) 2004-2008 Richard Kettlewell |
460b9539 | 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 | ||
05b75f8d | 38 | #include "common.h" |
460b9539 | 39 | |
460b9539 | 40 | #include <errno.h> |
41 | #include <syslog.h> | |
42 | #include <sys/time.h> | |
43 | ||
44 | #include "log.h" | |
45 | #include "disorder.h" | |
46 | #include "printf.h" | |
47 | ||
14ad73b9 | 48 | /** @brief Definition of a log output */ |
460b9539 | 49 | struct log_output { |
14ad73b9 | 50 | /** @brief Function to call */ |
460b9539 | 51 | void (*fn)(int pri, const char *msg, void *user); |
14ad73b9 | 52 | /** @brief User data */ |
460b9539 | 53 | void *user; |
54 | }; | |
55 | ||
14ad73b9 RK |
56 | /** @brief Function to call on a fatal error |
57 | * | |
58 | * This is normally @c exit() but in the presence of @c fork() it | |
59 | * sometimes gets set to @c _exit(). */ | |
460b9539 | 60 | void (*exitfn)(int) attribute((noreturn)) = exit; |
14ad73b9 RK |
61 | |
62 | /** @brief Debug flag */ | |
460b9539 | 63 | int debugging; |
14ad73b9 RK |
64 | |
65 | /** @brief Program name */ | |
460b9539 | 66 | const char *progname; |
14ad73b9 RK |
67 | |
68 | /** @brief Filename for debug messages */ | |
460b9539 | 69 | const char *debug_filename; |
14ad73b9 RK |
70 | |
71 | /** @brief Line number for debug messages */ | |
460b9539 | 72 | int debug_lineno; |
14ad73b9 RK |
73 | |
74 | /** @brief Pointer to chosen log output structure */ | |
460b9539 | 75 | struct log_output *log_default = &log_stderr; |
76 | ||
14ad73b9 | 77 | /** @brief Filename to debug for */ |
460b9539 | 78 | static const char *debug_only; |
79 | ||
14ad73b9 RK |
80 | /** @brief Construct log line, encoding special characters |
81 | * | |
82 | * We might be receiving things in any old encoding, or binary rubbish | |
83 | * in no encoding at all, so escape anything we don't like the look | |
84 | * of. We limit the log message to a kilobyte. | |
85 | */ | |
460b9539 | 86 | static void format(char buffer[], size_t bufsize, const char *fmt, va_list ap) { |
87 | char t[1024]; | |
88 | const char *p; | |
89 | int ch; | |
90 | size_t n = 0; | |
91 | ||
cb9a695c RK |
92 | if(byte_vsnprintf(t, sizeof t, fmt, ap) < 0) { |
93 | strcpy(t, "[byte_vsnprintf failed: "); | |
94 | strncat(t, fmt, sizeof t - strlen(t) - 1); | |
95 | } | |
460b9539 | 96 | p = t; |
97 | while((ch = (unsigned char)*p++)) { | |
98 | if(ch >= ' ' && ch <= 126) { | |
99 | if(n < bufsize) buffer[n++] = ch; | |
100 | } else { | |
101 | if(n < bufsize) buffer[n++] = '\\'; | |
102 | if(n < bufsize) buffer[n++] = '0' + ((ch >> 6) & 7); | |
103 | if(n < bufsize) buffer[n++] = '0' + ((ch >> 3) & 7); | |
104 | if(n < bufsize) buffer[n++] = '0' + ((ch >> 0) & 7); | |
105 | } | |
106 | } | |
107 | if(n >= bufsize) | |
108 | n = bufsize - 1; | |
109 | buffer[n] = 0; | |
110 | } | |
111 | ||
14ad73b9 | 112 | /** @brief Log to a file |
840f3008 RK |
113 | * @param pri Message priority (as per syslog) |
114 | * @param msg Messagge to log | |
14ad73b9 RK |
115 | * @param user The @c FILE @c * to log to or NULL for @c stderr |
116 | */ | |
460b9539 | 117 | static void logfp(int pri, const char *msg, void *user) { |
118 | struct timeval tv; | |
119 | FILE *fp = user ? user : stderr; | |
120 | /* ...because stderr is not a constant so we can't initialize log_stderr | |
121 | * sanely */ | |
122 | const char *p; | |
123 | ||
124 | if(progname) | |
125 | fprintf(fp, "%s: ", progname); | |
126 | if(pri <= LOG_ERR) | |
127 | fputs("ERROR: ", fp); | |
128 | else if(pri < LOG_DEBUG) | |
129 | fputs("INFO: ", fp); | |
130 | else { | |
131 | if(!debug_only) { | |
132 | if(!(debug_only = getenv("DISORDER_DEBUG_ONLY"))) | |
133 | debug_only = ""; | |
134 | } | |
135 | gettimeofday(&tv, 0); | |
136 | p = debug_filename; | |
137 | while(!strncmp(p, "../", 3)) p += 3; | |
138 | if(*debug_only && strcmp(p, debug_only)) | |
139 | return; | |
140 | fprintf(fp, "%llu.%06lu: %s:%d: ", | |
141 | (unsigned long long)tv.tv_sec, (unsigned long)tv.tv_usec, | |
142 | p, debug_lineno); | |
143 | } | |
144 | fputs(msg, fp); | |
145 | fputc('\n', fp); | |
146 | } | |
147 | ||
14ad73b9 | 148 | /** @brief Log to syslog */ |
460b9539 | 149 | static void logsyslog(int pri, const char *msg, |
150 | void attribute((unused)) *user) { | |
151 | if(pri < LOG_DEBUG) | |
152 | syslog(pri, "%s", msg); | |
153 | else | |
154 | syslog(pri, "%s:%d: %s", debug_filename, debug_lineno, msg); | |
155 | } | |
156 | ||
14ad73b9 | 157 | /** @brief Log output that writes to @c stderr */ |
460b9539 | 158 | struct log_output log_stderr = { logfp, 0 }; |
14ad73b9 RK |
159 | |
160 | /** @brief Log output that sends to syslog */ | |
460b9539 | 161 | struct log_output log_syslog = { logsyslog, 0 }; |
162 | ||
14ad73b9 | 163 | /** @brief Format and log a message */ |
460b9539 | 164 | static void vlogger(int pri, const char *fmt, va_list ap) { |
165 | char buffer[1024]; | |
166 | ||
167 | format(buffer, sizeof buffer, fmt, ap); | |
168 | log_default->fn(pri, buffer, log_default->user); | |
169 | } | |
170 | ||
14ad73b9 | 171 | /** @brief Format and log a message */ |
460b9539 | 172 | static void logger(int pri, const char *fmt, ...) { |
173 | va_list ap; | |
174 | ||
175 | va_start(ap, fmt); | |
176 | vlogger(pri, fmt, ap); | |
177 | va_end(ap); | |
178 | } | |
179 | ||
14ad73b9 | 180 | /** @brief Format and log a message |
840f3008 RK |
181 | * @param pri Message priority (as per syslog) |
182 | * @param fmt Format string | |
14ad73b9 | 183 | * @param errno_value Errno value to include as a string, or 0 |
840f3008 | 184 | * @param ap Argument list |
14ad73b9 | 185 | */ |
460b9539 | 186 | void elog(int pri, int errno_value, const char *fmt, va_list ap) { |
187 | char buffer[1024]; | |
188 | ||
189 | if(errno_value == 0) | |
190 | vlogger(pri, fmt, ap); | |
191 | else { | |
192 | memset(buffer, 0, sizeof buffer); | |
193 | byte_vsnprintf(buffer, sizeof buffer, fmt, ap); | |
194 | buffer[sizeof buffer - 1] = 0; | |
195 | logger(pri, "%s: %s", buffer, strerror(errno_value)); | |
196 | } | |
197 | } | |
198 | ||
199 | #define disorder_fatal fatal | |
200 | #define disorder_error error | |
201 | #define disorder_info info | |
202 | ||
203 | /* shared implementation of vararg functions */ | |
204 | #include "log-impl.h" | |
205 | ||
14ad73b9 | 206 | /** @brief Log a debug message */ |
460b9539 | 207 | void debug(const char *msg, ...) { |
208 | va_list ap; | |
209 | ||
210 | va_start(ap, msg); | |
211 | vlogger(LOG_DEBUG, msg, ap); | |
212 | va_end(ap); | |
213 | } | |
214 | ||
14ad73b9 | 215 | /** @brief Set the program name from @c argc */ |
460b9539 | 216 | void set_progname(char **argv) { |
217 | if((progname = strrchr(argv[0], '/'))) | |
218 | ++progname; | |
219 | else | |
220 | progname = argv[0]; | |
221 | } | |
222 | ||
223 | /* | |
224 | Local Variables: | |
225 | c-basic-offset:2 | |
226 | comment-column:40 | |
227 | End: | |
228 | */ |