0b1119fc445c53b49a38bc429081634799f7a1b3
[silc.git] / lib / silccore / 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 /*
21  * $Id$
22  * $Log$
23  * Revision 1.4  2000/07/17 16:44:57  priikone
24  *      Buffer overflow bug fixe in silc_log_format.
25  *
26  * Revision 1.3  2000/07/05 06:06:35  priikone
27  *      Global cosmetic change.
28  *
29  * Revision 1.2  2000/07/03 05:53:58  priikone
30  *      Fixed file purging bug.  The purging should work now ok.
31  *
32  * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
33  *      Imported from internal CVS/Added Log headers.
34  *
35  *
36  */
37
38 #include "silcincludes.h"
39
40 /* SILC Log name strings. These strings are printed to the log file. */
41 const SilcLogTypeName silc_log_types[] =
42 {
43   { "Info", SILC_LOG_INFO },
44   { "Warning", SILC_LOG_WARNING },
45   { "Error", SILC_LOG_ERROR },
46   { "Fatal", SILC_LOG_FATAL },
47
48   { NULL, -1 },
49 };
50
51 char *log_info_file;
52 char *log_warning_file;
53 char *log_error_file;
54 char *log_fatal_file;
55 unsigned int log_info_size;
56 unsigned int log_warning_size;
57 unsigned int log_error_size;
58 unsigned int log_fatal_size;
59
60 /* Formats arguments to a string and returns it after allocating memory
61    for it. It must be remembered to free it later. */
62
63 char *silc_log_format(char *fmt, ...)
64 {
65   va_list args;
66   static char buf[8192];
67
68   memset(buf, 0, sizeof(buf);
69   va_start(args, fmt);
70   vsnprintf(buf, sizeof(buf) - 1, fmt, args);
71   va_end(args);
72
73   return strdup(buf);
74 }
75
76 /* Outputs the log message to what ever log file selected. */
77
78 void silc_log_output(const char *filename, unsigned int maxsize,
79                      SilcLogType type, char *string)
80 {
81   FILE *fp;
82   const SilcLogTypeName *np;
83
84   /* Purge the log file if the max size is defined. */
85   if (maxsize) {
86     fp = fopen(filename, "r");
87     if (fp) {
88       int filelen;
89       
90       filelen = fseek(fp, (off_t)0L, SEEK_END);
91       fseek(fp, (off_t)0L, SEEK_SET);  
92       
93       /* Purge? */
94       if (filelen >= maxsize)
95         unlink(filename);
96     }
97   }
98
99   /* Open the log file */
100   if ((fp = fopen(filename, "a+")) == NULL) {
101     fprintf(stderr, "warning: could not open log file "
102             "%s: %s\n", filename, strerror(errno));
103     fprintf(stderr, "warning: log messages will be displayed on the screen\n");
104     fp = stderr;
105   }
106  
107   /* Get the log type name */
108   for(np = silc_log_types; np->name; np++) {
109     if (np->type == type)
110       break;
111   }
112
113   fprintf(fp, "[%s] [%s] %s\n", silc_get_time(), np->name, string);
114   fflush(fp);
115   fclose(fp);
116   silc_free(string);
117 }
118
119 /* Outputs the debug message to stderr. */
120
121 void silc_log_output_debug(char *file, char *function, 
122                            int line, char *string)
123 {
124   /* fprintf(stderr, "%s:%s:%d: %s\n", file, function, line, string); */
125   fprintf(stderr, "%s:%d: %s\n", function, line, string);
126   fflush(stderr);
127   silc_free(string);
128 }
129
130 /* Hexdumps a message */
131
132 void silc_log_output_hexdump(char *file, char *function, 
133                              int line, void *data_in,
134                              unsigned int len, char *string)
135 {
136   int i, k;
137   int off, pos, count;
138   unsigned char *data = (unsigned char *)data_in;
139
140   /* fprintf(stderr, "%s:%s:%d: %s\n", file, function, line, string); */
141   fprintf(stderr, "%s:%d: %s\n", function, line, string);
142   silc_free(string);
143
144   k = 0;
145   off = len % 16;
146   pos = 0;
147   count = 16;
148   while (1) {
149
150     if (off) {
151       if ((len - pos) < 16 && (len - pos <= len - off))
152         count = off;
153     } else {
154       if (pos == len)
155         count = 0;
156     }
157     if (off == len)
158       count = len;
159
160     if (count)
161       fprintf(stderr, "%08X  ", k++ * 16);
162
163     for (i = 0; i < count; i++) {
164       fprintf(stderr, "%02X ", data[pos + i]);
165       
166       if ((i + 1) % 4 == 0)
167         fprintf(stderr, " ");
168     }
169
170     if (count && count < 16) {
171       int j;
172       
173       for (j = 0; j < 16 - count; j++) {
174         fprintf(stderr, "   ");
175
176         if ((j + count + 1) % 4 == 0)
177           fprintf(stderr, " ");
178       }
179     }
180   
181     for (i = 0; i < count; i++) {
182       char ch;
183       
184       if (data[pos] < 32 || data[pos] >= 127)
185         ch = '.';
186       else
187         ch = data[pos];
188
189       fprintf(stderr, "%c", ch);
190       pos++;
191     }
192
193     if (count)
194       fprintf(stderr, "\n");
195
196     if (count < 16)
197       break;
198   }
199 }
200
201 /* Sets log files */
202
203 void silc_log_set_files(char *info, unsigned int info_size, 
204                         char *warning, unsigned int warning_size,
205                         char *error, unsigned int error_size,
206                         char *fatal, unsigned int fatal_size)
207 {
208   log_info_file = info;
209   log_warning_file = warning;
210   log_error_file = error;
211   log_fatal_file = fatal;
212
213   log_info_size = info_size;
214   log_warning_size = warning_size;
215   log_error_size = error_size;
216   log_fatal_size = fatal_size;
217 }