blob: 4f043b1dc13795b74612f6fa65a1c37ccab6b759 [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
22#include "../config.h"
23
Harald Welte28222962011-02-18 20:37:04 +010024#ifdef HAVE_SYSLOG_H
25
Harald Welte46cfd772011-02-17 15:56:56 +010026#include <stdarg.h>
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30#include <syslog.h>
31
32#ifdef HAVE_STRINGS_H
33#include <strings.h>
34#endif
35
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010036#include <osmocom/core/talloc.h>
37#include <osmocom/core/utils.h>
38#include <osmocom/core/logging.h>
Harald Welte46cfd772011-02-17 15:56:56 +010039
40static const int logp2syslog_level(unsigned int level)
41{
42 if (level >= LOGL_FATAL)
43 return LOG_CRIT;
44 else if (level >= LOGL_ERROR)
45 return LOG_ERR;
46 else if (level >= LOGL_NOTICE)
47 return LOG_NOTICE;
48 else if (level >= LOGL_INFO)
49 return LOG_INFO;
50 else
51 return LOG_DEBUG;
52}
53
54static void _syslog_output(struct log_target *target,
55 unsigned int level, const char *log)
56{
57 syslog(logp2syslog_level(level), "%s", log);
58}
59
60struct log_target *log_target_create_syslog(const char *ident, int option,
61 int facility)
62{
63 struct log_target *target;
64
65 target = log_target_create();
66 if (!target)
67 return NULL;
68
Harald Welte28222962011-02-18 20:37:04 +010069 target->tgt_syslog.facility = facility;
70 target->type = LOG_TGT_TYPE_SYSLOG;
Harald Welte46cfd772011-02-17 15:56:56 +010071 target->output = _syslog_output;
72
73 openlog(ident, option, facility);
74
75 return target;
76}
Harald Welte28222962011-02-18 20:37:04 +010077
78#endif /* HAVE_SYSLOG_H */