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