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 int silc_debug = FALSE;
26 char *silc_debug_string = NULL;
28 /* SILC Log name strings. These strings are printed to the log file. */
29 const SilcLogTypeName silc_log_types[] =
31 { "Info", SILC_LOG_INFO },
32 { "Warning", SILC_LOG_WARNING },
33 { "Error", SILC_LOG_ERROR },
34 { "Fatal", SILC_LOG_FATAL },
40 char *log_warning_file;
44 uint32 log_warning_size;
45 uint32 log_error_size;
46 uint32 log_fatal_size;
48 /* Log callbacks. If these are set by the application these are used
49 instead of the default functions in this file. */
50 static SilcLogCb info_cb = NULL;
51 static SilcLogCb warning_cb = NULL;
52 static SilcLogCb error_cb = NULL;
53 static SilcLogCb fatal_cb = NULL;
55 /* Debug callbacks. If set these are used instead of default ones. */
56 static SilcDebugCb debug_cb = NULL;
57 static SilcDebugHexdumpCb debug_hexdump_cb = NULL;
59 /* Outputs the log message to what ever log file selected. */
61 void silc_log_output(const char *filename, uint32 maxsize,
62 SilcLogType type, char *string)
65 const SilcLogTypeName *np;
76 case SILC_LOG_WARNING:
78 (*warning_cb)(string);
102 /* Purge the log file if the max size is defined. */
104 fp = fopen(filename, "r");
108 filelen = fseek(fp, (off_t)0L, SEEK_END);
109 fseek(fp, (off_t)0L, SEEK_SET);
112 if (filelen >= maxsize)
119 /* Open the log file */
120 if ((fp = fopen(filename, "a+")) == NULL) {
121 fprintf(stderr, "warning: could not open log file "
122 "%s: %s\n", filename, strerror(errno));
123 fprintf(stderr, "warning: log messages will be displayed on "
129 /* Get the log type name */
130 for (np = silc_log_types; np->name; np++) {
131 if (np->type == type)
135 fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), np->name, string);
142 /* Outputs the debug message to stderr. */
144 void silc_log_output_debug(char *file, char *function,
145 int line, char *string)
152 if (silc_debug_string &&
153 (!silc_string_regex_match(silc_debug_string, file) &&
154 !silc_string_regex_match(silc_debug_string, function))) {
161 (*debug_cb)(file, function, line, string);
166 fprintf(stderr, "%s:%d: %s\n", function, line, string);
171 /* Hexdumps a message */
173 void silc_log_output_hexdump(char *file, char *function,
174 int line, void *data_in,
175 uint32 len, char *string)
179 unsigned char *data = (unsigned char *)data_in;
186 if (silc_debug_string &&
187 (!silc_string_regex_match(silc_debug_string, file) &&
188 !silc_string_regex_match(silc_debug_string, function))) {
193 if (debug_hexdump_cb)
195 (*debug_hexdump_cb)(file, function, line, data_in, len, string);
200 fprintf(stderr, "%s:%d: %s\n", function, line, string);
210 if ((len - pos) < 16 && (len - pos <= len - off))
220 fprintf(stderr, "%08X ", k++ * 16);
222 for (i = 0; i < count; i++) {
223 fprintf(stderr, "%02X ", data[pos + i]);
225 if ((i + 1) % 4 == 0)
226 fprintf(stderr, " ");
229 if (count && count < 16) {
232 for (j = 0; j < 16 - count; j++) {
233 fprintf(stderr, " ");
235 if ((j + count + 1) % 4 == 0)
236 fprintf(stderr, " ");
240 for (i = 0; i < count; i++) {
243 if (data[pos] < 32 || data[pos] >= 127)
248 fprintf(stderr, "%c", ch);
253 fprintf(stderr, "\n");
262 void silc_log_set_files(char *info, uint32 info_size,
263 char *warning, uint32 warning_size,
264 char *error, uint32 error_size,
265 char *fatal, uint32 fatal_size)
267 log_info_file = info;
268 log_warning_file = warning;
269 log_error_file = error;
270 log_fatal_file = fatal;
272 log_info_size = info_size;
273 log_warning_size = warning_size;
274 log_error_size = error_size;
275 log_fatal_size = fatal_size;
278 /* Sets log callbacks */
280 void silc_log_set_callbacks(SilcLogCb info, SilcLogCb warning,
281 SilcLogCb error, SilcLogCb fatal)
284 warning_cb = warning;
289 /* Resets log callbacks */
291 void silc_log_reset_callbacks()
293 info_cb = warning_cb = error_cb = fatal_cb = NULL;
296 /* Sets debug callbacks */
298 void silc_log_set_debug_callbacks(SilcDebugCb debug,
299 SilcDebugHexdumpCb debug_hexdump)
302 debug_hexdump_cb = debug_hexdump;
305 /* Resets debug callbacks */
307 void silc_log_reset_debug_callbacks()
310 debug_hexdump_cb = NULL;
313 /* Set current debug string */
315 void silc_log_set_debug_string(const char *debug_string)
317 silc_free(silc_debug_string);
318 silc_debug_string = silc_string_regexify(debug_string);