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   else {
101     /* Purge the log file if the max size is defined. */
102     if (maxsize) {
103       fp = fopen(filename, "r");
104       if (fp) {
105         int filelen;
106         
107         filelen = fseek(fp, (off_t)0L, SEEK_END);
108         fseek(fp, (off_t)0L, SEEK_SET);  
109         
110         /* Purge? */
111         if (filelen >= maxsize)
112           unlink(filename);
113         
114         fclose(fp);
115       }
116     }
117     
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 "
123               "the screen\n");
124       fp = stderr;
125     }
126   }
127
128   /* Get the log type name */
129   for (np = silc_log_types; np->name; np++) {
130     if (np->type == type)
131       break;
132   }
133
134   fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), np->name, string);
135   fflush(fp);
136   if (fp != stderr)
137     fclose(fp);
138   silc_free(string);
139 }
140
141 /* Outputs the debug message to stderr. */
142
143 void silc_log_output_debug(char *file, char *function, 
144                            int line, char *string)
145 {
146   if (!silc_debug) {
147     silc_free(string);
148     return;
149   }
150
151   if (debug_cb)
152     {
153       (*debug_cb)(file, function, line, string);
154       silc_free(string);
155       return;
156     }
157
158   fprintf(stderr, "%s:%d: %s\n", function, line, string);
159   fflush(stderr);
160   silc_free(string);
161 }
162
163 /* Hexdumps a message */
164
165 void silc_log_output_hexdump(char *file, char *function, 
166                              int line, void *data_in,
167                              uint32 len, char *string)
168 {
169   int i, k;
170   int off, pos, count;
171   unsigned char *data = (unsigned char *)data_in;
172
173   if (!silc_debug) {
174     silc_free(string);
175     return;
176   }
177
178   if (debug_hexdump_cb)
179     {
180       (*debug_hexdump_cb)(file, function, line, data_in, len, string);
181       silc_free(string);
182       return;
183     }
184
185   fprintf(stderr, "%s:%d: %s\n", function, line, string);
186   silc_free(string);
187
188   k = 0;
189   off = len % 16;
190   pos = 0;
191   count = 16;
192   while (1) {
193
194     if (off) {
195       if ((len - pos) < 16 && (len - pos <= len - off))
196         count = off;
197     } else {
198       if (pos == len)
199         count = 0;
200     }
201     if (off == len)
202       count = len;
203
204     if (count)
205       fprintf(stderr, "%08X  ", k++ * 16);
206
207     for (i = 0; i < count; i++) {
208       fprintf(stderr, "%02X ", data[pos + i]);
209       
210       if ((i + 1) % 4 == 0)
211         fprintf(stderr, " ");
212     }
213
214     if (count && count < 16) {
215       int j;
216       
217       for (j = 0; j < 16 - count; j++) {
218         fprintf(stderr, "   ");
219
220         if ((j + count + 1) % 4 == 0)
221           fprintf(stderr, " ");
222       }
223     }
224   
225     for (i = 0; i < count; i++) {
226       char ch;
227       
228       if (data[pos] < 32 || data[pos] >= 127)
229         ch = '.';
230       else
231         ch = data[pos];
232
233       fprintf(stderr, "%c", ch);
234       pos++;
235     }
236
237     if (count)
238       fprintf(stderr, "\n");
239
240     if (count < 16)
241       break;
242   }
243 }
244
245 /* Sets log files */
246
247 void silc_log_set_files(char *info, uint32 info_size, 
248                         char *warning, uint32 warning_size,
249                         char *error, uint32 error_size,
250                         char *fatal, uint32 fatal_size)
251 {
252   log_info_file = info;
253   log_warning_file = warning;
254   log_error_file = error;
255   log_fatal_file = fatal;
256
257   log_info_size = info_size;
258   log_warning_size = warning_size;
259   log_error_size = error_size;
260   log_fatal_size = fatal_size;
261 }
262
263 /* Sets log callbacks */
264
265 void silc_log_set_callbacks(SilcLogCb info, SilcLogCb warning,
266                             SilcLogCb error, SilcLogCb fatal)
267 {
268   info_cb = info;
269   warning_cb = warning;
270   error_cb = error;
271   fatal_cb = fatal;
272 }
273
274 /* Resets log callbacks */
275
276 void silc_log_reset_callbacks()
277 {
278   info_cb = warning_cb = error_cb = fatal_cb = NULL;
279 }
280
281 /* Sets debug callbacks */
282
283 void silc_log_set_debug_callbacks(SilcDebugCb debug, 
284                                   SilcDebugHexdumpCb debug_hexdump)
285 {
286   debug_cb = debug;
287   debug_hexdump_cb = debug_hexdump;
288 }
289
290 /* Resets debug callbacks */
291
292 void silc_log_reset_debug_callbacks()
293 {
294   debug_cb = NULL;
295   debug_hexdump_cb = NULL;
296 }