Sat Mar 10 01:54:27 2012

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 #include "asterisk.h"
00026 #include "asterisk/utils.h"
00027 #include "asterisk/syslog.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 203605 $")
00030 
00031 #include <syslog.h>
00032 
00033 static const struct {
00034    const char *name;
00035    int value;
00036 } facility_map[] = {
00037    /* POSIX only specifies USER and LOCAL0 - LOCAL7 */
00038    { "user",     LOG_USER     },
00039    { "local0",   LOG_LOCAL0   },
00040    { "local1",   LOG_LOCAL1   },
00041    { "local2",   LOG_LOCAL2   },
00042    { "local3",   LOG_LOCAL3   },
00043    { "local4",   LOG_LOCAL4   },
00044    { "local5",   LOG_LOCAL5   },
00045    { "local6",   LOG_LOCAL6   },
00046    { "local7",   LOG_LOCAL7   },
00047 #if defined(HAVE_SYSLOG_FACILITY_LOG_KERN)
00048    { "kern",     LOG_KERN     },
00049 #endif
00050 #if defined(HAVE_SYSLOG_FACILITY_LOG_MAIL)
00051    { "mail",     LOG_MAIL     },
00052 #endif
00053 #if defined(HAVE_SYSLOG_FACILITY_LOG_DAEMON)
00054    { "daemon",   LOG_DAEMON   },
00055 #endif
00056 #if defined(HAVE_SYSLOG_FACILITY_LOG_AUTH)
00057    { "auth",     LOG_AUTH     },
00058    { "security", LOG_AUTH     },
00059 #endif
00060 #if defined(HAVE_SYSLOG_FACILITY_LOG_AUTHPRIV)
00061    { "authpriv", LOG_AUTHPRIV },
00062 #endif
00063 #if defined(HAVE_SYSLOG_FACILITY_LOG_SYSLOG)
00064    { "syslog",   LOG_SYSLOG   },
00065 #endif
00066 #if defined(HAVE_SYSLOG_FACILITY_LOG_FTP)
00067    { "ftp",      LOG_FTP      },
00068 #endif
00069 #if defined(HAVE_SYSLOG_FACILITY_LOG_LPR)
00070    { "lpr",      LOG_LPR      },
00071 #endif
00072 #if defined(HAVE_SYSLOG_FACILITY_LOG_NEWS)
00073    { "news",     LOG_NEWS     },
00074 #endif
00075 #if defined(HAVE_SYSLOG_FACILITY_LOG_UUCP)
00076    { "uucp",     LOG_UUCP     },
00077 #endif
00078 #if defined(HAVE_SYSLOG_FACILITY_LOG_CRON)
00079    { "cron",     LOG_CRON     },
00080 #endif
00081 };
00082 
00083 int ast_syslog_facility(const char *facility)
00084 {
00085    int index;
00086 
00087    for (index = 0; index < ARRAY_LEN(facility_map); index++) {
00088       if (!strcasecmp(facility_map[index].name, facility)) {
00089          return facility_map[index].value;
00090       }
00091    }
00092 
00093    return -1;
00094 }
00095 
00096 const char *ast_syslog_facility_name(int facility)
00097 {
00098    int index;
00099 
00100    for (index = 0; index < ARRAY_LEN(facility_map); index++) {
00101       if (facility_map[index].value == facility) {
00102          return facility_map[index].name;
00103       }
00104    }
00105 
00106    return NULL;
00107 }
00108 
00109 static const struct {
00110    const char *name;
00111    int value;
00112 } priority_map[] = {
00113    { "alert",   LOG_ALERT   },
00114    { "crit",    LOG_CRIT    },
00115    { "debug",   LOG_DEBUG   },
00116    { "emerg",   LOG_EMERG   },
00117    { "err",     LOG_ERR     },
00118    { "error",   LOG_ERR     },
00119    { "info",    LOG_INFO    },
00120    { "notice",  LOG_NOTICE  },
00121    { "warning", LOG_WARNING },
00122 };
00123 
00124 int ast_syslog_priority(const char *priority)
00125 {
00126    int index;
00127 
00128    for (index = 0; index < ARRAY_LEN(priority_map); index++) {
00129       if (!strcasecmp(priority_map[index].name, priority)) {
00130          return priority_map[index].value;
00131       }
00132    }
00133 
00134    return -1;
00135 }
00136 
00137 const char *ast_syslog_priority_name(int priority)
00138 {
00139    int index;
00140 
00141    for (index = 0; index < ARRAY_LEN(priority_map); index++) {
00142       if (priority_map[index].value == priority) {
00143          return priority_map[index].name;
00144       }
00145    }
00146 
00147    return NULL;
00148 }
00149 
00150 static const int logger_level_to_syslog_map[] = {
00151    [__LOG_DEBUG]   = LOG_DEBUG,
00152    [1]             = LOG_INFO, /* Only kept for backwards compatibility */
00153    [__LOG_NOTICE]  = LOG_NOTICE,
00154    [__LOG_WARNING] = LOG_WARNING,
00155    [__LOG_ERROR]   = LOG_ERR,
00156    [__LOG_VERBOSE] = LOG_DEBUG,
00157    [__LOG_DTMF]    = LOG_DEBUG,
00158 };
00159 
00160 int ast_syslog_priority_from_loglevel(int level)
00161 {
00162    if (level < 0 || level >= ARRAY_LEN(logger_level_to_syslog_map)) {
00163       return -1;
00164    }
00165    return logger_level_to_syslog_map[level];
00166 }

Generated on Sat Mar 10 01:54:27 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7