Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* |
| 2 | * err.c |
| 3 | * |
| 4 | * error status reporting functions |
| 5 | * |
| 6 | * David A. McGrew |
| 7 | * Cisco Systems, Inc. |
| 8 | */ |
| 9 | /* |
| 10 | * |
| 11 | * Copyright(c) 2001-2006 Cisco Systems, Inc. |
| 12 | * All rights reserved. |
| 13 | * |
| 14 | * Redistribution and use in source and binary forms, with or without |
| 15 | * modification, are permitted provided that the following conditions |
| 16 | * are met: |
| 17 | * |
| 18 | * Redistributions of source code must retain the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer. |
| 20 | * |
| 21 | * Redistributions in binary form must reproduce the above |
| 22 | * copyright notice, this list of conditions and the following |
| 23 | * disclaimer in the documentation and/or other materials provided |
| 24 | * with the distribution. |
| 25 | * |
| 26 | * Neither the name of the Cisco Systems, Inc. nor the names of its |
| 27 | * contributors may be used to endorse or promote products derived |
| 28 | * from this software without specific prior written permission. |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 31 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 32 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 33 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 34 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 35 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 36 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 37 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 39 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 40 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 41 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | #include "err.h" |
| 46 | |
| 47 | #ifdef ERR_REPORTING_SYSLOG |
| 48 | # ifdef HAVE_SYSLOG_H |
| 49 | # include <syslog.h> |
| 50 | # endif |
| 51 | #endif |
| 52 | |
| 53 | |
| 54 | /* err_level reflects the level of errors that are reported */ |
| 55 | |
| 56 | err_reporting_level_t err_level = err_level_none; |
| 57 | |
| 58 | #ifdef SRTP_KERNEL_LINUX |
| 59 | err_status_t |
| 60 | err_reporting_init(char *ident) { |
| 61 | |
| 62 | return err_status_ok; |
| 63 | } |
| 64 | |
| 65 | #else /* SRTP_KERNEL_LINUX */ |
| 66 | |
| 67 | /* err_file is the FILE to which errors are reported */ |
| 68 | |
| 69 | static FILE *err_file = NULL; |
| 70 | |
| 71 | err_status_t |
| 72 | err_reporting_init(char *ident) { |
| 73 | #ifdef ERR_REPORTING_SYSLOG |
| 74 | openlog(ident, LOG_PID, LOG_AUTHPRIV); |
| 75 | #endif |
| 76 | |
| 77 | /* |
| 78 | * Believe it or not, openlog doesn't return an error on failure. |
| 79 | * But then, neither does the syslog() call... |
| 80 | */ |
| 81 | |
| 82 | #ifdef ERR_REPORTING_STDOUT |
| 83 | err_file = stdout; |
| 84 | #elif defined(USE_ERR_REPORTING_FILE) |
| 85 | /* open file for error reporting */ |
| 86 | err_file = fopen(ERR_REPORTING_FILE, "w"); |
| 87 | if (err_file == NULL) |
| 88 | return err_status_init_fail; |
| 89 | #endif |
| 90 | |
| 91 | return err_status_ok; |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | err_report(int priority, char *format, ...) { |
| 96 | va_list args; |
| 97 | |
| 98 | if (priority <= err_level) { |
| 99 | |
| 100 | va_start(args, format); |
| 101 | if (err_file != NULL) { |
| 102 | vfprintf(err_file, format, args); |
| 103 | /* fprintf(err_file, "\n"); */ |
| 104 | } |
| 105 | #ifdef ERR_REPORTING_SYSLOG |
| 106 | if (1) { /* FIXME: Make this a runtime option. */ |
| 107 | int syslogpri; |
| 108 | |
| 109 | switch (priority) { |
| 110 | case err_level_emergency: |
| 111 | syslogpri = LOG_EMERG; |
| 112 | break; |
| 113 | case err_level_alert: |
| 114 | syslogpri = LOG_ALERT; |
| 115 | break; |
| 116 | case err_level_critical: |
| 117 | syslogpri = LOG_CRIT; |
| 118 | break; |
| 119 | case err_level_error: |
| 120 | syslogpri = LOG_ERR; |
| 121 | break; |
| 122 | case err_level_warning: |
| 123 | syslogpri = LOG_WARNING; |
| 124 | break; |
| 125 | case err_level_notice: |
| 126 | syslogpri = LOG_NOTICE; |
| 127 | break; |
| 128 | case err_level_info: |
| 129 | syslogpri = LOG_INFO; |
| 130 | break; |
| 131 | case err_level_debug: |
| 132 | case err_level_none: |
| 133 | default: |
| 134 | syslogpri = LOG_DEBUG; |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | vsyslog(syslogpri, format, args); |
| 139 | #endif |
| 140 | va_end(args); |
| 141 | } |
| 142 | } |
| 143 | #endif /* SRTP_KERNEL_LINUX */ |
| 144 | |
| 145 | void |
| 146 | err_reporting_set_level(err_reporting_level_t lvl) { |
| 147 | err_level = lvl; |
| 148 | } |