[svn-commits] seanbright: trunk r203508 - in /trunk: include/asterisk/ main/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Jun 25 18:54:06 CDT 2009


Author: seanbright
Date: Thu Jun 25 18:54:03 2009
New Revision: 203508

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=203508
Log:
Move syslog utility functions into a separate file so they can be re-used.

This has the pleasant side effect of cleaning up the header inclusion process
in logger.c.

Added:
    trunk/include/asterisk/syslog.h   (with props)
    trunk/main/syslog.c   (with props)
Modified:
    trunk/main/logger.c

Added: trunk/include/asterisk/syslog.h
URL: http://svn.asterisk.org/svn-view/asterisk/trunk/include/asterisk/syslog.h?view=auto&rev=203508
==============================================================================
--- trunk/include/asterisk/syslog.h (added)
+++ trunk/include/asterisk/syslog.h Thu Jun 25 18:54:03 2009
@@ -1,0 +1,71 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, malleable, LLC.
+ *
+ * Sean Bright <sean at 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 syslog.h
+ * \brief Syslog support functions for Asterisk logging.
+ */
+
+#ifndef _ASTERISK_SYSLOG_H
+#define _ASTERISK_SYSLOG_H
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+/*!
+ * \since 1.6.3
+ * \brief Maps a syslog facility name from a string to a syslog facility
+ *        constant.
+ *
+ * \param facility Facility name to map (i.e. "daemon")
+ *
+ * \retval syslog facility constant (i.e. LOG_DAEMON) if found
+ * \retval -1 if facility is not found
+ */
+int ast_syslog_facility(const char *facility);
+
+/*!
+ * \since 1.6.3
+ * \brief Maps a syslog priority name from a string to a syslog priority
+ *        constant.
+ *
+ * \param priority Priority name to map (i.e. "notice")
+ *
+ * \retval syslog priority constant (i.e. LOG_NOTICE) if found
+ * \retval -1 if priority is not found
+ */
+int ast_syslog_priority(const char *priority);
+
+/*!
+ * \since 1.6.3
+ * \brief Maps an Asterisk log level (i.e. LOG_ERROR) to a syslog priority
+ *        constant.
+ *
+ * \param level Asterisk log level constant (i.e. LOG_ERROR)
+ *
+ * \retval syslog priority constant (i.e. LOG_ERR) if found
+ * \retval -1 if priority is not found
+ */
+int ast_syslog_priority_from_loglevel(int level);
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif /* _ASTERISK_SYSLOG_H */

Propchange: trunk/include/asterisk/syslog.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/include/asterisk/syslog.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: trunk/include/asterisk/syslog.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: trunk/main/logger.c
URL: http://svn.asterisk.org/svn-view/asterisk/trunk/main/logger.c?view=diff&rev=203508&r1=203507&r2=203508
==============================================================================
--- trunk/main/logger.c (original)
+++ trunk/main/logger.c Thu Jun 25 18:54:03 2009
@@ -19,54 +19,22 @@
 /*! \file
  *
  * \brief Asterisk Logger
- * 
+ *
  * Logging routines
  *
  * \author Mark Spencer <markster at digium.com>
  */
 
-/*
- * define _ASTERISK_LOGGER_H to prevent the inclusion of logger.h;
- * it redefines LOG_* which we need to define syslog_level_map.
- * later, we force the inclusion of logger.h again.
- */
-#define _ASTERISK_LOGGER_H
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-/*
- * WARNING: additional #include directives should NOT be placed here, they 
- * should be placed AFTER '#undef _ASTERISK_LOGGER_H' below
- */
+/* When we include logger.h again it will trample on some stuff in syslog.h, but
+ * nothing we care about in here. */
+#include <syslog.h>
+
 #include "asterisk/_private.h"
 #include "asterisk/paths.h"	/* use ast_config_AST_LOG_DIR */
-#include <signal.h>
-#include <time.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#ifdef HAVE_BKTR
-#include <execinfo.h>
-#define MAX_BACKTRACE_FRAMES 20
-#endif
-
-#define SYSLOG_NAMES /* so we can map syslog facilities names to their numeric values,
-		        from <syslog.h> which is included by logger.h */
-#include <syslog.h>
-
-static const int syslog_level_map[] = {
-	LOG_DEBUG,
-	LOG_INFO,    /* arbitrary equivalent of LOG_EVENT */
-	LOG_NOTICE,
-	LOG_WARNING,
-	LOG_ERR,
-	LOG_DEBUG,
-	LOG_DEBUG
-};
-
-#define SYSLOG_NLEVELS sizeof(syslog_level_map) / sizeof(int)
-
-#undef _ASTERISK_LOGGER_H	/* now include logger.h */
 #include "asterisk/logger.h"
 #include "asterisk/lock.h"
 #include "asterisk/channel.h"
