5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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 /* Set TRUE/FALSE to enable/disable debugging */
25 bool silc_debug = FALSE;
26 bool silc_debug_hexdump = FALSE;
27 char *silc_debug_string = NULL;
29 /* SILC Log name strings. These strings are printed to the log file. */
30 const SilcLogTypeName silc_log_types[] =
32 { "Info", SILC_LOG_INFO },
33 { "Warning", SILC_LOG_WARNING },
34 { "Error", SILC_LOG_ERROR },
35 { "Fatal", SILC_LOG_FATAL },
41 char *log_warning_file;
45 uint32 log_warning_size;
46 uint32 log_error_size;
47 uint32 log_fatal_size;
49 /* Log callbacks. If these are set by the application these are used
50 instead of the default functions in this file. */
51 static SilcLogCb info_cb = NULL;
52 static SilcLogCb warning_cb = NULL;
53 static SilcLogCb error_cb = NULL;
54 static SilcLogCb fatal_cb = NULL;
56 /* Debug callbacks. If set these are used instead of default ones. */
57 static SilcDebugCb debug_cb = NULL;
58 static SilcDebugHexdumpCb debug_hexdump_cb = NULL;
60 /* Outputs the log message to what ever log file selected. */
62 void silc_log_output(const char *filename, uint32 maxsize,
63 SilcLogType type, char *string)
66 const SilcLogTypeName *np;
77 case SILC_LOG_WARNING:
79 (*warning_cb)(string);
103 /* Purge the log file if the max size is defined. */
105 fp = fopen(filename, "r");
109 filelen = fseek(fp, (off_t)0L, SEEK_END);
110 fseek(fp, (off_t)0L, SEEK_SET);
113 if (filelen >= maxsize)
120 /* Open the log file */
121 if ((fp = fopen(filename, "a+")) == NULL) {
122 fprintf(stderr, "warning: could not open log file "
123 "%s: %s\n", filename, strerror(errno));
124 fprintf(stderr, "warning: log messages will be displayed on "
130 /* Get the log type name */
131 for (np = silc_log_types; np->name; np++) {
132 if (np->type == type)
136 fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), np->name, string);
143 /* Outputs the debug message to stderr. */
145 void silc_log_output_debug(char *file, char *function,
146 int line, char *string)
153 if (silc_debug_string &&
154 (!silc_string_regex_match(silc_debug_string, file) &&
155 !silc_string_regex_match(silc_debug_string, function))) {
162 (*debug_cb)(file, function, line, string);
167 fprintf(stderr, "%s:%d: %s\n", function, line, string);
172 /* Hexdumps a message */
174 void silc_log_output_hexdump(char *file, char *function,
175 int line, void *data_in,
176 uint32 len, char *string)
180 unsigned char *data = (unsigned char *)data_in;
182 if (!silc_debug_hexdump) {
187 if (silc_debug_string &&
188 (!silc_string_regex_match(silc_debug_string, file) &&
189 !silc_string_regex_match(silc_debug_string, function))) {
194 if (debug_hexdump_cb)
196 (*debug_hexdump_cb)(file, function, line, data_in, len, string);
201 fprintf(stderr, "%s:%d: %s\n", function, line, string);
211 if ((len - pos) < 16 && (len - pos <= len - off))
221 fprintf(stderr, "%08X ", k++ * 16);
223 for (i = 0; i < count; i++) {
224 fprintf(stderr, "%02X ", data[pos + i]);
226 if ((i + 1) % 4 == 0)
227 fprintf(stderr, " ");
230 if (count && count < 16) {
233 for (j = 0; j < 16 - count; j++) {
234 fprintf(stderr, " ");
236 if ((j + count + 1) % 4 == 0)
237 fprintf(stderr, " ");
241 for (i = 0; i < count; i++) {
244 if (data[pos] < 32 || data[pos] >= 127)
249 fprintf(stderr, "%c", ch);
254 fprintf(stderr, "\n");
263 void silc_log_set_files(char *info, uint32 info_size,
264 char *warning, uint32 warning_size,
265 char *error, uint32 error_size,
266 char *fatal, uint32 fatal_size)
268 log_info_file = info;
269 log_warning_file = warning;
270 log_error_file = error;
271 log_fatal_file = fatal;
273 log_info_size = info_size;
274 log_warning_size = warning_size;
275 log_error_size = error_size;
276 log_fatal_size = fatal_size;
279 /* Sets log callbacks */
281 void silc_log_set_callbacks(SilcLogCb info, SilcLogCb warning,
282 SilcLogCb error, SilcLogCb fatal)
285 warning_cb = warning;
290 /* Resets log callbacks */
292 void silc_log_reset_callbacks()
294 info_cb = warning_cb = error_cb = fatal_cb = NULL;
297 /* Sets debug callbacks */
299 void silc_log_set_debug_callbacks(SilcDebugCb debug,
300 SilcDebugHexdumpCb debug_hexdump)
303 debug_hexdump_cb = debug_hexdump;
306 /* Resets debug callbacks */
308 void silc_log_reset_debug_callbacks()
311 debug_hexdump_cb = NULL;
314 /* Set current debug string */
316 void silc_log_set_debug_string(const char *debug_string)
318 silc_free(silc_debug_string);
319 if (strchr(debug_string, '(') &&
320 strchr(debug_string, ')'))
321 silc_debug_string = strdup(debug_string);
323 silc_debug_string = silc_string_regexify(debug_string);