Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file logging_syslog.c |
| 2 | * Syslog logging support code. */ |
| 3 | /* |
| 4 | * (C) 2011 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 23 | /*! \addtogroup logging |
| 24 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 25 | * \file logging_syslog.c */ |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 26 | |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 27 | #include "../config.h" |
| 28 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 29 | #ifdef HAVE_SYSLOG_H |
| 30 | |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 31 | #include <stdarg.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <syslog.h> |
| 36 | |
| 37 | #ifdef HAVE_STRINGS_H |
| 38 | #include <strings.h> |
| 39 | #endif |
| 40 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 41 | #include <osmocom/core/talloc.h> |
| 42 | #include <osmocom/core/utils.h> |
| 43 | #include <osmocom/core/logging.h> |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 44 | |
Sylvain Munaut | e45e699 | 2011-11-20 08:59:36 +0100 | [diff] [blame] | 45 | static int logp2syslog_level(unsigned int level) |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 46 | { |
| 47 | if (level >= LOGL_FATAL) |
| 48 | return LOG_CRIT; |
| 49 | else if (level >= LOGL_ERROR) |
| 50 | return LOG_ERR; |
| 51 | else if (level >= LOGL_NOTICE) |
| 52 | return LOG_NOTICE; |
| 53 | else if (level >= LOGL_INFO) |
| 54 | return LOG_INFO; |
| 55 | else |
| 56 | return LOG_DEBUG; |
| 57 | } |
| 58 | |
| 59 | static void _syslog_output(struct log_target *target, |
| 60 | unsigned int level, const char *log) |
| 61 | { |
| 62 | syslog(logp2syslog_level(level), "%s", log); |
| 63 | } |
| 64 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 65 | /*! Create a new logging target for syslog logging |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 66 | * \param[in] ident syslog string identifier |
| 67 | * \param[in] option syslog options |
| 68 | * \param[in] facility syslog facility |
| 69 | * \returns Log target in case of success, NULL in case of error |
| 70 | */ |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 71 | struct log_target *log_target_create_syslog(const char *ident, int option, |
| 72 | int facility) |
| 73 | { |
| 74 | struct log_target *target; |
| 75 | |
| 76 | target = log_target_create(); |
| 77 | if (!target) |
| 78 | return NULL; |
| 79 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 80 | target->tgt_syslog.facility = facility; |
| 81 | target->type = LOG_TGT_TYPE_SYSLOG; |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 82 | target->output = _syslog_output; |
| 83 | |
| 84 | openlog(ident, option, facility); |
| 85 | |
| 86 | return target; |
| 87 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 88 | |
| 89 | #endif /* HAVE_SYSLOG_H */ |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 90 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 91 | /* @} */ |