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