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 = NULL;
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 less 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 */
107 if ((size = ftell(log->fp)) < 0) {
108 /* OMG, EBADF is here.. we'll try our best.. */
109 FILE *oldfp = log->fp;
110 fclose(oldfp); /* we can discard the error */
111 log->fp = NULL; /* make sure we don't get here recursively */
112 SILC_LOG_ERROR(("Error while checking size of the log file %s, fp=%d",
113 log->filename, oldfp));
116 if (size < log->maxsize) return;
119 fprintf(log->fp, "[%s] [%s] Cycling log file, over max "
120 "logsize (%lu kilobytes)\n",
121 silc_get_time(), log->typename, log->maxsize / 1024);
124 snprintf(newname, sizeof(newname), "%s.old", log->filename);
127 /* I heard the following syscall may cause portability issues, but I don't
128 * have any other solution since SILC library doesn't provide any other
129 * function like this. -Johnny */
130 rename(log->filename, newname);
131 if (!(log->fp = fopen(log->filename, "w")))
132 SILC_LOG_WARNING(("Couldn't reopen logfile %s for type \"%s\": %s",
133 log->filename, log->typename, strerror(errno)));
136 /* Reset a logging channel (close and reopen) */
138 static bool silc_log_reset(SilcLog log)
140 if (!log) return FALSE;
145 if (!log->filename) return FALSE;
146 if (!(log->fp = fopen(log->filename, "a+"))) {
147 SILC_LOG_WARNING(("Couldn't reset logfile %s for type \"%s\": %s",
148 log->filename, log->typename, strerror(errno)));
154 /* Internal timeout callback to flush log channels and check file sizes */
156 SILC_TASK_CALLBACK(silc_log_fflush_callback)
159 if (!silc_log_quick) {
160 silc_log_flush_all();
162 silc_log_checksize(&silclogs[u]);
164 silc_log_starting = FALSE;
165 if (silc_log_flushdelay < SILC_LOG_FLUSH_MIN_DELAY)
166 silc_log_flushdelay = SILC_LOG_FLUSH_MIN_DELAY;
167 silc_schedule_task_add((SilcSchedule) context, 0, silc_log_fflush_callback,
168 context, silc_log_flushdelay, 0, SILC_TASK_TIMEOUT,
169 SILC_TASK_PRI_NORMAL);
172 /* Outputs the log message to the first available channel. Channels are
173 * ordered by importance (see SilcLogType documentation).
174 * More important channels can be printed on less important ones, but not
177 void silc_log_output(SilcLogType type, char *string)
179 char *typename = NULL;
183 if ((type > SILC_LOG_MAX) || !(log = silc_log_find_by_type(type)))
186 /* Save the original typename, because even if we redirect the message
187 * to another channel we'll keep however the original channel name */
188 typename = log->typename;
190 /* If there is a custom callback set, use it and return. */
192 if ((*log->cb)(type, string, log->context))
196 if (!silc_log_scheduled) {
197 if (silc_log_no_init == FALSE) {
199 "Warning, trying to output without log files initialization, "
200 "log output is going to stderr\n");
201 silc_log_no_init = TRUE;
203 /* redirect output */
209 /* ok, now we have to find an open stream */
211 if (log && (fp = log->fp)) goto found;
212 if (type == 0) break;
213 log = silc_log_find_by_type(--type);
216 /* Couldn't find any open stream.. sorry :( */
217 SILC_LOG_DEBUG(("Warning! couldn't find any available log channel!"));
221 fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), typename, string);
222 if (silc_log_quick || silc_log_starting) {
225 silc_log_checksize(log);
229 /* If debugging, also output the logging message to the console */
230 if (typename && silc_debug) {
231 fprintf(stderr, "[Logging] [%s] %s\n", typename, string);
237 /* returns an internally allocated pointer to a logging channel file */
238 char *silc_log_get_file(SilcLogType type)
242 if (!(log = silc_log_find_by_type(type)))
245 return log->filename;
249 /* Set and initialize the specified logging channel. See the API reference */
250 bool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
251 SilcSchedule scheduler)
256 log = silc_log_find_by_type(type);
257 if (!log || !scheduler)
260 SILC_LOG_DEBUG(("Setting \"%s\" file to %s (max size=%d)",
261 log->typename, filename, maxsize));
263 /* before assuming the new file, make sure we can open it */
265 if (!(fp = fopen(filename, "a+"))) {
266 fprintf(stderr, "warning: couldn't open log file %s: %s\n",
267 filename, strerror(errno));
272 /* remove old file */
278 silc_free(log->filename);
279 log->filename = NULL;
284 log->filename = strdup(filename);
286 log->maxsize = maxsize;
289 if (silc_log_scheduled)
292 /* add schedule hook with a short delay to make sure we'll use right delay */
293 silc_schedule_task_add(scheduler, 0, silc_log_fflush_callback,
294 (void *) scheduler, 10, 0,
295 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
297 silc_log_scheduled = TRUE;
302 /* Sets a log callback, set callback to NULL to return to default behaviour */
304 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context)
308 if (!(log = silc_log_find_by_type(type)))
312 log->context = context;
315 /* Resets log callbacks */
317 void silc_log_reset_callbacks()
320 SILC_FOREACH_LOG(u) {
321 silclogs[u].cb = NULL;
322 silclogs[u].context = NULL;
326 /* Flushes all opened logging channels */
328 void silc_log_flush_all() {
330 SILC_LOG_DEBUG(("Flushing all logs"));
331 SILC_FOREACH_LOG(u) {
333 fflush(silclogs[u].fp);
337 /* Resets all known logging channels */
339 void silc_log_reset_all() {
341 SILC_LOG_DEBUG(("Resetting all logs"));
343 silc_log_reset(&silclogs[u]);
344 /* Immediately flush any possible warning message */
345 silc_log_flush_all();
348 /* Outputs the debug message to stderr. */
350 void silc_log_output_debug(char *file, char *function,
351 int line, char *string)
356 if (silc_log_debug_string &&
357 !silc_string_regex_match(silc_log_debug_string, file) &&
358 !silc_string_regex_match(silc_log_debug_string, function))
361 if (silc_log_debug_cb) {
362 if ((*silc_log_debug_cb)(file, function, line, string,
363 silc_log_debug_context))
367 fprintf(stderr, "%s:%d: %s\n", function, line, string);
374 /* Hexdumps a message */
376 void silc_log_output_hexdump(char *file, char *function,
377 int line, void *data_in,
378 SilcUInt32 len, char *string)
382 unsigned char *data = (unsigned char *)data_in;
384 if (!silc_debug_hexdump)
387 if (silc_log_debug_string &&
388 !silc_string_regex_match(silc_log_debug_string, file) &&
389 !silc_string_regex_match(silc_log_debug_string, function))
392 if (silc_log_hexdump_cb) {
393 if ((*silc_log_hexdump_cb)(file, function, line, data_in, len, string,
394 silc_log_hexdump_context))
398 fprintf(stderr, "%s:%d: %s\n", function, line, string);
406 if ((len - pos) < 16 && (len - pos <= len - off))
416 fprintf(stderr, "%08X ", k++ * 16);
418 for (i = 0; i < count; i++) {
419 fprintf(stderr, "%02X ", data[pos + i]);
421 if ((i + 1) % 4 == 0)
422 fprintf(stderr, " ");
425 if (count && count < 16) {
428 for (j = 0; j < 16 - count; j++) {
429 fprintf(stderr, " ");
431 if ((j + count + 1) % 4 == 0)
432 fprintf(stderr, " ");
436 for (i = 0; i < count; i++) {
439 if (data[pos] < 32 || data[pos] >= 127)
444 fprintf(stderr, "%c", ch);
449 fprintf(stderr, "\n");
459 /* Sets debug callbacks */
461 void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
463 SilcLogHexdumpCb hexdump_cb,
464 void *hexdump_context)
466 silc_log_debug_cb = debug_cb;
467 silc_log_debug_context = debug_context;
468 silc_log_hexdump_cb = hexdump_cb;
469 silc_log_hexdump_context = hexdump_context;
472 /* Resets debug callbacks */
474 void silc_log_reset_debug_callbacks()
476 silc_log_debug_cb = NULL;
477 silc_log_debug_context = NULL;
478 silc_log_hexdump_cb = NULL;
479 silc_log_hexdump_context = NULL;
482 /* Set current debug string */
484 void silc_log_set_debug_string(const char *debug_string)
486 silc_free(silc_log_debug_string);
487 if ((strchr(debug_string, '(') &&
488 strchr(debug_string, ')')) ||
489 strchr(debug_string, '$'))
490 silc_log_debug_string = strdup(debug_string);
492 silc_log_debug_string = silc_string_regexify(debug_string);