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;
27 /* SILC Log name strings. These strings are printed to the log file. */
28 const SilcLogTypeName silc_log_types[] =
30 { "Info", SILC_LOG_INFO },
31 { "Warning", SILC_LOG_WARNING },
32 { "Error", SILC_LOG_ERROR },
33 { "Fatal", SILC_LOG_FATAL },
39 char *log_warning_file;
43 uint32 log_warning_size;
44 uint32 log_error_size;
45 uint32 log_fatal_size;
47 /* Log callbacks. If these are set by the application these are used
48 instead of the default functions in this file. */
49 static SilcLogCb info_cb = NULL;
50 static SilcLogCb warning_cb = NULL;
51 static SilcLogCb error_cb = NULL;
52 static SilcLogCb fatal_cb = NULL;
54 /* Debug callbacks. If set these are used instead of default ones. */
55 static SilcDebugCb debug_cb = NULL;
56 static SilcDebugHexdumpCb debug_hexdump_cb = NULL;
58 /* Outputs the log message to what ever log file selected. */
60 void silc_log_output(const char *filename, uint32 maxsize,
61 SilcLogType type, char *string)
64 const SilcLogTypeName *np;
75 case SILC_LOG_WARNING:
77 (*warning_cb)(string);
101 /* Purge the log file if the max size is defined. */
103 fp = fopen(filename, "r");
107 filelen = fseek(fp, (off_t)0L, SEEK_END);
108 fseek(fp, (off_t)0L, SEEK_SET);
111 if (filelen >= maxsize)
118 /* Open the log file */
119 if ((fp = fopen(filename, "a+")) == NULL) {
120 fprintf(stderr, "warning: could not open log file "
121 "%s: %s\n", filename, strerror(errno));
122 fprintf(stderr, "warning: log messages will be displayed on the screen\n");
126 /* Get the log type name */
127 for(np = silc_log_types; np->name; np++) {
128 if (np->type == type)
132 fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), np->name, string);
139 /* Outputs the debug message to stderr. */
141 void silc_log_output_debug(char *file, char *function,
142 int line, char *string)
151 (*debug_cb)(file, function, line, string);
156 fprintf(stderr, "%s:%d: %s\n", function, line, string);
161 /* Hexdumps a message */
163 void silc_log_output_hexdump(char *file, char *function,
164 int line, void *data_in,
165 uint32 len, char *string)
169 unsigned char *data = (unsigned char *)data_in;
176 if (debug_hexdump_cb)
178 (*debug_hexdump_cb)(file, function, line, data_in, len, string);
183 fprintf(stderr, "%s:%d: %s\n", function, line, string);
193 if ((len - pos) < 16 && (len - pos <= len - off))
203 fprintf(stderr, "%08X ", k++ * 16);
205 for (i = 0; i < count; i++) {
206 fprintf(stderr, "%02X ", data[pos + i]);
208 if ((i + 1) % 4 == 0)
209 fprintf(stderr, " ");
212 if (count && count < 16) {
215 for (j = 0; j < 16 - count; j++) {
216 fprintf(stderr, " ");
218 if ((j + count + 1) % 4 == 0)
219 fprintf(stderr, " ");
223 for (i = 0; i < count; i++) {
226 if (data[pos] < 32 || data[pos] >= 127)
231 fprintf(stderr, "%c", ch);
236 fprintf(stderr, "\n");
245 void silc_log_set_files(char *info, uint32 info_size,
246 char *warning, uint32 warning_size,
247 char *error, uint32 error_size,
248 char *fatal, uint32 fatal_size)
250 log_info_file = info;
251 log_warning_file = warning;
252 log_error_file = error;
253 log_fatal_file = fatal;
255 log_info_size = info_size;
256 log_warning_size = warning_size;
257 log_error_size = error_size;
258 log_fatal_size = fatal_size;
261 /* Sets log callbacks */
263 void silc_log_set_callbacks(SilcLogCb info, SilcLogCb warning,
264 SilcLogCb error, SilcLogCb fatal)
267 warning_cb = warning;
272 /* Resets log callbacks */
274 void silc_log_reset_callbacks()
276 info_cb = warning_cb = error_cb = fatal_cb = NULL;
279 /* Sets debug callbacks */
281 void silc_log_set_debug_callbacks(SilcDebugCb debug,
282 SilcDebugHexdumpCb debug_hexdump)
285 debug_hexdump_cb = debug_hexdump;
288 /* Resets debug callbacks */
290 void silc_log_reset_debug_callbacks()
293 debug_hexdump_cb = NULL;