| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | static const struct { | 
					
						
							|  |  |  | 	const char *name; | 
					
						
							|  |  |  | 	int value; | 
					
						
							|  |  |  | } facility_map[] = { | 
					
						
							|  |  |  | 	/* POSIX only specifies USER and LOCAL0 - LOCAL7 */ | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "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   }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_KERN)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "kern",     LOG_KERN     }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_MAIL)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "mail",     LOG_MAIL     }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_DAEMON)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "daemon",   LOG_DAEMON   }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_AUTH)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "auth",     LOG_AUTH     }, | 
					
						
							|  |  |  | 	{ "security", LOG_AUTH     }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_AUTHPRIV)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "authpriv", LOG_AUTHPRIV }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_SYSLOG)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "syslog",   LOG_SYSLOG   }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_FTP)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "ftp",      LOG_FTP      }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_LPR)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "lpr",      LOG_LPR      }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_NEWS)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "news",     LOG_NEWS     }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_UUCP)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "uucp",     LOG_UUCP     }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_SYSLOG_FACILITY_LOG_CRON)
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "cron",     LOG_CRON     }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | int ast_syslog_facility(const char *facility) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | 	int index; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (index = 0; index < ARRAY_LEN(facility_map); index++) { | 
					
						
							|  |  |  | 		if (!strcasecmp(facility_map[index].name, facility)) { | 
					
						
							|  |  |  | 			return facility_map[index].value; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-06-26 00:21:09 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-26 00:21:09 +00:00
										 |  |  | 	return -1; | 
					
						
							| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | static const struct { | 
					
						
							|  |  |  | 	const char *name; | 
					
						
							|  |  |  | 	int value; | 
					
						
							|  |  |  | } priority_map[] = { | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 	{ "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 }, | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | int ast_syslog_priority(const char *priority) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-06-26 03:06:06 +00:00
										 |  |  | 	int index; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (index = 0; index < ARRAY_LEN(priority_map); index++) { | 
					
						
							|  |  |  | 		if (!strcasecmp(priority_map[index].name, priority)) { | 
					
						
							|  |  |  | 			return priority_map[index].value; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-06-26 00:21:09 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-26 00:21:09 +00:00
										 |  |  | 	return -1; | 
					
						
							| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-26 13:00:35 +00:00
										 |  |  | 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | static const int logger_level_to_syslog_map[] = { | 
					
						
							|  |  |  | 	[__LOG_DEBUG]   = LOG_DEBUG, | 
					
						
							| 
									
										
										
										
											2009-06-26 00:23:55 +00:00
										 |  |  | 	[1]             = LOG_INFO, /* Only kept for backwards compatibility */ | 
					
						
							| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | 	[__LOG_NOTICE]  = LOG_NOTICE, | 
					
						
							|  |  |  | 	[__LOG_WARNING] = LOG_WARNING, | 
					
						
							|  |  |  | 	[__LOG_ERROR]   = LOG_ERR, | 
					
						
							|  |  |  | 	[__LOG_VERBOSE] = LOG_DEBUG, | 
					
						
							| 
									
										
										
										
											2009-06-26 00:23:55 +00:00
										 |  |  | 	[__LOG_DTMF]    = LOG_DEBUG, | 
					
						
							| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-26 00:21:09 +00:00
										 |  |  | int ast_syslog_priority_from_loglevel(int level) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-06-25 23:54:03 +00:00
										 |  |  | 	if (level < 0 || level >= ARRAY_LEN(logger_level_to_syslog_map)) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return logger_level_to_syslog_map[level]; | 
					
						
							|  |  |  | } |