updates.
[silc.git] / lib / silcutil / silclog.c
1 /*
2
3   silclog.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 Pekka Riikonen
8
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.
13   
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.
18
19 */
20 /* $Id$ */
21
22 #include "silcincludes.h"
23
24 /* Set TRUE/FALSE to enable/disable debugging */
25 int silc_debug = FALSE;
26
27 /* SILC Log name strings. These strings are printed to the log file. */
28 const SilcLogTypeName silc_log_types[] =
29 {
30   { "Info", SILC_LOG_INFO },
31   { "Warning", SILC_LOG_WARNING },
32   { "Error", SILC_LOG_ERROR },
33   { "Fatal", SILC_LOG_FATAL },
34
35   { NULL, -1 },
36 };
37
38 char *log_info_file;
39 char *log_warning_file;
40 char *log_error_file;
41 char *log_fatal_file;
42 uint32 log_info_size;
43 uint32 log_warning_size;
44 uint32 log_error_size;
45 uint32 log_fatal_size;
46
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;
53
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;
57
58 /* Outputs the log message to what ever log file selected. */
59
60 void silc_log_output(const char *filename, uint32 maxsize,
61                      SilcLogType type, char *string)
62 {
63   FILE *fp;
64   const SilcLogTypeName *np;
65
66   switch(type)
67     {
68     case SILC_LOG_INFO:
69       if (info_cb) {
70         (*info_cb)(string);
71         silc_free(string);
72         return;
73       }
74       break;
75     case SILC_LOG_WARNING:
76       if (warning_cb) {
77         (*warning_cb)(string);
78         silc_free(string);
79         return;
80       }
81       break;
82     case SILC_LOG_ERROR:
83       if (error_cb) {
84         (*error_cb)(string);
85         silc_free(string);
86         return;
87       }
88       break;
89     case SILC_LOG_FATAL:
90       if (fatal_cb) {
91         (*fatal_cb)(string);
92         silc_free(string);
93         return;
94       }
95       break;
96     }
97
98   if (!filename)
99     fp = stderr;
100
101   if (filename) {
102     /* Purge the log file if the max size is defined. */
103     if (maxsize) {
104       fp = fopen(filename, "r");
105       if (fp) {
106         int filelen;
107         
108         filelen = fseek(fp, (off_t)0L, SEEK_END);
109         fseek(fp, (off_t)0L, SEEK_SET);  
110         
111         /* Purge? */
112         if (filelen >= maxsize)
113           unlink(filename);
114         
115         fclose(fp);
116       }
117     }
118     
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 "
124               "the screen\n");
125       fp = stderr;
126     }
127   }
128
129   /* Get the log type name */
130   for (np = silc_log_types; np->name; np++) {
131     if (np->type == type)
132       break;
133   }
134
135   fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), np->name, string);
136   fflush(fp);
137   if (fp != stderr)
138     fclose(fp);
139   silc_free(string);
140 }
141
142 /* Outputs the debug message to stderr. */
143
144 void silc_log_output_debug(char *file, char *function, 
145                            int line, char *string)
146 {
147   if (!silc_debug) {
148     silc_free(string);
149     return;
150   }
151
152   if (debug_cb)
153     {
154       (*debug_cb)(file, function, line, string);
155       silc_free(string);
156       return;
157     }
158
159   fprintf(stderr, "%s:%d: %s\n", function, line, string);
160   fflush(stderr);
161   silc_free(string);
162 }
163
164 /* Hexdumps a message */
165
166 void silc_log_output_hexdump(char *file, char *function, 
167                              int line, void *data_in,
168                              uint32 len, char *string)
169 {
170   int i, k;
171   int off, pos, count;
172   unsigned char *data = (unsigned char *)data_in;
173
174   if (!silc_debug) {
175     silc_free(string);
176     return;
177   }
178
179   if (debug_hexdump_cb)
180     {
181       (*debug_hexdump_cb)(file, function, line, data_in, len, string);
182       silc_free(string);
183       return;
184     }
185
186   fprintf(stderr, "%s:%d: %s\n", function, line, string);
187   silc_free(string);
188
189   k = 0;
190   off = len % 16;
191   pos = 0;
192   count = 16;
193   while (1) {
194
195     if (off) {
196       if ((len - pos) < 16 && (len - pos <= len - off))
197         count = off;
198     } else {
199       if (pos == len)
200         count = 0;
201     }
202     if (off == len)
203       count = len;
204
205     if (count)
206       fprintf(stderr, "%08X  ", k++ * 16);
207
208     for (i = 0; i < count; i++) {
209       fprintf(stderr, "%02X ", data[pos + i]);
210       
211       if ((i + 1) % 4 == 0)
212         fprintf(stderr, " ");
213     }
214
215     if (count && count < 16) {
216       int j;
217       
218       for (j = 0; j < 16 - count; j++) {
219         fprintf(stderr, "   ");
220
221         if ((j + count + 1) % 4 == 0)
222           fprintf(stderr, " ");
223       }
224     }
225   
226     for (i = 0; i < count; i++) {
227       char ch;
228       
229       if (data[pos] < 32 || data[pos] >= 127)
230         ch = '.';
231       else
232         ch = data[pos];
233
234       fprintf(stderr, "%c", ch);
235       pos++;
236     }
237
238     if (count)
239       fprintf(stderr, "\n");
240
241     if (count < 16)
242       break;
243   }
244 }
245
246 /* Sets log files */
247
248 void silc_log_set_files(char *info, uint32 info_size, 
249                         char *warning, uint32 warning_size,
250                         char *error, uint32 error_size,
251                         char *fatal, uint32 fatal_size)
252 {
253   log_info_file = info;
254   log_warning_file = warning;
255   log_error_file = error;
256   log_fatal_file = fatal;
257
258   log_info_size = info_size;
259   log_warning_size = warning_size;
260   log_error_size = error_size;
261   log_fatal_size = fatal_size;
262 }
263
264 /* Sets log callbacks */
265
266 void silc_log_set_callbacks(SilcLogCb info, SilcLogCb warning,
267                             SilcLogCb error, SilcLogCb fatal)
268 {
269   info_cb = info;
270   warning_cb = warning;
271   error_cb = error;
272   fatal_cb = fatal;
273 }
274
275 /* Resets log callbacks */
276
277 void silc_log_reset_callbacks()
278 {
279   info_cb = warning_cb = error_cb = fatal_cb = NULL;
280 }
281
282 /* Sets debug callbacks */
283
284 void silc_log_set_debug_callbacks(SilcDebugCb debug, 
285                                   SilcDebugHexdumpCb debug_hexdump)
286 {
287   debug_cb = debug;
288   debug_hexdump_cb = debug_hexdump;
289 }
290
291 /* Resets debug callbacks */
292
293 void silc_log_reset_debug_callbacks()
294 {
295   debug_cb = NULL;
296   debug_hexdump_cb = NULL;
297 }