4 * logging.c - generic logging for snmp-agent
5 * * Contributed by Ragnar Kjørstad, ucd@ragnark.vestdata.no 1999-06-26
8 #include <net-snmp/net-snmp-config.h>
21 #include <sys/types.h>
27 #ifndef LOG_CONS /* Interesting Ultrix feature */
28 #include <sys/syslog.h>
31 #if TIME_WITH_SYS_TIME
33 # include <sys/timeb.h>
35 # include <sys/time.h>
40 # include <sys/time.h>
46 #include <netinet/in.h>
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>
73 #include <net-snmp/library/callback.h>
74 #define LOGLENGTH 1024
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;
83 static HANDLE eventlog_h;
86 #ifndef HAVE_VSNPRINTF
88 * Need to use the UCD-provided one
90 int vsnprintf(char *str, size_t count, const char *fmt,
95 init_snmp_logging(void)
97 #ifdef BRCM_SNMP_SUPPORT
98 netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "logTimestamp",
99 NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_LOG_TIMESTAMP);
104 snmp_get_do_logging(void)
106 return (do_syslogging || do_filelogging || do_stderrlogging ||
112 sprintf_stamp(time_t * now, char *sbuf)
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);
129 snmp_disable_syslog(void)
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());
150 snmp_disable_filelog(void)
152 if (do_filelogging) {
153 fputs("\n", logfile);
161 snmp_disable_stderrlog(void)
163 do_stderrlogging = 0;
168 snmp_disable_log(void)
170 snmp_disable_syslog();
171 snmp_disable_filelog();
172 snmp_disable_stderrlog();
173 snmp_disable_calllog();
177 snmp_enable_syslog(void)
180 * This default should probably be net-snmp at some point
182 snmp_enable_syslog_ident(DEFAULT_LOG_ID, LOG_DAEMON);
186 snmp_enable_syslog_ident(const char *ident, const int facility)
188 snmp_disable_syslog();
191 * This default should probably be net-snmp at some point
193 ident = DEFAULT_LOG_ID;
195 openlog(ident, LOG_CONS | LOG_PID, facility);
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());
210 snmp_enable_filelog(const char *logfilename, int dont_zero_log)
212 snmp_disable_filelog();
213 logfile = fopen(logfilename, dont_zero_log ? "a" : "w");
218 * Apparently, "line buffering" under Windows is
219 * actually implemented as "full buffering".
220 * Let's try turning off buffering completely.
222 setvbuf(logfile, NULL, _IONBF, BUFSIZ);
224 setvbuf(logfile, NULL, _IOLBF, BUFSIZ);
232 snmp_enable_stderrlog(void)
234 do_stderrlogging = 1;
239 snmp_enable_calllog(void)
246 snmp_disable_calllog(void)
253 snmp_log_string(int priority, const char *string)
256 struct snmp_log_message slm;
259 LPCTSTR event_msg[2];
264 syslog(priority, "%s", string);
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.
291 etype = EVENTLOG_ERROR_TYPE;
294 etype = EVENTLOG_WARNING_TYPE;
299 etype = EVENTLOG_INFORMATION_TYPE;
302 etype = EVENTLOG_INFORMATION_TYPE;
305 event_msg[0] = string;
307 if (!ReportEvent(eventlog_h, etype, 0, 0, NULL, 1, 0,
309 fprintf(stderr, "Could not report event. Last error: "
310 "0x%x\n", GetLastError());
314 if (do_log_callback) {
315 int dodebug = snmp_get_do_debugging();
316 slm.priority = priority;
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,
323 snmp_set_do_debugging(dodebug);
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);
333 newline = string[strlen(string) - 1] == '\n';
336 fprintf(logfile, "%s%s", sbuf, string);
338 if (do_stderrlogging)
339 fprintf(stderr, "%s%s", sbuf, string);
344 snmp_vlog(int priority, const char *format, va_list ap)
346 char buffer[LOGLENGTH];
350 length = vsnprintf(buffer, LOGLENGTH, format, ap);
353 return (0); /* Empty string */
356 snmp_log_string(LOG_ERR, "Could not format log-string\n");
360 if (length < LOGLENGTH) {
361 snmp_log_string(priority, buffer);
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);
373 vsnprintf(dynamic, length + 1, format, ap);
374 snmp_log_string(priority, dynamic);
382 snmp_log(int priority, const char *format, ...)
391 va_start(ap, format);
397 priority = va_arg(ap, int);
398 format = va_arg(ap, const char *);
400 ret = snmp_vlog(priority, format, ap);
406 * log a critical error.
409 snmp_log_perror(const char *s)
411 char *error = strerror(errno);
414 snmp_log(LOG_ERR, "%s: %s\n", s, error);
416 snmp_log(LOG_ERR, "%s: Error %d out-of-range\n", s, errno);
419 snmp_log(LOG_ERR, "%s\n", error);
421 snmp_log(LOG_ERR, "Error %d out-of-range\n", errno);
425 #else /* SNMP_DEBUG */
427 * logging.c - generic logging for snmp-agent
428 * * Contributed by Ragnar Kjørstad, ucd@ragnark.vestdata.no 1999-06-26
431 #include <net-snmp/net-snmp-config.h>
444 #include <sys/types.h>
445 #include <sys/stat.h>
450 #ifndef LOG_CONS /* Interesting Ultrix feature */
451 #include <sys/syslog.h>
454 #if TIME_WITH_SYS_TIME
456 # include <sys/timeb.h>
458 # include <sys/time.h>
463 # include <sys/time.h>
468 #if HAVE_NETINET_IN_H
469 #include <netinet/in.h>
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>
496 #include <net-snmp/library/callback.h>
499 init_snmp_logging(void)
505 snmp_get_do_logging(void)
512 sprintf_stamp(time_t * now, char *sbuf)
518 snmp_disable_syslog(void)
525 snmp_disable_filelog(void)
532 snmp_disable_stderrlog(void)
539 snmp_disable_log(void)
545 snmp_enable_syslog(void)
551 snmp_enable_syslog_ident(const char *ident, const int facility)
557 snmp_enable_filelog(const char *logfilename, int dont_zero_log)
564 snmp_enable_stderrlog(void)
571 snmp_enable_calllog(void)
578 snmp_disable_calllog(void)
585 snmp_log_string(int priority, const char *string)
591 snmp_vlog(int priority, const char *format, va_list ap)
599 snmp_log(int priority, const char *format, ...)
609 * log a critical error.
612 snmp_log_perror(const char *s)
617 #endif /* BRCM_SNMP_DEBUG */