err.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include "err.h"
  45. #ifdef ERR_REPORTING_SYSLOG
  46. # ifdef HAVE_SYSLOG_H
  47. # include <syslog.h>
  48. # endif
  49. #endif
  50. /* err_level reflects the level of errors that are reported */
  51. err_reporting_level_t err_level = err_level_none;
  52. #ifdef SRTP_KERNEL_LINUX
  53. err_status_t
  54. err_reporting_init(char *ident) {
  55. return err_status_ok;
  56. }
  57. #else /* SRTP_KERNEL_LINUX */
  58. /* err_file is the FILE to which errors are reported */
  59. static FILE *err_file = NULL;
  60. err_status_t
  61. err_reporting_init(char *ident) {
  62. #ifdef ERR_REPORTING_SYSLOG
  63. openlog(ident, LOG_PID, LOG_AUTHPRIV);
  64. #endif
  65. /*
  66. * Believe it or not, openlog doesn't return an error on failure.
  67. * But then, neither does the syslog() call...
  68. */
  69. #ifdef ERR_REPORTING_STDOUT
  70. err_file = stdout;
  71. #elif defined(USE_ERR_REPORTING_FILE)
  72. /* open file for error reporting */
  73. err_file = fopen(ERR_REPORTING_FILE, "w");
  74. if (err_file == NULL)
  75. return err_status_init_fail;
  76. #endif
  77. return err_status_ok;
  78. }
  79. void
  80. err_report(int priority, char *format, ...) {
  81. va_list args;
  82. if (priority <= err_level) {
  83. va_start(args, format);
  84. if (err_file != NULL) {
  85. vfprintf(err_file, format, args);
  86. /* fprintf(err_file, "\n"); */
  87. }
  88. #ifdef ERR_REPORTING_SYSLOG
  89. if (1) { /* FIXME: Make this a runtime option. */
  90. int syslogpri;
  91. switch (priority) {
  92. case err_level_emergency:
  93. syslogpri = LOG_EMERG;
  94. break;
  95. case err_level_alert:
  96. syslogpri = LOG_ALERT;
  97. break;
  98. case err_level_critical:
  99. syslogpri = LOG_CRIT;
  100. break;
  101. case err_level_error:
  102. syslogpri = LOG_ERR;
  103. break;
  104. case err_level_warning:
  105. syslogpri = LOG_WARNING;
  106. break;
  107. case err_level_notice:
  108. syslogpri = LOG_NOTICE;
  109. break;
  110. case err_level_info:
  111. syslogpri = LOG_INFO;
  112. break;
  113. case err_level_debug:
  114. case err_level_none:
  115. default:
  116. syslogpri = LOG_DEBUG;
  117. break;
  118. }
  119. vsyslog(syslogpri, format, args);
  120. #endif
  121. va_end(args);
  122. }
  123. }
  124. #endif /* SRTP_KERNEL_LINUX */
  125. void
  126. err_reporting_set_level(err_reporting_level_t lvl) {
  127. err_level = lvl;
  128. }