5 Author: Johnny Mnemonic <johnny@themnemonic.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "silcincludes.h"
24 /* Minimum time delay for log flushing calls (in seconds) */
25 #define SILC_LOG_FLUSH_MIN_DELAY 2
27 /* nice macro for looping through all logs -- makes the code more readable */
28 #define SILC_FOREACH_LOG(__x__) for (__x__ = 0; __x__ < SILC_LOG_MAX; __x__++)
30 /* Our working struct -- at the moment we keep it private, but this could
31 * change in the future */
32 struct SilcLogStruct {
41 typedef struct SilcLogStruct *SilcLog;
43 /* These are the known logging channels. We initialize this struct with most
44 * of the fields set to NULL, because we'll fill in those values at runtime. */
45 static struct SilcLogStruct silclogs[SILC_LOG_MAX] = {
46 {NULL, NULL, 0, "Info", SILC_LOG_INFO, NULL, NULL},
47 {NULL, NULL, 0, "Warning", SILC_LOG_WARNING, NULL, NULL},
48 {NULL, NULL, 0, "Error", SILC_LOG_ERROR, NULL, NULL},
49 {NULL, NULL, 0, "Fatal", SILC_LOG_FATAL, NULL, NULL},
52 /* If TRUE, log files will be flushed for each log input */
53 bool silc_log_quick = FALSE;
55 /* Set TRUE/FALSE to enable/disable debugging */
56 bool silc_debug = FALSE;
57 bool silc_debug_hexdump = FALSE;
59 /* Flush delay (in seconds) */
60 long silc_log_flushdelay = 300;
62 /* Regular pattern matching expression for the debug output */
63 char silc_log_debug_string[128];
65 /* Debug callbacks. If set, these are triggered for each specific output. */
66 static SilcLogDebugCb silc_log_debug_cb = NULL;
67 static void *silc_log_debug_context = NULL;
68 static SilcLogHexdumpCb silc_log_hexdump_cb = NULL;
69 static void *silc_log_hexdump_context = NULL;
71 /* Did we register already our functions to the scheduler? */
72 static bool silc_log_scheduled = FALSE;
73 static bool silc_log_no_init = FALSE;
75 /* This is only needed during starting up -- don't lose any logging message */
76 static bool silc_log_starting = TRUE;
78 /* The type wrapper utility. Translates a SilcLogType id to the corresponding
79 * logfile, or NULL if not found. */
80 static SilcLog silc_log_find_by_type(SilcLogType type)
82 /* this is not really needed, but i think it's more secure */
85 return &silclogs[SILC_LOG_INFO];
86 case SILC_LOG_WARNING:
87 return &silclogs[SILC_LOG_WARNING];
89 return &silclogs[SILC_LOG_ERROR];
91 return &silclogs[SILC_LOG_FATAL];
98 /* Given an open log file, checks the size and rotates it if there is a
99 * max size set lower then the current size */
100 static void silc_log_checksize(SilcLog log)
105 if (!log || !log->fp || !log->maxsize)
106 return; /* we are not interested */
108 if ((size = ftell(log->fp)) < 0) {
109 /* OMG, EBADF is here.. we'll try our best.. */
110 FILE *oldfp = log->fp;
111 fclose(oldfp); /* we can discard the error */
112 log->fp = NULL; /* make sure we don't get here recursively */
113 SILC_LOG_ERROR(("Error while checking size of the log file %s, fp=%p",
114 log->filename, oldfp));
117 if (size < log->maxsize) return;
120 fprintf(log->fp, "[%s] [%s] Cycling log file, over max "
121 "logsize (%lu kilobytes)\n",
122 silc_get_time(), log->typename, log->maxsize / 1024);
125 snprintf(newname, sizeof(newname), "%s.old", log->filename);
128 /* I heard the following syscall may cause portability issues, but I don't
129 * have any other solution since SILC library doesn't provide any other
130 * function like this. -Johnny */
131 rename(log->filename, newname);
132 if (!(log->fp = fopen(log->filename, "w")))
133 SILC_LOG_WARNING(("Couldn't reopen logfile %s for type \"%s\": %s",
134 log->filename, log->typename, strerror(errno)));
137 /* Reset a logging channel (close and reopen) */
139 static bool silc_log_reset(SilcLog log)
141 if (!log) return FALSE;
146 if (!log->filename) return FALSE;
147 if (!(log->fp = fopen(log->filename, "a+"))) {
148 SILC_LOG_WARNING(("Couldn't reset logfile %s for type \"%s\": %s",
149 log->filename, log->typename, strerror(errno)));
156 /* Internal timeout callback to flush log channels and check file sizes */
158 SILC_TASK_CALLBACK(silc_log_fflush_callback)
161 if (!silc_log_quick) {
162 silc_log_flush_all();
164 silc_log_checksize(&silclogs[u]);
166 silc_log_starting = FALSE;
167 if (silc_log_flushdelay < SILC_LOG_FLUSH_MIN_DELAY)
168 silc_log_flushdelay = SILC_LOG_FLUSH_MIN_DELAY;
169 silc_schedule_task_add((SilcSchedule) context, 0, silc_log_fflush_callback,
170 context, silc_log_flushdelay, 0, SILC_TASK_TIMEOUT,
171 SILC_TASK_PRI_NORMAL);
174 /* Outputs the log message to the first available channel. Channels are
175 * ordered by importance (see SilcLogType documentation).
176 * More important channels can be printed on less important ones, but not
179 void silc_log_output(SilcLogType type, char *string)
181 char *typename = NULL;
185 if ((type > SILC_LOG_MAX) || !(log = silc_log_find_by_type(type)))
188 /* Save the original typename, because even if we redirect the message
189 * to another channel we'll keep however the original channel name */
190 typename = log->typename;
192 /* If there is a custom callback set, use it and return. */
194 if ((*log->cb)(type, string, log->context))
198 if (!silc_log_scheduled) {
199 if (silc_log_no_init == FALSE) {
201 "Warning, trying to output without log files initialization, "
202 "log output is going to stderr\n");
203 silc_log_no_init = TRUE;
205 /* redirect output */
211 /* ok, now we have to find an open stream */
213 if (log && (fp = log->fp)) goto found;
214 if (type == 0) break;
215 log = silc_log_find_by_type(--type);
218 /* Couldn't find any open stream.. sorry :( */
219 SILC_LOG_DEBUG(("Warning! couldn't find any available log channel!"));
223 fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), typename, string);
224 if (silc_log_quick || silc_log_starting) {
226 if (log) /* we may have been redirected to stderr */
227 silc_log_checksize(log);
231 /* If debugging, also output the logging message to the console */
232 if (typename && silc_debug) {
233 fprintf(stderr, "[Logging] [%s] %s\n", typename, string);
239 /* returns an internally allocated pointer to a logging channel file */
240 char *silc_log_get_file(SilcLogType type)
244 if (!(log = silc_log_find_by_type(type)))
247 return log->filename;
251 /* Set and initialize the specified logging channel. See the API reference */
252 bool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
253 SilcSchedule scheduler)
258 log = silc_log_find_by_type(type);
262 SILC_LOG_DEBUG(("Setting \"%s\" file to %s (max size=%d)",
263 log->typename, filename, maxsize));
265 /* before assuming the new file, make sure we can open it */
267 if (!(fp = fopen(filename, "a+"))) {
268 fprintf(stderr, "warning: couldn't open log file %s: %s\n",
269 filename, strerror(errno));
274 /* clean the logging channel */
278 silc_free(log->filename);
279 log->filename = NULL;
284 log->filename = strdup(filename);
286 log->maxsize = maxsize;
290 if (silc_log_scheduled)
293 /* Add schedule hook with a short delay to make sure we'll use
295 silc_schedule_task_add(scheduler, 0, silc_log_fflush_callback,
296 (void *) scheduler, 10, 0,
297 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
298 silc_log_scheduled = TRUE;
304 /* Sets a log callback, set callback to NULL to return to default behaviour */
306 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context)
310 if (!(log = silc_log_find_by_type(type)))
314 log->context = context;
317 /* Resets log callbacks */
319 void silc_log_reset_callbacks()
322 SILC_FOREACH_LOG(u) {
323 silclogs[u].cb = NULL;
324 silclogs[u].context = NULL;
328 /* Flushes all opened logging channels */
330 void silc_log_flush_all() {
332 SILC_LOG_DEBUG(("Flushing all logs"));
333 SILC_FOREACH_LOG(u) {
335 fflush(silclogs[u].fp);
339 /* Resets all known logging channels */
341 void silc_log_reset_all() {
343 SILC_LOG_DEBUG(("Resetting all logs"));
345 silc_log_reset(&silclogs[u]);
346 /* Immediately flush any possible warning message */
347 silc_log_flush_all();
350 /* Outputs the debug message to stderr. */
352 void silc_log_output_debug(char *file, char *function,
353 int line, char *string)
358 if (silc_log_debug_string &&
359 !silc_string_regex_match(silc_log_debug_string, file) &&
360 !silc_string_regex_match(silc_log_debug_string, function))
363 if (silc_log_debug_cb) {
364 if ((*silc_log_debug_cb)(file, function, line, string,
365 silc_log_debug_context))
369 fprintf(stderr, "%s:%d: %s\n", function, line, string);
376 /* Hexdumps a message */
378 void silc_log_output_hexdump(char *file, char *function,
379 int line, void *data_in,
380 SilcUInt32 len, char *string)
384 unsigned char *data = (unsigned char *)data_in;
386 if (!silc_debug_hexdump)
389 if (silc_log_debug_string &&
390 !silc_string_regex_match(silc_log_debug_string, file) &&
391 !silc_string_regex_match(silc_log_debug_string, function))
394 if (silc_log_hexdump_cb) {
395 if ((*silc_log_hexdump_cb)(file, function, line, data_in, len, string,
396 silc_log_hexdump_context))
400 fprintf(stderr, "%s:%d: %s\n", function, line, string);
408 if ((len - pos) < 16 && (len - pos <= len - off))
418 fprintf(stderr, "%08X ", k++ * 16);
420 for (i = 0; i < count; i++) {
421 fprintf(stderr, "%02X ", data[pos + i]);
423 if ((i + 1) % 4 == 0)
424 fprintf(stderr, " ");
427 if (count && count < 16) {
430 for (j = 0; j < 16 - count; j++) {
431 fprintf(stderr, " ");
433 if ((j + count + 1) % 4 == 0)
434 fprintf(stderr, " ");
438 for (i = 0; i < count; i++) {
441 if (data[pos] < 32 || data[pos] >= 127)
446 fprintf(stderr, "%c", ch);
451 fprintf(stderr, "\n");
461 /* Sets debug callbacks */
463 void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
465 SilcLogHexdumpCb hexdump_cb,
466 void *hexdump_context)
468 silc_log_debug_cb = debug_cb;
469 silc_log_debug_context = debug_context;
470 silc_log_hexdump_cb = hexdump_cb;
471 silc_log_hexdump_context = hexdump_context;
474 /* Resets debug callbacks */
476 void silc_log_reset_debug_callbacks()
478 silc_log_debug_cb = NULL;
479 silc_log_debug_context = NULL;
480 silc_log_hexdump_cb = NULL;
481 silc_log_hexdump_context = NULL;
484 /* Set current debug string */
486 void silc_log_set_debug_string(const char *debug_string)
489 if ((strchr(debug_string, '(') && strchr(debug_string, ')')) ||
490 strchr(debug_string, '$'))
491 string = strdup(debug_string);
493 string = silc_string_regexify(debug_string);
494 strncpy(silc_log_debug_string, string, strlen(string));