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_init(sdb_object_t __attribute__((unused)) *user_data)
60 {
61 openlog("sysdbd", LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_DAEMON);
62 return 0;
63 } /* sdb_syslog_init */
65 static int
66 sdb_syslog_log(int prio, const char *msg,
67 sdb_object_t __attribute__((unused)) *user_data)
68 {
69 syslog(SDB_LOG_PRIO_TO_SYSLOG(prio), "%s", msg);
70 return 0;
71 } /* sdb_syslog_log */
73 static int
74 sdb_syslog_shutdown(sdb_object_t __attribute__((unused)) *user_data)
75 {
76 closelog();
77 return 0;
78 } /* sdb_syslog_shutdown */
80 int
81 sdb_module_init(sdb_plugin_info_t *info)
82 {
83 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "syslog");
84 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
85 "log messages to the system logger");
86 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
87 "Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>");
88 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
89 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
90 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
92 sdb_plugin_register_init("syslog", sdb_syslog_init, NULL);
93 sdb_plugin_register_log("syslog", sdb_syslog_log, NULL);
94 sdb_plugin_register_shutdown("syslog", sdb_syslog_shutdown, NULL);
95 return 0;
96 } /* sdb_module_init */
98 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */