blob: d0d6a9695500c90767ab3af2f0af1d4201227273 [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 *
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 Welte18fc4652011-08-17 14:14:17 +020023/*! \addtogroup logging
24 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020025 * \file logging_syslog.c */
Harald Welte18fc4652011-08-17 14:14:17 +020026
Harald Welte46cfd772011-02-17 15:56:56 +010027#include "../config.h"
28
Harald Welte28222962011-02-18 20:37:04 +010029#ifdef HAVE_SYSLOG_H
30
Harald Welte46cfd772011-02-17 15:56:56 +010031#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 Ayuso83419342011-03-22 16:36:13 +010041#include <osmocom/core/talloc.h>
42#include <osmocom/core/utils.h>
43#include <osmocom/core/logging.h>
Harald Welte46cfd772011-02-17 15:56:56 +010044
Sylvain Munaute45e6992011-11-20 08:59:36 +010045static int logp2syslog_level(unsigned int level)
Harald Welte46cfd772011-02-17 15:56:56 +010046{
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
59static 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 Hofmeyr87e45502017-06-20 00:17:59 +020065/*! Create a new logging target for syslog logging
Harald Welte18fc4652011-08-17 14:14:17 +020066 * \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 Welte46cfd772011-02-17 15:56:56 +010071struct 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 Welte28222962011-02-18 20:37:04 +010080 target->tgt_syslog.facility = facility;
81 target->type = LOG_TGT_TYPE_SYSLOG;
Harald Welte46cfd772011-02-17 15:56:56 +010082 target->output = _syslog_output;
83
84 openlog(ident, option, facility);
85
86 return target;
87}
Harald Welte28222962011-02-18 20:37:04 +010088
89#endif /* HAVE_SYSLOG_H */
Harald Welte18fc4652011-08-17 14:14:17 +020090
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020091/* @} */