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