blob: 20908564d849184bb407a17053d733371b43b36d [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 *
Harald Welte46cfd772011-02-17 15:56:56 +010019 */
20
Harald Welte18fc4652011-08-17 14:14:17 +020021/*! \addtogroup logging
22 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020023 * \file logging_syslog.c */
Harald Welte18fc4652011-08-17 14:14:17 +020024
Harald Welte46cfd772011-02-17 15:56:56 +010025#include "../config.h"
26
Harald Welte28222962011-02-18 20:37:04 +010027#ifdef HAVE_SYSLOG_H
28
Harald Welte46cfd772011-02-17 15:56:56 +010029#include <stdarg.h>
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33#include <syslog.h>
34
35#ifdef HAVE_STRINGS_H
36#include <strings.h>
37#endif
38
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010039#include <osmocom/core/talloc.h>
40#include <osmocom/core/utils.h>
41#include <osmocom/core/logging.h>
Harald Welte46cfd772011-02-17 15:56:56 +010042
Sylvain Munaute45e6992011-11-20 08:59:36 +010043static int logp2syslog_level(unsigned int level)
Harald Welte46cfd772011-02-17 15:56:56 +010044{
45 if (level >= LOGL_FATAL)
46 return LOG_CRIT;
47 else if (level >= LOGL_ERROR)
48 return LOG_ERR;
49 else if (level >= LOGL_NOTICE)
50 return LOG_NOTICE;
51 else if (level >= LOGL_INFO)
52 return LOG_INFO;
53 else
54 return LOG_DEBUG;
55}
56
57static void _syslog_output(struct log_target *target,
58 unsigned int level, const char *log)
59{
60 syslog(logp2syslog_level(level), "%s", log);
61}
62
Neels Hofmeyr87e45502017-06-20 00:17:59 +020063/*! Create a new logging target for syslog logging
Harald Welte18fc4652011-08-17 14:14:17 +020064 * \param[in] ident syslog string identifier
65 * \param[in] option syslog options
66 * \param[in] facility syslog facility
67 * \returns Log target in case of success, NULL in case of error
68 */
Harald Welte46cfd772011-02-17 15:56:56 +010069struct log_target *log_target_create_syslog(const char *ident, int option,
70 int facility)
71{
72 struct log_target *target;
73
74 target = log_target_create();
75 if (!target)
76 return NULL;
77
Harald Welte28222962011-02-18 20:37:04 +010078 target->tgt_syslog.facility = facility;
79 target->type = LOG_TGT_TYPE_SYSLOG;
Harald Welte46cfd772011-02-17 15:56:56 +010080 target->output = _syslog_output;
81
82 openlog(ident, option, facility);
83
84 return target;
85}
Harald Welte28222962011-02-18 20:37:04 +010086
87#endif /* HAVE_SYSLOG_H */
Harald Welte18fc4652011-08-17 14:14:17 +020088
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020089/* @} */