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