blob: 6fe3c719ce4733583c173f4a68165495417dd49a [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
Harald Welte96e2a002017-06-12 21:44:18 +020026/*! \file logging_syslog.c
27 * \brief libosmocore logging output via syslog
28 */
Harald Welte18fc4652011-08-17 14:14:17 +020029
Harald Welte46cfd772011-02-17 15:56:56 +010030#include "../config.h"
31
Harald Welte28222962011-02-18 20:37:04 +010032#ifdef HAVE_SYSLOG_H
33
Harald Welte46cfd772011-02-17 15:56:56 +010034#include <stdarg.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#include <syslog.h>
39
40#ifdef HAVE_STRINGS_H
41#include <strings.h>
42#endif
43
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010044#include <osmocom/core/talloc.h>
45#include <osmocom/core/utils.h>
46#include <osmocom/core/logging.h>
Harald Welte46cfd772011-02-17 15:56:56 +010047
Sylvain Munaute45e6992011-11-20 08:59:36 +010048static int logp2syslog_level(unsigned int level)
Harald Welte46cfd772011-02-17 15:56:56 +010049{
50 if (level >= LOGL_FATAL)
51 return LOG_CRIT;
52 else if (level >= LOGL_ERROR)
53 return LOG_ERR;
54 else if (level >= LOGL_NOTICE)
55 return LOG_NOTICE;
56 else if (level >= LOGL_INFO)
57 return LOG_INFO;
58 else
59 return LOG_DEBUG;
60}
61
62static void _syslog_output(struct log_target *target,
63 unsigned int level, const char *log)
64{
65 syslog(logp2syslog_level(level), "%s", log);
66}
67
Harald Welte18fc4652011-08-17 14:14:17 +020068/*! \brief Create a new logging target for syslog logging
69 * \param[in] ident syslog string identifier
70 * \param[in] option syslog options
71 * \param[in] facility syslog facility
72 * \returns Log target in case of success, NULL in case of error
73 */
Harald Welte46cfd772011-02-17 15:56:56 +010074struct log_target *log_target_create_syslog(const char *ident, int option,
75 int facility)
76{
77 struct log_target *target;
78
79 target = log_target_create();
80 if (!target)
81 return NULL;
82
Harald Welte28222962011-02-18 20:37:04 +010083 target->tgt_syslog.facility = facility;
84 target->type = LOG_TGT_TYPE_SYSLOG;
Harald Welte46cfd772011-02-17 15:56:56 +010085 target->output = _syslog_output;
86
87 openlog(ident, option, facility);
88
89 return target;
90}
Harald Welte28222962011-02-18 20:37:04 +010091
92#endif /* HAVE_SYSLOG_H */
Harald Welte18fc4652011-08-17 14:14:17 +020093
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020094/* @} */