/[LeafOK_CVS]/pvpgn-1.7.4/src/common/eventlog.c
ViewVC logotype

Contents of /pvpgn-1.7.4/src/common/eventlog.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
Branch: GNU, MAIN
CVS Tags: arelease, HEAD
Changes since 1.1: +0 -0 lines
Content type: text/x-csrc
no message

1 /*
2 * Copyright (C) 1999 Rob Crittenden (rcrit@greyoak.com)
3 * Copyright (C) 1999,2000 Ross Combs (rocombs@cs.nmsu.edu)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "common/setup_before.h"
20 #include <stdio.h>
21 #ifdef HAVE_STDDEF_H
22 # include <stddef.h>
23 #else
24 # ifndef NULL
25 # define NULL ((void *)0)
26 # endif
27 #endif
28 #include <errno.h>
29 #include "compat/strerror.h"
30 #ifdef HAVE_STRING_H
31 # include <string.h>
32 #else
33 # ifdef HAVE_STRINGS_H
34 # include <strings.h>
35 # endif
36 #endif
37 #include "compat/strcasecmp.h"
38 #include "compat/vargs.h"
39 #ifdef TIME_WITH_SYS_TIME
40 # include <sys/time.h>
41 # include <time.h>
42 #else
43 # ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
45 # else
46 # include <time.h>
47 # endif
48 #endif
49 #include "compat/strftime.h"
50 #ifdef HAVE_UNISTD_H
51 # include <unistd.h>
52 #endif
53 #include "common/eventlog.h"
54 #include "common/hexdump.h"
55 #include "common/setup_after.h"
56
57 #ifdef WIN32_GUI
58 # include "win32/winmain.h"
59 #endif
60
61 static FILE * eventstrm=NULL;
62 static t_eventlog_level currlevel=eventlog_level_debug|
63 eventlog_level_info|
64 eventlog_level_warn|
65 eventlog_level_error|
66 eventlog_level_fatal;
67 /* FIXME: maybe this should be default for win32 */
68 static int eventlog_debugmode=0;
69
70 extern void eventlog_set_debugmode(int debugmode)
71 {
72 eventlog_debugmode = debugmode;
73 }
74
75 extern void eventlog_set(FILE * fp)
76 {
77 eventstrm = fp;
78 }
79
80 extern FILE * eventlog_get(void)
81 {
82 return eventstrm;
83 }
84
85 extern int eventlog_close(void)
86 {
87 fclose(eventstrm);
88 return 0;
89 }
90
91 extern int eventlog_open(char const * filename)
92 {
93 FILE * temp;
94
95 if (!filename)
96 {
97 eventlog(eventlog_level_error,__FUNCTION__,"got NULL filename");
98 return -1;
99 }
100
101 if (!(temp = fopen(filename,"a")))
102 {
103 eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for appending (fopen: %s)",filename,pstrerror(errno));
104 return -1;
105 }
106
107 if (eventstrm && eventstrm!=stderr) /* close old one */
108 if (fclose(eventstrm)<0)
109 {
110 eventstrm = temp;
111 eventlog(eventlog_level_error,__FUNCTION__,"could not close previous logfile after writing (fclose: %s)",pstrerror(errno));
112 return 0;
113 }
114 eventstrm = temp;
115
116 return 0;
117 }
118
119
120 extern void eventlog_clear_level(void)
121 {
122 currlevel = eventlog_level_none;
123 }
124
125
126 extern int eventlog_add_level(char const * levelname)
127 {
128 if (!levelname)
129 {
130 eventlog(eventlog_level_error,__FUNCTION__,"got NULL levelname");
131 return -1;
132 }
133
134 if (strcasecmp(levelname,"trace")==0)
135 {
136 currlevel |= eventlog_level_trace;
137 return 0;
138 }
139 if (strcasecmp(levelname,"debug")==0)
140 {
141 currlevel |= eventlog_level_debug;
142 return 0;
143 }
144 if (strcasecmp(levelname,"info")==0)
145 {
146 currlevel |= eventlog_level_info;
147 return 0;
148 }
149 if (strcasecmp(levelname,"warn")==0)
150 {
151 currlevel |= eventlog_level_warn;
152 return 0;
153 }
154 if (strcasecmp(levelname,"error")==0)
155 {
156 currlevel |= eventlog_level_error;
157 return 0;
158 }
159 if (strcasecmp(levelname,"fatal")==0)
160 {
161 currlevel |= eventlog_level_fatal;
162 return 0;
163 }
164
165 eventlog(eventlog_level_error,__FUNCTION__,"got bad levelname \"%s\"",levelname);
166 return -1;
167 }
168
169
170 extern int eventlog_del_level(char const * levelname)
171 {
172 if (!levelname)
173 {
174 eventlog(eventlog_level_error,__FUNCTION__,"got NULL levelname");
175 return -1;
176 }
177
178 if (strcasecmp(levelname,"trace")==0)
179 {
180 currlevel &= ~eventlog_level_trace;
181 return 0;
182 }
183 if (strcasecmp(levelname,"debug")==0)
184 {
185 currlevel &= ~eventlog_level_debug;
186 return 0;
187 }
188 if (strcasecmp(levelname,"info")==0)
189 {
190 currlevel &= ~eventlog_level_info;
191 return 0;
192 }
193 if (strcasecmp(levelname,"warn")==0)
194 {
195 currlevel &= ~eventlog_level_warn;
196 return 0;
197 }
198 if (strcasecmp(levelname,"error")==0)
199 {
200 currlevel &= ~eventlog_level_error;
201 return 0;
202 }
203 if (strcasecmp(levelname,"fatal")==0)
204 {
205 currlevel &= ~eventlog_level_fatal;
206 return 0;
207 }
208
209
210 eventlog(eventlog_level_error,__FUNCTION__,"got bad levelname \"%s\"",levelname);
211 return -1;
212 }
213
214 extern char const * eventlog_get_levelname_str(t_eventlog_level level)
215 {
216 switch (level)
217 {
218 case eventlog_level_trace:
219 return "trace";
220 case eventlog_level_debug:
221 return "debug";
222 case eventlog_level_info:
223 return "info ";
224 case eventlog_level_warn:
225 return "warn ";
226 case eventlog_level_error:
227 return "error";
228 case eventlog_level_fatal:
229 return "fatal";
230 default:
231 return "unknown";
232 }
233 }
234
235 extern void eventlog_hexdump_data(void const * data, unsigned int len)
236 {
237 unsigned int i;
238 char dst[100];
239 unsigned char * datac;
240
241 if (!data) {
242 eventlog(eventlog_level_error, __FUNCTION__, "got NULL data");
243 return;
244 }
245
246 for (i = 0, datac = (char*)data; i < len; i += 16, datac += 16)
247 {
248 hexdump_string(datac, (len - i < 16) ? (len - i) : 16, dst, i);
249 fprintf(eventstrm,"%s\n",dst);
250 #ifdef WIN32_GUI
251 gui_lprintf(eventlog_level_info,"%s\n",dst);
252 #endif
253 if (eventlog_debugmode)
254 {
255 printf("%s\n",dst);
256 }
257
258 }
259 if (eventlog_debugmode) fflush(stdout);
260 fflush(eventstrm);
261 }
262
263 #ifdef DEBUGMODSTRINGS
264 extern void eventlog_real(t_eventlog_level level, char const * module, char const * fmt, ...)
265 #else
266 extern void eventlog(t_eventlog_level level, char const * module, char const * fmt, ...)
267 #endif
268 {
269 va_list args;
270 char time_string[EVENT_TIME_MAXLEN];
271 struct tm * tmnow;
272 time_t now;
273
274 if (!(level&currlevel))
275 return;
276 if (!eventstrm)
277 return;
278
279 /* get the time before parsing args */
280 time(&now);
281 if (!(tmnow = localtime(&now)))
282 strcpy(time_string,"?");
283 else
284 strftime(time_string,EVENT_TIME_MAXLEN,EVENT_TIME_FORMAT,tmnow);
285
286 if (!module)
287 {
288 fprintf(eventstrm,"%s [error] eventlog: got NULL module\n",time_string);
289 #ifdef WIN32_GUI
290 gui_lprintf(eventlog_level_error,"%s [error] eventlog: got NULL module\n",time_string);
291 #endif
292 fflush(eventstrm);
293 return;
294 }
295
296 if (!fmt)
297 {
298 fprintf(eventstrm,"%s [error] eventlog: got NULL fmt\n",time_string);
299 #ifdef WIN32_GUI
300 gui_lprintf(eventlog_level_error,"%s [error] eventlog: got NULL fmt\n",time_string);
301 #endif
302 fflush(eventstrm);
303 return;
304 }
305
306 fprintf(eventstrm,"%s [%s] %s: ",time_string,eventlog_get_levelname_str(level),module);
307 #ifdef WIN32_GUI
308 gui_lprintf(level,"%s [%s] %s: ",time_string,eventlog_get_levelname_str(level),module);
309 #endif
310
311 VA_START(args,fmt);
312
313 #ifdef HAVE_VPRINTF
314 vfprintf(eventstrm,fmt,args);
315 #ifdef WIN32_GUI
316 gui_lvprintf(level,fmt,args);
317 #endif
318 #else
319 # if HAVE__DOPRNT
320 _doprnt(fmt,args,eventstrm);
321 # else
322 fprintf(eventstrm,"sorry, vfprintf() and _doprnt() are not available on this system");
323 # endif
324 #endif
325 va_end(args);
326 fprintf(eventstrm,"\n");
327 #ifdef WIN32_GUI
328 gui_lprintf(level,"\n");
329 #endif
330
331 if (eventlog_debugmode) {
332 printf("%s [%s] %s: ",time_string,eventlog_get_levelname_str(level),module);
333 va_start(args,fmt);
334 #ifdef HAVE_VPRINTF
335 vprintf(fmt,args);
336 #else
337 # if HAVE__DOPRNT
338 _doprnt(fmt,args,stdout);
339 # else
340 printf("sorry, vfprintf() and _doprnt() are not available on this system");
341 # endif
342 #endif
343 va_end(args);
344 printf("\n");
345 fflush(stdout);
346 }
347 fflush(eventstrm);
348 }
349
350
351 extern void eventlog_step(char const * filename, t_eventlog_level level, char const * module, char const * fmt, ...)
352 {
353 va_list args;
354 char time_string[EVENT_TIME_MAXLEN];
355 struct tm * tmnow;
356 time_t now;
357 FILE * fp;
358
359 if (!(level&currlevel))
360 return;
361 if (!eventstrm)
362 return;
363
364 if (!(fp = fopen(filename, "a")))
365 return;
366
367 /* get the time before parsing args */
368 time(&now);
369 if (!(tmnow = localtime(&now)))
370 strcpy(time_string,"?");
371 else
372 strftime(time_string,EVENT_TIME_MAXLEN,EVENT_TIME_FORMAT,tmnow);
373
374 if (!module)
375 {
376 fprintf(fp,"%s [error] eventlog_step: got NULL module\n",time_string);
377 fclose(fp);
378 return;
379 }
380 if (!fmt)
381 {
382 fprintf(fp,"%s [error] eventlog_step: got NULL fmt\n",time_string);
383 fclose(fp);
384 return;
385 }
386
387 fprintf(fp,"%s [%s] %s: ",time_string,eventlog_get_levelname_str(level),module);
388 va_start(args,fmt);
389 #ifdef HAVE_VPRINTF
390 vfprintf(fp,fmt,args);
391 #else
392 # if HAVE__DOPRNT
393 _doprnt(fmt,args,fp);
394 # else
395 fprintf(fp,"sorry, vfprintf() and _doprnt() are not available on this system");
396 # endif
397 #endif
398 va_end(args);
399 fprintf(fp,"\n");
400 fclose(fp);
401 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1