Debian Codegraph @

main/syslog.c

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