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