blob: f980689d19aaa746ad53b27ad6cdc08446ba1461 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file logging_syslog.c
2 * Syslog logging support code. */
3/*
4 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
Harald Welte46cfd772011-02-17 15:56:56 +01005 * All Rights Reserved
6 *
Harald Weltee08da972017-11-13 01:00:26 +09007 * SPDX-License-Identifier: GPL-2.0+
8 *
Harald Welte46cfd772011-02-17 15:56:56 +01009 * 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 Welte18fc4652011-08-17 14:14:17 +020025/*! \addtogroup logging
26 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020027 * \file logging_syslog.c */
Harald Welte18fc4652011-08-17 14:14:17 +020028
Harald Welte46cfd772011-02-17 15:56:56 +010029#include "../config.h"
30
Harald Welte28222962011-02-18 20:37:04 +010031#ifdef HAVE_SYSLOG_H
32
Harald Welte46cfd772011-02-17 15:56:56 +010033#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 Ayuso83419342011-03-22 16:36:13 +010043#include <osmocom/core/talloc.h>
44#include <osmocom/core/utils.h>
45#include <osmocom/core/logging.h>
Harald Welte46cfd772011-02-17 15:56:56 +010046
Sylvain Munaute45e6992011-11-20 08:59:36 +010047static int logp2syslog_level(unsigned int level)
Harald Welte46cfd772011-02-17 15:56:56 +010048{
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
61static 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 Hofmeyr87e45502017-06-20 00:17:59 +020067/*! Create a new logging target for syslog logging
Harald Welte18fc4652011-08-17 14:14:17 +020068 * \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 Welte46cfd772011-02-17 15:56:56 +010073struct 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 Welte28222962011-02-18 20:37:04 +010082 target->tgt_syslog.facility = facility;
83 target->type = LOG_TGT_TYPE_SYSLOG;
Harald Welte46cfd772011-02-17 15:56:56 +010084 target->output = _syslog_output;
85
86 openlog(ident, option, facility);
87
88 return target;
89}
Harald Welte28222962011-02-18 20:37:04 +010090
91#endif /* HAVE_SYSLOG_H */
Harald Welte18fc4652011-08-17 14:14:17 +020092
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020093/* @} */