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