@@ -79,6 +47,16 @@
 #include "asterisk/strings.h"
 #include "asterisk/pbx.h"
 #include "asterisk/app.h"
+#include "asterisk/syslog.h"
+
+#include <signal.h>
+#include <time.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#ifdef HAVE_BKTR
+#include <execinfo.h>
+#define MAX_BACKTRACE_FRAMES 20
+#endif
 
 #if defined(__linux__) && !defined(__NR_gettid)
 #include <asm/unistd.h>
@@ -256,9 +234,6 @@
 {
 	struct logchannel *chan;
 	char *facility;
-#ifndef SOLARIS
-	CODE *cptr;
-#endif
 
 	if (ast_strlen_zero(channel) || !(chan = ast_calloc(1, sizeof(*chan) + strlen(components) + 1)))
 		return NULL;
@@ -278,61 +253,9 @@
 			facility = "local0";
 		}
 
-#ifndef SOLARIS
-		/*
- 		* Walk through the list of facilitynames (defined in sys/syslog.h)
-		* to see if we can find the one we have been given
-		*/
-		chan->facility = -1;
- 		cptr = facilitynames;
-		while (cptr->c_name) {
-			if (!strcasecmp(facility, cptr->c_name)) {
-		 		chan->facility = cptr->c_val;
-				break;
-			}
-			cptr++;
-		}
-#else
-		chan->facility = -1;
-		if (!strcasecmp(facility, "kern")) 
-			chan->facility = LOG_KERN;
-		else if (!strcasecmp(facility, "USER")) 
-			chan->facility = LOG_USER;
-		else if (!strcasecmp(facility, "MAIL")) 
-			chan->facility = LOG_MAIL;
-		else if (!strcasecmp(facility, "DAEMON")) 
-			chan->facility = LOG_DAEMON;
-		else if (!strcasecmp(facility, "AUTH")) 
-			chan->facility = LOG_AUTH;
-		else if (!strcasecmp(facility, "SYSLOG")) 
-			chan->facility = LOG_SYSLOG;
-		else if (!strcasecmp(facility, "LPR")) 
-			chan->facility = LOG_LPR;
-		else if (!strcasecmp(facility, "NEWS")) 
-			chan->facility = LOG_NEWS;
-		else if (!strcasecmp(facility, "UUCP")) 
-			chan->facility = LOG_UUCP;
-		else if (!strcasecmp(facility, "CRON")) 
-			chan->facility = LOG_CRON;
-		else if (!strcasecmp(facility, "LOCAL0")) 
-			chan->facility = LOG_LOCAL0;
-		else if (!strcasecmp(facility, "LOCAL1")) 
-			chan->facility = LOG_LOCAL1;
-		else if (!strcasecmp(facility, "LOCAL2")) 
-			chan->facility = LOG_LOCAL2;
-		else if (!strcasecmp(facility, "LOCAL3")) 
-			chan->facility = LOG_LOCAL3;
-		else if (!strcasecmp(facility, "LOCAL4")) 
-			chan->facility = LOG_LOCAL4;
-		else if (!strcasecmp(facility, "LOCAL5")) 
-			chan->facility = LOG_LOCAL5;
-		else if (!strcasecmp(facility, "LOCAL6")) 
-			chan->facility = LOG_LOCAL6;
-		else if (!strcasecmp(facility, "LOCAL7")) 
-			chan->facility = LOG_LOCAL7;
-#endif /* Solaris */
-
-		if (0 > chan->facility) {
+		chan->facility = ast_syslog_facility(facility);
+
+		if (chan->facility < 0) {
 			fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
 			ast_free(chan);
 			return NULL;
@@ -843,8 +766,9 @@
 static void ast_log_vsyslog(struct logmsg *msg)
 {
 	char buf[BUFSIZ];
-
-	if (msg->level >= SYSLOG_NLEVELS) {
+	int syslog_level = ast_syslog_priority_from_loglevel(msg->level);
+
+	if (syslog_level < 0) {
 		/* we are locked here, so cannot ast_log() */
 		fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", msg->level);
 		return;
@@ -862,7 +786,7 @@
 	}
 
 	term_strip(buf, buf, strlen(buf) + 1);
-	syslog(syslog_level_map[msg->level], "%s", buf);
+	syslog(syslog_level, "%s", buf);
 }
 
 /*! \brief Print a normal log message to the channels */

Added: trunk/main/syslog.c
URL: http://svn.asterisk.org/svn-view/asterisk/trunk/main/syslog.c?view=auto&rev=203508
==============================================================================
--- trunk/main/syslog.c (added)
+++ trunk/main/syslog.c Thu Jun 25 18:54:03 2009
@@ -1,0 +1,120 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, malleable, LLC.
+ *
+ * Sean Bright <sean at 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 at malleable.com>
+ */
+
+#include "asterisk.h"
+#include "asterisk/utils.h"
+#include "asterisk/syslog.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <syslog.h>
+
+int ast_syslog_facility(const char *facility)
+{
+    if (!strcasecmp(facility, "KERN")) {
+		return LOG_KERN;
+    } else if (!strcasecmp(facility, "USER")) {
+		return LOG_USER;
+    } else if (!strcasecmp(facility, "MAIL")) {
+		return LOG_MAIL;
+    } else if (!strcasecmp(facility, "DAEMON")) {
+		return LOG_DAEMON;
+    } else if (!strcasecmp(facility, "AUTH")) {
+		return LOG_AUTH;
+	} else if (!strcasecmp(facility, "AUTHPRIV")) {
+		return LOG_AUTHPRIV;
+    } else if (!strcasecmp(facility, "SYSLOG")) {
+		return LOG_SYSLOG;
+	} else if (!strcasecmp(facility, "SECURITY")) {
+		return LOG_AUTH;
+	} else if (!strcasecmp(facility, "FTP")) {
+		return LOG_FTP;
+    } else if (!strcasecmp(facility, "LPR")) {
+		return LOG_LPR;
+    } else if (!strcasecmp(facility, "NEWS")) {
+		return LOG_NEWS;
+    } else if (!strcasecmp(facility, "UUCP")) {
+		return LOG_UUCP;
+    } else if (!strcasecmp(facility, "CRON")) {
+		return LOG_CRON;
+    } else if (!strcasecmp(facility, "LOCAL0")) {
+		return LOG_LOCAL0;
+    } else if (!strcasecmp(facility, "LOCAL1")) {
+		return LOG_LOCAL1;
+    } else if (!strcasecmp(facility, "LOCAL2")) {
+		return LOG_LOCAL2;
+    } else if (!strcasecmp(facility, "LOCAL3")) {
+		return LOG_LOCAL3;
+    } else if (!strcasecmp(facility, "LOCAL4")) {
+		return LOG_LOCAL4;
+    } else if (!strcasecmp(facility, "LOCAL5")) {
+		return LOG_LOCAL5;
+    } else if (!strcasecmp(facility, "LOCAL6")) {
+		return LOG_LOCAL6;
+    } else if (!strcasecmp(facility, "LOCAL7")) {
+		return LOG_LOCAL7;
+    }
+
+    return -1;
+}
+
+int ast_syslog_priority(const char *priority)
+{
+    if (!strcasecmp(priority, "ALERT")) {
+		return LOG_ALERT;
+    } else if (!strcasecmp(priority, "CRIT")) {
+		return LOG_CRIT;
+    } else if (!strcasecmp(priority, "DEBUG")) {
+		return LOG_DEBUG;
+    } else if (!strcasecmp(priority, "EMERG")) {
+		return LOG_EMERG;
+    } else if (!strcasecmp(priority, "ERR")) {
+		return LOG_ERR;
+    } else if (!strcasecmp(priority, "INFO")) {
+		return LOG_INFO;
+    } else if (!strcasecmp(priority, "NOTICE")) {
+		return LOG_NOTICE;
+    } else if (!strcasecmp(priority, "WARNING")) {
+		return LOG_WARNING;
+    }
+
+    return -1;
+}
+
+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];
+}

Propchange: trunk/main/syslog.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/main/syslog.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: trunk/main/syslog.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the svn-commits mailing list