aboutsummaryrefslogtreecommitdiffstats
path: root/main/syslog.c
blob: 88f554e3344568bd388c6f96b5cfab3749d45e51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2009, malleable, LLC.
 *
 * Sean Bright <sean@malleable.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Asterisk Syslog Utility Functions
 * \author Sean Bright <sean@malleable.com>
 */

#include "asterisk.h"
#include "asterisk/utils.h"
#include "asterisk/syslog.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include <syslog.h>

static const struct {
	const char *name;
	int value;
} facility_map[] = {
	/* POSIX only specifies USER and LOCAL0 - LOCAL7 */
	{ "user",     LOG_USER     },
	{ "local0",   LOG_LOCAL0   },
	{ "local1",   LOG_LOCAL1   },
	{ "local2",   LOG_LOCAL2   },
	{ "local3",   LOG_LOCAL3   },
	{ "local4",   LOG_LOCAL4   },
	{ "local5",   LOG_LOCAL5   },
	{ "local6",   LOG_LOCAL6   },
	{ "local7",   LOG_LOCAL7   },
#if defined(HAVE_SYSLOG_FACILITY_LOG_KERN)
	{ "kern",     LOG_KERN     },
#endif
#if defined(HAVE_SYSLOG_FACILITY_LOG_MAIL)
	{ "mail",     LOG_MAIL     },
#endif
#if defined(HAVE_SYSLOG_FACILITY_LOG_DAEMON)
	{ "daemon",   LOG_DAEMON   },
#endif
#if defined(HAVE_SYSLOG_FACILITY_LOG_AUTH)
	{ "auth",     LOG_AUTH     },
	{ "security", LOG_AUTH     },
#endif
#if defined(HAVE_SYSLOG_FACILITY_LOG_AUTHPRIV)
	{ "authpriv", LOG_AUTHPRIV },
#endif
#if defined(HAVE_SYSLOG_FACILITY_LOG_SYSLOG)
	{ "syslog",   LOG_SYSLOG   },
#endif
#if defined(HAVE_SYSLOG_FACILITY_LOG_FTP)
	{ "ftp",      LOG_FTP      },
#endif
#if defined(HAVE_SYSLOG_FACILITY_LOG_LPR)
	{ "lpr",      LOG_LPR      },
#endif
#if defined(HAVE_SYSLOG_FACILITY_LOG_NEWS)
	{ "news",     LOG_NEWS     },
#endif
#if defined(HAVE_SYSLOG_FACILITY_LOG_UUCP)
	{ "uucp",     LOG_UUCP     },
#endif
#if defined(HAVE_SYSLOG_FACILITY_LOG_CRON)
	{ "cron",     LOG_CRON     },
#endif
};

int ast_syslog_facility(const char *facility)
{
	int index;

	for (index = 0; index < ARRAY_LEN(facility_map); index++) {
		if (!strcasecmp(facility_map[index].name, facility)) {
			return facility_map[index].value;
		}
	}

	return -1;
}

const char *ast_syslog_facility_name(int facility)
{
	int index;

	for (index = 0; index < ARRAY_LEN(facility_map); index++) {
		if (facility_map[index].value == facility) {
			return facility_map[index].name;
		}
	}

	return NULL;
}

static const struct {
	const char *name;
	int value;
} priority_map[] = {
	{ "alert",   LOG_ALERT   },
	{ "crit",    LOG_CRIT    },
	{ "debug",   LOG_DEBUG   },
	{ "emerg",   LOG_EMERG   },
	{ "err",     LOG_ERR     },
	{ "error",   LOG_ERR     },
	{ "info",    LOG_INFO    },
	{ "notice",  LOG_NOTICE  },
	{ "warning", LOG_WARNING },
};

int ast_syslog_priority(const char *priority)
{
	int index;

	for (index = 0; index < ARRAY_LEN(priority_map); index++) {
		if (!strcasecmp(priority_map[index].name, priority)) {
			return priority_map[index].value;
		}
	}

	return -1;
}

const char *ast_syslog_priority_name(int priority)
{
	int index;

	for (index = 0; index < ARRAY_LEN(priority_map); index++) {
		if (priority_map[index].value == priority) {
			return priority_map[index].name;
		}
	}

	return NULL;
}

static const int logger_level_to_syslog_map[] = {
	[__LOG_DEBUG]   = LOG_DEBUG,
	[1]             = LOG_INFO, /* Only kept for backwards compatibility */
	[__LOG_NOTICE]  = LOG_NOTICE,
	[__LOG_WARNING] = LOG_WARNING,
	[__LOG_ERROR]   = LOG_ERR,
	[__LOG_VERBOSE] = LOG_DEBUG,
	[__LOG_DTMF]    = LOG_DEBUG,
};

int ast_syslog_priority_from_loglevel(int level)
{
	if (level < 0 || level >= ARRAY_LEN(logger_level_to_syslog_map)) {
		return -1;
	}
	return logger_level_to_syslog_map[level];
}