1 /*
2 * SysDB - src/plugins/syslog.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "sysdb.h"
29 #include "core/plugin.h"
30 #include "utils/error.h"
32 #include <assert.h>
33 #include <syslog.h>
35 SDB_PLUGIN_MAGIC;
37 #if (SDB_LOG_EMERG != LOG_EMERG) \
38 || (SDB_LOG_ERR != LOG_ERR) \
39 || (SDB_LOG_WARNING != LOG_WARNING) \
40 || (SDB_LOG_NOTICE != LOG_NOTICE) \
41 || (SDB_LOG_INFO != LOG_INFO) \
42 || (SDB_LOG_DEBUG != LOG_DEBUG)
43 # define SDB_LOG_PRIO_TO_SYSLOG(prio) \
44 (((prio) == SDB_LOG_EMERG) ? LOG_EMERG \
45 : ((prio) == SDB_LOG_ERR) ? LOG_ERR \
46 : ((prio) == SDB_LOG_WARNING) ? LOG_WARNING \
47 : ((prio) == SDB_LOG_NOTICE) ? LOG_NOTICE \
48 : ((prio) == SDB_LOG_INFO) ? LOG_INFO \
49 : ((prio) == SDB_LOG_DEBUG) ? LOG_DEBUG : LOG_ERR)
50 #else
51 # define SDB_LOG_PRIO_TO_SYSLOG(prio) (prio)
52 #endif
54 /*
55 * plugin API
56 */
58 static int
59 sdb_syslog_log(int prio, const char *msg,
60 sdb_object_t __attribute__((unused)) *user_data)
61 {
62 /* XXX: make the log-level configurable */
63 if (prio >= SDB_LOG_DEBUG)
64 return 0;
65 syslog(SDB_LOG_PRIO_TO_SYSLOG(prio), "%s", msg);
66 return 0;
67 } /* sdb_syslog_log */
69 static int
70 sdb_syslog_shutdown(sdb_object_t __attribute__((unused)) *user_data)
71 {
72 closelog();
73 return 0;
74 } /* sdb_syslog_shutdown */
76 int
77 sdb_module_init(sdb_plugin_info_t *info)
78 {
79 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
80 "log messages to the system logger");
81 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
82 "Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>");
83 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
84 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
85 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
87 if (info)
88 openlog("sysdbd", LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_DAEMON);
90 sdb_plugin_register_log("main", sdb_syslog_log, NULL);
91 sdb_plugin_register_shutdown("main", sdb_syslog_shutdown, NULL);
92 return 0;
93 } /* sdb_module_init */
95 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */