michael@0: /* michael@0: * err.c michael@0: * michael@0: * error status reporting functions michael@0: * michael@0: * David A. McGrew michael@0: * Cisco Systems, Inc. michael@0: */ michael@0: /* michael@0: * michael@0: * Copyright(c) 2001-2006 Cisco Systems, Inc. michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * Redistributions in binary form must reproduce the above michael@0: * copyright notice, this list of conditions and the following michael@0: * disclaimer in the documentation and/or other materials provided michael@0: * with the distribution. michael@0: * michael@0: * Neither the name of the Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS michael@0: * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE michael@0: * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, michael@0: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR michael@0: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, michael@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED michael@0: * OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: #include "err.h" michael@0: michael@0: #ifdef ERR_REPORTING_SYSLOG michael@0: # ifdef HAVE_SYSLOG_H michael@0: # include michael@0: # endif michael@0: #endif michael@0: michael@0: michael@0: /* err_level reflects the level of errors that are reported */ michael@0: michael@0: err_reporting_level_t err_level = err_level_none; michael@0: michael@0: #ifdef SRTP_KERNEL_LINUX michael@0: err_status_t michael@0: err_reporting_init(char *ident) { michael@0: michael@0: return err_status_ok; michael@0: } michael@0: michael@0: #else /* SRTP_KERNEL_LINUX */ michael@0: michael@0: /* err_file is the FILE to which errors are reported */ michael@0: michael@0: static FILE *err_file = NULL; michael@0: michael@0: err_status_t michael@0: err_reporting_init(char *ident) { michael@0: #ifdef ERR_REPORTING_SYSLOG michael@0: openlog(ident, LOG_PID, LOG_AUTHPRIV); michael@0: #endif michael@0: michael@0: /* michael@0: * Believe it or not, openlog doesn't return an error on failure. michael@0: * But then, neither does the syslog() call... michael@0: */ michael@0: michael@0: #ifdef ERR_REPORTING_STDOUT michael@0: err_file = stdout; michael@0: #elif defined(USE_ERR_REPORTING_FILE) michael@0: /* open file for error reporting */ michael@0: err_file = fopen(ERR_REPORTING_FILE, "w"); michael@0: if (err_file == NULL) michael@0: return err_status_init_fail; michael@0: #endif michael@0: michael@0: return err_status_ok; michael@0: } michael@0: michael@0: void michael@0: err_report(int priority, char *format, ...) { michael@0: va_list args; michael@0: michael@0: if ((err_reporting_level_t)priority <= err_level) { michael@0: michael@0: va_start(args, format); michael@0: if (err_file != NULL) { michael@0: vfprintf(err_file, format, args); michael@0: /* fprintf(err_file, "\n"); */ michael@0: } michael@0: #ifdef ERR_REPORTING_SYSLOG michael@0: if (1) { /* FIXME: Make this a runtime option. */ michael@0: int syslogpri; michael@0: michael@0: switch (priority) { michael@0: case err_level_emergency: michael@0: syslogpri = LOG_EMERG; michael@0: break; michael@0: case err_level_alert: michael@0: syslogpri = LOG_ALERT; michael@0: break; michael@0: case err_level_critical: michael@0: syslogpri = LOG_CRIT; michael@0: break; michael@0: case err_level_error: michael@0: syslogpri = LOG_ERR; michael@0: break; michael@0: case err_level_warning: michael@0: syslogpri = LOG_WARNING; michael@0: break; michael@0: case err_level_notice: michael@0: syslogpri = LOG_NOTICE; michael@0: break; michael@0: case err_level_info: michael@0: syslogpri = LOG_INFO; michael@0: break; michael@0: case err_level_debug: michael@0: case err_level_none: michael@0: default: michael@0: syslogpri = LOG_DEBUG; michael@0: break; michael@0: } michael@0: michael@0: vsyslog(syslogpri, format, args); michael@0: #endif michael@0: va_end(args); michael@0: } michael@0: } michael@0: #endif /* SRTP_KERNEL_LINUX */ michael@0: michael@0: void michael@0: err_reporting_set_level(err_reporting_level_t lvl) { michael@0: err_level = lvl; michael@0: }