Tue Aug 20 16:34:39 2013

Asterisk developer's documentation


syslog.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2009, malleable, LLC.
00005  *
00006  * Sean Bright <sean@malleable.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Asterisk Syslog Utility Functions
00022  * \author Sean Bright <sean@malleable.com>
00023  */
00024 
00025 /*** MODULEINFO
00026    <support_level>core</support_level>
00027  ***/
00028 
00029 #include "asterisk.h"
00030 #include "asterisk/utils.h"
00031 #include "asterisk/syslog.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
00034 
00035 #include <syslog.h>
00036 
00037 static const struct {
00038    const char *name;
00039    int value;
00040 } facility_map[] = {
00041    /* POSIX only specifies USER and LOCAL0 - LOCAL7 */
00042    { "user",     LOG_USER     },
00043    { "local0",   LOG_LOCAL0   },
00044    { "local1",   LOG_LOCAL1   },
00045    { "local2",   LOG_LOCAL2   },
00046    { "local3",   LOG_LOCAL3   },
00047    { "local4",   LOG_LOCAL4   },
00048    { "local5",   LOG_LOCAL5   },
00049    { "local6",   LOG_LOCAL6   },
00050    { "local7",   LOG_LOCAL7   },
00051 #if defined(HAVE_SYSLOG_FACILITY_LOG_KERN)
00052    { "kern",     LOG_KERN     },
00053 #endif
00054 #if defined(HAVE_SYSLOG_FACILITY_LOG_MAIL)
00055    { "mail",     LOG_MAIL     },
00056 #endif
00057 #if defined(HAVE_SYSLOG_FACILITY_LOG_DAEMON)
00058    { "daemon",   LOG_DAEMON   },
00059 #endif
00060 #if defined(HAVE_SYSLOG_FACILITY_LOG_AUTH)
00061    { "auth",     LOG_AUTH     },
00062    { "security", LOG_AUTH     },
00063 #endif
00064 #if defined(HAVE_SYSLOG_FACILITY_LOG_AUTHPRIV)
00065    { "authpriv", LOG_AUTHPRIV },
00066 #endif
00067 #if defined(HAVE_SYSLOG_FACILITY_LOG_SYSLOG)
00068    { "syslog",   LOG_SYSLOG   },
00069 #endif
00070 #if defined(HAVE_SYSLOG_FACILITY_LOG_FTP)
00071    { "ftp",      LOG_FTP      },
00072 #endif
00073 #if defined(HAVE_SYSLOG_FACILITY_LOG_LPR)
00074    { "lpr",      LOG_LPR      },
00075 #endif
00076 #if defined(HAVE_SYSLOG_FACILITY_LOG_NEWS)
00077    { "news",     LOG_NEWS     },
00078 #endif
00079 #if defined(HAVE_SYSLOG_FACILITY_LOG_UUCP)
00080    { "uucp",     LOG_UUCP     },
00081 #endif
00082 #if defined(HAVE_SYSLOG_FACILITY_LOG_CRON)
00083    { "cron",     LOG_CRON     },
00084 #endif
00085 };
00086 
00087 int ast_syslog_facility(const char *facility)
00088 {
00089    int index;
00090 
00091    for (index = 0; index < ARRAY_LEN(facility_map); index++) {
00092       if (!strcasecmp(facility_map[index].name, facility)) {
00093          return facility_map[index].value;
00094       }
00095    }
00096 
00097    return -1;
00098 }
00099 
00100 const char *ast_syslog_facility_name(int facility)
00101 {
00102    int index;
00103 
00104    for (index = 0; index < ARRAY_LEN(facility_map); index++) {
00105       if (facility_map[index].value == facility) {
00106          return facility_map[index].name;
00107       }
00108    }
00109 
00110    return NULL;
00111 }
00112 
00113 static const struct {
00114    const char *name;
00115    int value;
00116 } priority_map[] = {
00117    { "alert",   LOG_ALERT   },
00118    { "crit",    LOG_CRIT    },
00119    { "debug",   LOG_DEBUG   },
00120    { "emerg",   LOG_EMERG   },
00121    { "err",     LOG_ERR     },
00122    { "error",   LOG_ERR     },
00123    { "info",    LOG_INFO    },
00124    { "notice",  LOG_NOTICE  },
00125    { "warning", LOG_WARNING },
00126 };
00127 
00128 int ast_syslog_priority(const char *priority)
00129 {
00130    int index;
00131 
00132    for (index = 0; index < ARRAY_LEN(priority_map); index++) {
00133       if (!strcasecmp(priority_map[index].name, priority)) {
00134          return priority_map[index].value;
00135       }
00136    }
00137 
00138    return -1;
00139 }
00140 
00141 const char *ast_syslog_priority_name(int priority)
00142 {
00143    int index;
00144 
00145    for (index = 0; index < ARRAY_LEN(priority_map); index++) {
00146       if (priority_map[index].value == priority) {
00147          return priority_map[index].name;
00148       }
00149    }
00150 
00151    return NULL;
00152 }
00153 
00154 static const int logger_level_to_syslog_map[] = {
00155    [__LOG_DEBUG]   = LOG_DEBUG,
00156    [1]             = LOG_INFO, /* Only kept for backwards compatibility */
00157    [__LOG_NOTICE]  = LOG_NOTICE,
00158    [__LOG_WARNING] = LOG_WARNING,
00159    [__LOG_ERROR]   = LOG_ERR,
00160    [__LOG_VERBOSE] = LOG_DEBUG,
00161    [__LOG_DTMF]    = LOG_DEBUG,
00162 };
00163 
00164 int ast_syslog_priority_from_loglevel(int level)
00165 {
00166    if (level < 0 || level >= ARRAY_LEN(logger_level_to_syslog_map)) {
00167       return -1;
00168    }
00169    return logger_level_to_syslog_map[level];
00170 }

Generated on 20 Aug 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1