syslog.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include <syslog.h>
  30. static const struct {
  31. const char *name;
  32. int value;
  33. } facility_map[] = {
  34. /* POSIX only specifies USER and LOCAL0 - LOCAL7 */
  35. { "user", LOG_USER },
  36. { "local0", LOG_LOCAL0 },
  37. { "local1", LOG_LOCAL1 },
  38. { "local2", LOG_LOCAL2 },
  39. { "local3", LOG_LOCAL3 },
  40. { "local4", LOG_LOCAL4 },
  41. { "local5", LOG_LOCAL5 },
  42. { "local6", LOG_LOCAL6 },
  43. { "local7", LOG_LOCAL7 },
  44. #if defined(HAVE_SYSLOG_FACILITY_LOG_KERN)
  45. { "kern", LOG_KERN },
  46. #endif
  47. #if defined(HAVE_SYSLOG_FACILITY_LOG_MAIL)
  48. { "mail", LOG_MAIL },
  49. #endif
  50. #if defined(HAVE_SYSLOG_FACILITY_LOG_DAEMON)
  51. { "daemon", LOG_DAEMON },
  52. #endif
  53. #if defined(HAVE_SYSLOG_FACILITY_LOG_AUTH)
  54. { "auth", LOG_AUTH },
  55. { "security", LOG_AUTH },
  56. #endif
  57. #if defined(HAVE_SYSLOG_FACILITY_LOG_AUTHPRIV)
  58. { "authpriv", LOG_AUTHPRIV },
  59. #endif
  60. #if defined(HAVE_SYSLOG_FACILITY_LOG_SYSLOG)
  61. { "syslog", LOG_SYSLOG },
  62. #endif
  63. #if defined(HAVE_SYSLOG_FACILITY_LOG_FTP)
  64. { "ftp", LOG_FTP },
  65. #endif
  66. #if defined(HAVE_SYSLOG_FACILITY_LOG_LPR)
  67. { "lpr", LOG_LPR },
  68. #endif
  69. #if defined(HAVE_SYSLOG_FACILITY_LOG_NEWS)
  70. { "news", LOG_NEWS },
  71. #endif
  72. #if defined(HAVE_SYSLOG_FACILITY_LOG_UUCP)
  73. { "uucp", LOG_UUCP },
  74. #endif
  75. #if defined(HAVE_SYSLOG_FACILITY_LOG_CRON)
  76. { "cron", LOG_CRON },
  77. #endif
  78. };
  79. int ast_syslog_facility(const char *facility)
  80. {
  81. int index;
  82. for (index = 0; index < ARRAY_LEN(facility_map); index++) {
  83. if (!strcasecmp(facility_map[index].name, facility)) {
  84. return facility_map[index].value;
  85. }
  86. }
  87. return -1;
  88. }
  89. const char *ast_syslog_facility_name(int facility)
  90. {
  91. int index;
  92. for (index = 0; index < ARRAY_LEN(facility_map); index++) {
  93. if (facility_map[index].value == facility) {
  94. return facility_map[index].name;
  95. }
  96. }
  97. return NULL;
  98. }
  99. static const struct {
  100. const char *name;
  101. int value;
  102. } priority_map[] = {
  103. { "alert", LOG_ALERT },
  104. { "crit", LOG_CRIT },
  105. { "debug", LOG_DEBUG },
  106. { "emerg", LOG_EMERG },
  107. { "err", LOG_ERR },
  108. { "error", LOG_ERR },
  109. { "info", LOG_INFO },
  110. { "notice", LOG_NOTICE },
  111. { "warning", LOG_WARNING },
  112. };
  113. int ast_syslog_priority(const char *priority)
  114. {
  115. int index;
  116. for (index = 0; index < ARRAY_LEN(priority_map); index++) {
  117. if (!strcasecmp(priority_map[index].name, priority)) {
  118. return priority_map[index].value;
  119. }
  120. }
  121. return -1;
  122. }
  123. const char *ast_syslog_priority_name(int priority)
  124. {
  125. int index;
  126. for (index = 0; index < ARRAY_LEN(priority_map); index++) {
  127. if (priority_map[index].value == priority) {
  128. return priority_map[index].name;
  129. }
  130. }
  131. return NULL;
  132. }
  133. static const int logger_level_to_syslog_map[] = {
  134. [__LOG_DEBUG] = LOG_DEBUG,
  135. [1] = LOG_INFO, /* Only kept for backwards compatibility */
  136. [__LOG_NOTICE] = LOG_NOTICE,
  137. [__LOG_WARNING] = LOG_WARNING,
  138. [__LOG_ERROR] = LOG_ERR,
  139. [__LOG_VERBOSE] = LOG_DEBUG,
  140. [__LOG_DTMF] = LOG_DEBUG,
  141. };
  142. int ast_syslog_priority_from_loglevel(int level)
  143. {
  144. /* First 16 levels are reserved for system use.
  145. * Default to using LOG_NOTICE for dynamic logging.
  146. */
  147. if (level >= 16 && level < ASTNUMLOGLEVELS) {
  148. return LOG_NOTICE;
  149. }
  150. if (level < 0 || level >= ARRAY_LEN(logger_level_to_syslog_map)) {
  151. return -1;
  152. }
  153. return logger_level_to_syslog_map[level];
  154. }