# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / snmp_logging.c
1 #ifdef BRCM_SNMP_DEBUG 
2
3 /*
4  * logging.c - generic logging for snmp-agent
5  * * Contributed by Ragnar Kjørstad, ucd@ragnark.vestdata.no 1999-06-26 
6  */
7
8 #include <net-snmp/net-snmp-config.h>
9 #include <stdio.h>
10 #if HAVE_MALLOC_H
11 #include <malloc.h>
12 #endif
13 #if HAVE_STRING_H
14 #include <string.h>
15 #else
16 #include <strings.h>
17 #endif
18 #if HAVE_STDLIB_H
19 #include <stdlib.h>
20 #endif
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #if HAVE_SYSLOG_H
26 #include <syslog.h>
27 #ifndef LOG_CONS                /* Interesting Ultrix feature */
28 #include <sys/syslog.h>
29 #endif
30 #endif
31 #if TIME_WITH_SYS_TIME
32 # ifdef WIN32
33 #  include <sys/timeb.h>
34 # else
35 #  include <sys/time.h>
36 # endif
37 # include <time.h>
38 #else
39 # if HAVE_SYS_TIME_H
40 #  include <sys/time.h>
41 # else
42 #  include <time.h>
43 # endif
44 #endif
45 #if HAVE_NETINET_IN_H
46 #include <netinet/in.h>
47 #endif
48
49 #if HAVE_STDARG_H
50 #include <stdarg.h>
51 #else
52 #include <varargs.h>
53 #endif
54
55 #if HAVE_DMALLOC_H
56 #include <dmalloc.h>
57 #endif
58
59 #if HAVE_WINSOCK_H
60 #include <winsock.h>
61 #endif
62
63 #if HAVE_WINDOWS_H
64 #include <windows.h>
65 #endif
66
67 #include <net-snmp/types.h>
68 #include <net-snmp/output_api.h>
69 #include <net-snmp/library/snmp_logging.h>      /* For this file's "internal" definitions */
70 #include <net-snmp/config_api.h>
71 #include <net-snmp/utilities.h>
72
73 #include <net-snmp/library/callback.h>
74 #define LOGLENGTH 1024
75
76 static int      do_syslogging = 0;
77 static int      do_filelogging = 0;
78 static int      do_stderrlogging = 1;
79 static int      do_log_callback = 0;
80 static int      newline = 1;
81 static FILE    *logfile;
82 #ifdef WIN32
83 static HANDLE   eventlog_h;
84 #endif
85
86 #ifndef HAVE_VSNPRINTF
87                 /*
88                  * Need to use the UCD-provided one 
89                  */
90 int             vsnprintf(char *str, size_t count, const char *fmt,
91                           va_list arg);
92 #endif
93
94 void
95 init_snmp_logging(void)
96 {
97 #ifdef BRCM_SNMP_SUPPORT
98     netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "logTimestamp", 
99                          NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_LOG_TIMESTAMP);
100 #endif
101 }
102
103 int
104 snmp_get_do_logging(void)
105 {
106     return (do_syslogging || do_filelogging || do_stderrlogging ||
107             do_log_callback);
108 }
109
110
111 static char    *
112 sprintf_stamp(time_t * now, char *sbuf)
113 {
114     time_t          Now;
115     struct tm      *tm;
116
117     if (now == NULL) {
118         now = &Now;
119         time(now);
120     }
121     tm = localtime(now);
122     sprintf(sbuf, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d ",
123             tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
124             tm->tm_hour, tm->tm_min, tm->tm_sec);
125     return sbuf;
126 }
127
128 void
129 snmp_disable_syslog(void)
130 {
131 #if HAVE_SYSLOG_H
132     if (do_syslogging)
133         closelog();
134 #endif
135 #ifdef WIN32
136     if (eventlog_h != NULL) {
137         if (!CloseEventLog(eventlog_h)) {
138             fprintf(stderr, "Could not close event log. "
139                     "Last error: 0x%x\n", GetLastError());
140         } else {
141             eventlog_h = NULL;
142         }
143     }
144 #endif                          /* WIN32 */
145     do_syslogging = 0;
146 }
147
148
149 void
150 snmp_disable_filelog(void)
151 {
152     if (do_filelogging) {
153         fputs("\n", logfile);
154         fclose(logfile);
155     }
156     do_filelogging = 0;
157 }
158
159
160 void
161 snmp_disable_stderrlog(void)
162 {
163     do_stderrlogging = 0;
164 }
165
166
167 void
168 snmp_disable_log(void)
169 {
170     snmp_disable_syslog();
171     snmp_disable_filelog();
172     snmp_disable_stderrlog();
173     snmp_disable_calllog();
174 }
175
176 void
177 snmp_enable_syslog(void)
178 {
179     /*
180      * This default should probably be net-snmp at some point 
181      */
182     snmp_enable_syslog_ident(DEFAULT_LOG_ID, LOG_DAEMON);
183 }
184
185 void
186 snmp_enable_syslog_ident(const char *ident, const int facility)
187 {
188     snmp_disable_syslog();
189     if (ident == NULL)
190         /*
191          * This default should probably be net-snmp at some point 
192          */
193         ident = DEFAULT_LOG_ID;
194 #if HAVE_SYSLOG_H
195     openlog(ident, LOG_CONS | LOG_PID, facility);
196     do_syslogging = 1;
197 #endif
198 #ifdef WIN32
199     eventlog_h = OpenEventLog(NULL, ident);
200     if (eventlog_h == NULL) {
201         fprintf(stderr, "Could not open event log for %s. "
202                 "Last error: 0x%x\n", ident, GetLastError());
203         do_syslogging = 0;
204     } else
205         do_syslogging = 1;
206 #endif                          /* WIN32 */
207 }
208
209 void
210 snmp_enable_filelog(const char *logfilename, int dont_zero_log)
211 {
212     snmp_disable_filelog();
213     logfile = fopen(logfilename, dont_zero_log ? "a" : "w");
214     if (logfile) {
215         do_filelogging = 1;
216 #ifdef WIN32
217         /*
218          * Apparently, "line buffering" under Windows is
219          *  actually implemented as "full buffering".
220          *  Let's try turning off buffering completely.
221          */
222         setvbuf(logfile, NULL, _IONBF, BUFSIZ);
223 #else
224         setvbuf(logfile, NULL, _IOLBF, BUFSIZ);
225 #endif
226     } else
227         do_filelogging = 0;
228 }
229
230
231 void
232 snmp_enable_stderrlog(void)
233 {
234     do_stderrlogging = 1;
235 }
236
237
238 void
239 snmp_enable_calllog(void)
240 {
241     do_log_callback = 1;
242 }
243
244
245 void
246 snmp_disable_calllog(void)
247 {
248     do_log_callback = 0;
249 }
250
251
252 void
253 snmp_log_string(int priority, const char *string)
254 {
255     char            sbuf[40];
256     struct snmp_log_message slm;
257 #ifdef WIN32
258     WORD            etype;
259     LPCTSTR         event_msg[2];
260 #endif                          /* WIN32 */
261
262 #if HAVE_SYSLOG_H
263     if (do_syslogging) {
264         syslog(priority, "%s", string);
265     }
266 #endif
267
268 #ifdef WIN32
269     if (do_syslogging) {
270         /*
271          **  EVENT TYPES:
272          **
273          **  Information (EVENTLOG_INFORMATION_TYPE)
274          **      Information events indicate infrequent but significant
275          **      successful operations.
276          **  Warning (EVENTLOG_WARNING_TYPE)
277          **      Warning events indicate problems that are not immediately
278          **      significant, but that may indicate conditions that could
279          **      cause future problems. Resource consumption is a good
280          **      candidate for a warning event.
281          **  Error (EVENTLOG_ERROR_TYPE)
282          **      Error events indicate significant problems that the user
283          **      should know about. Error events usually indicate a loss of
284          **      functionality or data.
285          */
286         switch (priority) {
287         case LOG_EMERG:
288         case LOG_ALERT:
289         case LOG_CRIT:
290         case LOG_ERR:
291             etype = EVENTLOG_ERROR_TYPE;
292             break;
293         case LOG_WARNING:
294             etype = EVENTLOG_WARNING_TYPE;
295             break;
296         case LOG_NOTICE:
297         case LOG_INFO:
298         case LOG_DEBUG:
299             etype = EVENTLOG_INFORMATION_TYPE;
300             break;
301         default:
302             etype = EVENTLOG_INFORMATION_TYPE;
303             break;
304         }
305         event_msg[0] = string;
306         event_msg[1] = NULL;
307         if (!ReportEvent(eventlog_h, etype, 0, 0, NULL, 1, 0,
308                          event_msg, NULL))
309             fprintf(stderr, "Could not report event.  Last error: "
310                     "0x%x\n", GetLastError());
311     }
312 #endif                          /* WIN32 */
313
314     if (do_log_callback) {
315         int             dodebug = snmp_get_do_debugging();
316         slm.priority = priority;
317         slm.msg = string;
318         if (dodebug)            /* turn off debugging inside the callbacks else will loop */
319             snmp_set_do_debugging(0);
320         snmp_call_callbacks(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_LOGGING,
321                             &slm);
322         if (dodebug)
323             snmp_set_do_debugging(dodebug);
324     }
325
326     if (do_filelogging || do_stderrlogging) {
327         if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
328                                    NETSNMP_DS_LIB_LOG_TIMESTAMP) && newline) {
329             sprintf_stamp(NULL, sbuf);
330         } else {
331             strcpy(sbuf, "");
332         }
333         newline = string[strlen(string) - 1] == '\n';
334
335         if (do_filelogging)
336             fprintf(logfile, "%s%s", sbuf, string);
337
338         if (do_stderrlogging)
339             fprintf(stderr, "%s%s", sbuf, string);
340     }
341 }
342
343 int
344 snmp_vlog(int priority, const char *format, va_list ap)
345 {
346     char            buffer[LOGLENGTH];
347     int             length;
348     char           *dynamic;
349
350     length = vsnprintf(buffer, LOGLENGTH, format, ap);
351
352     if (length == 0)
353         return (0);             /* Empty string */
354
355     if (length == -1) {
356         snmp_log_string(LOG_ERR, "Could not format log-string\n");
357         return (-1);
358     }
359
360     if (length < LOGLENGTH) {
361         snmp_log_string(priority, buffer);
362         return (0);
363     }
364
365     dynamic = (char *) malloc(length + 1);
366     if (dynamic == NULL) {
367         snmp_log_string(LOG_ERR,
368                         "Could not allocate memory for log-message\n");
369         snmp_log_string(priority, buffer);
370         return (-2);
371     }
372
373     vsnprintf(dynamic, length + 1, format, ap);
374     snmp_log_string(priority, dynamic);
375     free(dynamic);
376     return 0;
377 }
378
379
380 int
381 #if HAVE_STDARG_H
382 snmp_log(int priority, const char *format, ...)
383 #else
384 snmp_log(va_alist)
385      va_dcl
386 #endif
387 {
388     va_list         ap;
389     int             ret;
390 #if HAVE_STDARG_H
391     va_start(ap, format);
392 #else
393     int             priority;
394     const char     *format;
395     va_start(ap);
396
397     priority = va_arg(ap, int);
398     format = va_arg(ap, const char *);
399 #endif
400     ret = snmp_vlog(priority, format, ap);
401     va_end(ap);
402     return (ret);
403 }
404
405 /*
406  * log a critical error.
407  */
408 void
409 snmp_log_perror(const char *s)
410 {
411     char           *error = strerror(errno);
412     if (s) {
413         if (error)
414             snmp_log(LOG_ERR, "%s: %s\n", s, error);
415         else
416             snmp_log(LOG_ERR, "%s: Error %d out-of-range\n", s, errno);
417     } else {
418         if (error)
419             snmp_log(LOG_ERR, "%s\n", error);
420         else
421             snmp_log(LOG_ERR, "Error %d out-of-range\n", errno);
422     }
423 }
424
425 #else /* SNMP_DEBUG */
426 /*
427  * logging.c - generic logging for snmp-agent
428  * * Contributed by Ragnar Kjørstad, ucd@ragnark.vestdata.no 1999-06-26 
429  */
430
431 #include <net-snmp/net-snmp-config.h>
432 #include <stdio.h>
433 #if HAVE_MALLOC_H
434 #include <malloc.h>
435 #endif
436 #if HAVE_STRING_H
437 #include <string.h>
438 #else
439 #include <strings.h>
440 #endif
441 #if HAVE_STDLIB_H
442 #include <stdlib.h>
443 #endif
444 #include <sys/types.h>
445 #include <sys/stat.h>
446 #include <fcntl.h>
447 #include <errno.h>
448 #if HAVE_SYSLOG_H
449 #include <syslog.h>
450 #ifndef LOG_CONS                /* Interesting Ultrix feature */
451 #include <sys/syslog.h>
452 #endif
453 #endif
454 #if TIME_WITH_SYS_TIME
455 # ifdef WIN32
456 #  include <sys/timeb.h>
457 # else
458 #  include <sys/time.h>
459 # endif
460 # include <time.h>
461 #else
462 # if HAVE_SYS_TIME_H
463 #  include <sys/time.h>
464 # else
465 #  include <time.h>
466 # endif
467 #endif
468 #if HAVE_NETINET_IN_H
469 #include <netinet/in.h>
470 #endif
471
472 #if HAVE_STDARG_H
473 #include <stdarg.h>
474 #else
475 #include <varargs.h>
476 #endif
477
478 #if HAVE_DMALLOC_H
479 #include <dmalloc.h>
480 #endif
481
482 #if HAVE_WINSOCK_H
483 #include <winsock.h>
484 #endif
485
486 #if HAVE_WINDOWS_H
487 #include <windows.h>
488 #endif
489
490 #include <net-snmp/types.h>
491 #include <net-snmp/output_api.h>
492 #include <net-snmp/library/snmp_logging.h>      /* For this file's "internal" definitions */
493 #include <net-snmp/config_api.h>
494 #include <net-snmp/utilities.h>
495
496 #include <net-snmp/library/callback.h>
497
498 void
499 init_snmp_logging(void)
500 {
501   return;
502 }
503
504 int
505 snmp_get_do_logging(void)
506 {
507   return;
508 }
509
510
511 static char    *
512 sprintf_stamp(time_t * now, char *sbuf)
513 {
514   return NULL;
515 }
516
517 void
518 snmp_disable_syslog(void)
519 {
520   return;
521 }
522
523
524 void
525 snmp_disable_filelog(void)
526 {
527   return;
528 }
529
530
531 void
532 snmp_disable_stderrlog(void)
533 {
534   return;
535 }
536
537
538 void
539 snmp_disable_log(void)
540 {
541   return;
542 }
543
544 void
545 snmp_enable_syslog(void)
546 {
547   return;
548 }
549
550 void
551 snmp_enable_syslog_ident(const char *ident, const int facility)
552 {
553   return;
554 }
555
556 void
557 snmp_enable_filelog(const char *logfilename, int dont_zero_log)
558 {
559   return;
560 }
561
562
563 void
564 snmp_enable_stderrlog(void)
565 {
566   return;
567 }
568
569
570 void
571 snmp_enable_calllog(void)
572 {
573   return;
574 }
575
576
577 void
578 snmp_disable_calllog(void)
579 {
580   return;
581 }
582
583
584 void
585 snmp_log_string(int priority, const char *string)
586 {
587   return;
588 }
589
590 int
591 snmp_vlog(int priority, const char *format, va_list ap)
592 {
593   return 0;
594 }
595
596
597 int
598 #if HAVE_STDARG_H
599 snmp_log(int priority, const char *format, ...)
600 #else
601 snmp_log(va_alist)
602      va_dcl
603 #endif
604 {
605   return (0);
606 }
607
608 /*
609  * log a critical error.
610  */
611 void
612 snmp_log_perror(const char *s)
613 {
614   return;
615 }
616
617 #endif /* BRCM_SNMP_DEBUG */