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 */
44 static struct SilcLogStruct silclogs[SILC_LOG_MAX] = {
45 {NULL, NULL, 0, "Info", SILC_LOG_INFO, NULL, NULL},
46 {NULL, NULL, 0, "Warning", SILC_LOG_WARNING, NULL, NULL},
47 {NULL, NULL, 0, "Error", SILC_LOG_ERROR, NULL, NULL},
48 {NULL, NULL, 0, "Fatal", SILC_LOG_FATAL, NULL, NULL},
51 /* If TRUE, log files will be flushed for each log input */
52 bool silc_log_quick = FALSE;
54 /* Set TRUE/FALSE to enable/disable debugging */
55 bool silc_debug = FALSE;
56 bool silc_debug_hexdump = FALSE;
59 long silc_log_flushdelay = 300;
61 /* Regular pattern matching expression for the debug output */
62 char *silc_log_debug_string = NULL;
64 /* Debug callbacks. If set these are used instead of default ones. */
65 static SilcLogDebugCb silc_log_debug_cb = NULL;
66 static void *silc_log_debug_context = NULL;
67 static SilcLogHexdumpCb silc_log_hexdump_cb = NULL;
68 static void *silc_log_hexdump_context = NULL;
70 /* Did we register already our functions to the scheduler? */
71 static bool silc_log_scheduled = FALSE;
72 static bool silc_log_no_init = FALSE;
74 /* This is only needed during starting up -- don't lose any logging */
75 static bool silc_log_starting = TRUE;
77 /* The type wrapper utility. Translates a SilcLogType id to the corresponding
78 * logfile, or NULL if not found. */
79 static SilcLog silc_log_find_by_type(SilcLogType type)
81 /* this is not really needed, but i think it's more secure */
84 return &silclogs[SILC_LOG_INFO];
85 case SILC_LOG_WARNING:
86 return &silclogs[SILC_LOG_WARNING];
88 return &silclogs[SILC_LOG_ERROR];
90 return &silclogs[SILC_LOG_FATAL];
97 /* Given an open log file, checks the size and rotates it if there is a
98 * max size set less then the current size */
99 static void silc_log_checksize(SilcLog log)
104 if (!log || !log->fp || !log->maxsize)
105 return; /* we are not interested */
106 if ((size = ftell(log->fp)) < 0) {
107 /* OMG, EBADF is here.. we'll try our best.. */
108 FILE *oldfp = log->fp;
109 fclose(oldfp); /* we can discard the error */
110 log->fp = NULL; /* make sure we don't get here recursively */
111 SILC_LOG_ERROR(("Error while checking size of the log file %s, fp=%d",
112 log->filename, oldfp));
115 if (size < log->maxsize) return;
118 fprintf(log->fp, "[%s] [%s] Cycling log file, over max "
119 "logsize (%lu kilobytes)\n",
120 silc_get_time(), log->typename, log->maxsize / 1024);
123 snprintf(newname, sizeof(newname), "%s.old", log->filename);
126 /* I heard the following syscall may cause portability issues, but I don't
127 * have any other solution since SILC library doesn't provide any other
128 * function like this. -Johnny */
129 rename(log->filename, newname);
130 if (!(log->fp = fopen(log->filename, "w")))
131 SILC_LOG_WARNING(("Couldn't reopen logfile %s for type \"%s\": %s",
132 log->filename, log->typename, strerror(errno)));
135 /* Reset a logging channel (close and reopen) */
137 static bool silc_log_reset(SilcLog log)
139 if (!log) return FALSE;
144 if (!log->filename) return FALSE;
145 if (!(log->fp = fopen(log->filename, "a+"))) {
146 SILC_LOG_WARNING(("Couldn't reset logfile %s for type \"%s\": %s",
147 log->filename, log->typename, strerror(errno)));
153 /* Internal timeout callback to flush log channels and check file sizes */
155 SILC_TASK_CALLBACK(silc_log_fflush_callback)
158 if (!silc_log_quick) {
159 silc_log_flush_all();
161 silc_log_checksize(&silclogs[u]);
163 silc_log_starting = FALSE;
164 if (silc_log_flushdelay < SILC_LOG_FLUSH_MIN_DELAY)
165 silc_log_flushdelay = SILC_LOG_FLUSH_MIN_DELAY;
166 silc_schedule_task_add((SilcSchedule) context, 0, silc_log_fflush_callback,
167 context, silc_log_flushdelay, 0, SILC_TASK_TIMEOUT,
168 SILC_TASK_PRI_NORMAL);
171 /* Outputs the log message to the first available channel. Channels are
172 * ordered by importance (see SilcLogType documentation).
173 * More importants channels can be printed on less important ones, but not
176 void silc_log_output(SilcLogType type, char *string)
182 if ((type > SILC_LOG_MAX) || !(log = silc_log_find_by_type(type)))
185 /* If there is a custom callback set, use it and return. */
187 if ((*log->cb)(type, string, log->context))
191 /* save the original typename, because if we redirect the channel we
192 * keep however the original destination channel name */
193 typename = log->typename;
195 if (!silc_log_scheduled) {
196 if (silc_log_no_init == FALSE) {
198 "Warning, trying to output without log files initialization, "
199 "log output is going to stderr\n");
200 silc_log_no_init = TRUE;
202 /* redirect output */
208 /* ok, now we have to find an open stream */
210 if (log && (fp = log->fp)) goto found;
211 if (type == 0) break;
212 log = silc_log_find_by_type(--type);
215 /* Couldn't find any open stream.. sorry :( */
216 SILC_LOG_DEBUG(("Warning! couldn't find any available log channel!"));
220 fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), typename, string);
221 if (silc_log_quick || silc_log_starting) {
224 silc_log_checksize(log);
231 /* returns an internally allocated pointer to a logging channel file */
232 char *silc_log_get_file(SilcLogType type)
236 if (!(log = silc_log_find_by_type(type)))
239 return log->filename;
243 /* Set and initialize the specified logging channel. See the API reference */
244 bool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
245 SilcSchedule scheduler)
250 log = silc_log_find_by_type(type);
251 if (!log || !scheduler)
254 SILC_LOG_DEBUG(("Setting \"%s\" file to %s (max size=%d)",
255 log->typename, filename, maxsize));
257 /* before assuming the new file, make sure we can open it */
259 if (!(fp = fopen(filename, "a+"))) {
260 fprintf(stderr, "warning: couldn't open log file %s: %s\n",
261 filename, strerror(errno));
266 /* remove old file */
272 silc_free(log->filename);
273 log->filename = NULL;
278 log->filename = strdup(filename);
280 log->maxsize = maxsize;
283 if (silc_log_scheduled)
286 /* add schedule hook with a short delay to make sure we'll use right delay */
287 silc_schedule_task_add(scheduler, 0, silc_log_fflush_callback,
288 (void *) scheduler, 10, 0,
289 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
291 silc_log_scheduled = TRUE;
296 /* Sets a log callback, set callback to NULL to return to default behaviour */
298 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context)
302 if (!(log = silc_log_find_by_type(type)))
306 log->context = context;
309 /* Resets log callbacks */
311 void silc_log_reset_callbacks()
314 SILC_FOREACH_LOG(u) {
315 silclogs[u].cb = NULL;
316 silclogs[u].context = NULL;
320 /* Flushes all opened logging channels */
322 void silc_log_flush_all() {
324 SILC_LOG_DEBUG(("Flushing all logs"));
325 SILC_FOREACH_LOG(u) {
327 fflush(silclogs[u].fp);
331 /* Resets all known logging channels */
333 void silc_log_reset_all() {
335 SILC_LOG_DEBUG(("Resetting all logs"));
337 silc_log_reset(&silclogs[u]);
340 /* Outputs the debug message to stderr. */
342 void silc_log_output_debug(char *file, char *function,
343 int line, char *string)
348 if (silc_log_debug_string &&
349 !silc_string_regex_match(silc_log_debug_string, file) &&
350 !silc_string_regex_match(silc_log_debug_string, function))
353 if (silc_log_debug_cb) {
354 if ((*silc_log_debug_cb)(file, function, line, string,
355 silc_log_debug_context))
359 fprintf(stderr, "%s:%d: %s\n", function, line, string);
366 /* Hexdumps a message */
368 void silc_log_output_hexdump(char *file, char *function,
369 int line, void *data_in,
370 SilcUInt32 len, char *string)
374 unsigned char *data = (unsigned char *)data_in;
376 if (!silc_debug_hexdump)
379 if (silc_log_debug_string &&
380 !silc_string_regex_match(silc_log_debug_string, file) &&
381 !silc_string_regex_match(silc_log_debug_string, function))
384 if (silc_log_hexdump_cb) {
385 if ((*silc_log_hexdump_cb)(file, function, line, data_in, len, string,
386 silc_log_hexdump_context))
390 fprintf(stderr, "%s:%d: %s\n", function, line, string);
398 if ((len - pos) < 16 && (len - pos <= len - off))
408 fprintf(stderr, "%08X ", k++ * 16);
410 for (i = 0; i < count; i++) {
411 fprintf(stderr, "%02X ", data[pos + i]);
413 if ((i + 1) % 4 == 0)
414 fprintf(stderr, " ");
417 if (count && count < 16) {
420 for (j = 0; j < 16 - count; j++) {
421 fprintf(stderr, " ");
423 if ((j + count + 1) % 4 == 0)
424 fprintf(stderr, " ");
428 for (i = 0; i < count; i++) {
431 if (data[pos] < 32 || data[pos] >= 127)
436 fprintf(stderr, "%c", ch);
441 fprintf(stderr, "\n");
451 /* Sets debug callbacks */
453 void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
455 SilcLogHexdumpCb hexdump_cb,
456 void *hexdump_context)
458 silc_log_debug_cb = debug_cb;
459 silc_log_debug_context = debug_context;
460 silc_log_hexdump_cb = hexdump_cb;
461 silc_log_hexdump_context = hexdump_context;
464 /* Resets debug callbacks */
466 void silc_log_reset_debug_callbacks()
468 silc_log_debug_cb = NULL;
469 silc_log_debug_context = NULL;
470 silc_log_hexdump_cb = NULL;
471 silc_log_hexdump_context = NULL;
474 /* Set current debug string */
476 void silc_log_set_debug_string(const char *debug_string)
478 silc_free(silc_log_debug_string);
479 if ((strchr(debug_string, '(') &&
480 strchr(debug_string, ')')) ||
481 strchr(debug_string, '$'))
482 silc_log_debug_string = strdup(debug_string);
484 silc_log_debug_string = silc_string_regexify(debug_string);