summaryrefslogtreecommitdiffstats
path: root/src/logging_syslog.c
blob: 6fe3c719ce4733583c173f4a68165495417dd49a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Syslog logging support code */

/* (C) 2011 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

/*! \addtogroup logging
 *  @{
 */

/*! \file logging_syslog.c
 *  \brief libosmocore logging output via syslog
 */

#include "../config.h"

#ifdef HAVE_SYSLOG_H

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>

#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif

#include <osmocom/core/talloc.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/logging.h>

static int logp2syslog_level(unsigned int level)
{
	if (level >= LOGL_FATAL)
		return LOG_CRIT;
	else if (level >= LOGL_ERROR)
		return LOG_ERR;
	else if (level >= LOGL_NOTICE)
		return LOG_NOTICE;
	else if (level >= LOGL_INFO)
		return LOG_INFO;
	else
		return LOG_DEBUG;
}

static void _syslog_output(struct log_target *target,
			   unsigned int level, const char *log)
{
	syslog(logp2syslog_level(level), "%s", log);
}

/*! \brief Create a new logging target for syslog logging
 *  \param[in] ident syslog string identifier
 *  \param[in] option syslog options
 *  \param[in] facility syslog facility
 *  \returns Log target in case of success, NULL in case of error
 */
struct log_target *log_target_create_syslog(const char *ident, int option,
					    int facility)
{
	struct log_target *target;

	target = log_target_create();
	if (!target)
		return NULL;

	target->tgt_syslog.facility = facility;
	target->type = LOG_TGT_TYPE_SYSLOG;
	target->output = _syslog_output;

	openlog(ident, option, facility);

	return target;
}

#endif /* HAVE_SYSLOG_H */

/* @} */