syslog.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, malleable, LLC.
  5. *
  6. * Sean Bright <sean@malleable.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Asterisk Syslog Utility Functions
  21. * \author Sean Bright <sean@malleable.com>
  22. */
  23. /*** MODULEINFO
  24. <support_level>core</support_level>
  25. ***/
  26. #include "asterisk.h"
  27. #include "asterisk/utils.h"
  28. #include "asterisk/syslog.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include <syslog.h>
  31. static const struct {
  32. const char *name;
  33. int value;
  34. } facility_map[] = {
  35. /* POSIX only specifies USER and LOCAL0 - LOCAL7 */
  36. { "user", LOG_USER },
  37. { "local0", LOG_LOCAL0 },
  38. { "local1", LOG_LOCAL1 },
  39. { "local2", LOG_LOCAL2 },
  40. { "local3", LOG_LOCAL3 },
  41. { "local4", LOG_LOCAL4 },
  42. { "local5", LOG_LOCAL5 },
  43. { "local6", LOG_LOCAL6 },
  44. { "local7", LOG_LOCAL7 },
  45. #if defined(HAVE_SYSLOG_FACILITY_LOG_KERN)
  46. { "kern", LOG_KERN },
  47. #endif
  48. #if defined(HAVE_SYSLOG_FACILITY_LOG_MAIL)
  49. { "mail", LOG_MAIL },
  50. #endif
  51. #if defined(HAVE_SYSLOG_FACILITY_LOG_DAEMON)
  52. { "daemon", LOG_DAEMON },
  53. #endif
  54. #if defined(HAVE_SYSLOG_FACILITY_LOG_AUTH)
  55. { "auth", LOG_AUTH },
  56. { "security", LOG_AUTH },
  57. #endif
  58. #if defined(HAVE_SYSLOG_FACILITY_LOG_AUTHPRIV)
  59. { "authpriv", LOG_AUTHPRIV },
  60. #endif
  61. #if defined(HAVE_SYSLOG_FACILITY_LOG_SYSLOG)
  62. { "syslog", LOG_SYSLOG },
  63. #endif
  64. #if defined(HAVE_SYSLOG_FACILITY_LOG_FTP)
  65. { "ftp", LOG_FTP },
  66. #endif
  67. #if defined(HAVE_SYSLOG_FACILITY_LOG_LPR)
  68. { "lpr", LOG_LPR },
  69. #endif
  70. #if defined(HAVE_SYSLOG_FACILITY_LOG_NEWS)
  71. { "news", LOG_NEWS },
  72. #endif
  73. #if defined(HAVE_SYSLOG_FACILITY_LOG_UUCP)
  74. { "uucp", LOG_UUCP },
  75. #endif
  76. #if defined(HAVE_SYSLOG_FACILITY_LOG_CRON)
  77. { "cron", LOG_CRON },
  78. #endif
  79. };
  80. int ast_syslog_facility(const char *facility)
  81. {
  82. int index;
  83. for (index = 0; index < ARRAY_LEN(facility_map); index++) {
  84. if (!strcasecmp(facility_map[index].name, facility)) {
  85. return facility_map[index].value;
  86. }
  87. }
  88. return -1;
  89. }
  90. const char *ast_syslog_facility_name(int facility)
  91. {
  92. int index;
  93. for (index = 0; index < ARRAY_LEN(facility_map); index++) {
  94. if (facility_map[index].value == facility) {
  95. return facility_map[index].name;
  96. }
  97. }
  98. return NULL;
  99. }
  100. static const struct {
  101. const char *name;
  102. int value;
  103. } priority_map[] = {
  104. { "alert", LOG_ALERT },
  105. { "crit", LOG_CRIT },
  106. { "debug", LOG_DEBUG },
  107. { "emerg", LOG_EMERG },
  108. { "err", LOG_ERR },
  109. { "error", LOG_ERR },
  110. { "info", LOG_INFO },
  111. { "notice", LOG_NOTICE },
  112. { "warning", LOG_WARNING },
  113. };
  114. int ast_syslog_priority(const char *priority)
  115. {
  116. int index;
  117. for (index = 0; index < ARRAY_LEN(priority_map); index++) {
  118. if (!strcasecmp(priority_map[index].name, priority)) {
  119. return priority_map[index].value;
  120. }
  121. }
  122. return -1;
  123. }
  124. const char *ast_syslog_priority_name(int priority)
  125. {
  126. int index;
  127. for (index = 0; index < ARRAY_LEN(priority_map); index++) {
  128. if (priority_map[index].value == priority) {
  129. return priority_map[index].name;
  130. }
  131. }
  132. return NULL;
  133. }
  134. static const int logger_level_to_syslog_map[] = {
  135. [__LOG_DEBUG] = LOG_DEBUG,
  136. [1] = LOG_INFO, /* Only kept for backwards compatibility */
  137. [__LOG_NOTICE] = LOG_NOTICE,
  138. [__LOG_WARNING] = LOG_WARNING,
  139. [__LOG_ERROR] = LOG_ERR,
  140. [__LOG_VERBOSE] = LOG_DEBUG,
  141. [__LOG_DTMF] = LOG_DEBUG,
  142. };
  143. int ast_syslog_priority_from_loglevel(int level)
  144. {
  145. /* First 16 levels are reserved for system use.
  146. * Default to using LOG_NOTICE for dynamic logging.
  147. */
  148. if (level >= 16 && level < ASTNUMLOGLEVELS) {
  149. return LOG_NOTICE;
  150. }
  151. if (level < 0 || level >= ARRAY_LEN(logger_level_to_syslog_map)) {
  152. return -1;
  153. }
  154. return logger_level_to_syslog_map[level];
  155. }