Merge branch 'collectd-4.6'
[collectd.git] / src / syslog.c
1 /**
2  * collectd - src/syslog.c
3  * Copyright (C) 2007  Florian Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Florian Forster <octo at verplant.org>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #if HAVE_SYSLOG_H
28 # include <syslog.h>
29 #endif
30
31 #if COLLECT_DEBUG
32 static int log_level = LOG_DEBUG;
33 #else
34 static int log_level = LOG_INFO;
35 #endif /* COLLECT_DEBUG */
36
37 static const char *config_keys[] =
38 {
39         "LogLevel"
40 };
41 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
42
43 static int sl_config (const char *key, const char *value)
44 {
45         if (strcasecmp (key, "LogLevel") == 0)
46         {
47                 if ((strcasecmp (value, "emerg") == 0)
48                                 || (strcasecmp (value, "alert") == 0)
49                                 || (strcasecmp (value, "crit") == 0)
50                                 || (strcasecmp (value, "err") == 0))
51                         log_level = LOG_ERR;
52                 else if (strcasecmp (value, "warning") == 0)
53                         log_level = LOG_WARNING;
54                 else if (strcasecmp (value, "notice") == 0)
55                         log_level = LOG_NOTICE;
56                 else if (strcasecmp (value, "info") == 0)
57                         log_level = LOG_INFO;
58 #if COLLECT_DEBUG
59                 else if (strcasecmp (value, "debug") == 0)
60                         log_level = LOG_DEBUG;
61 #endif
62                 else
63                         return (1);
64         }
65         else
66                 return (-1);
67
68         return (0);
69 } /* int sl_config */
70
71 static void sl_log (int severity, const char *msg,
72                 user_data_t __attribute__((unused)) *user_data)
73 {
74         if (severity > log_level)
75                 return;
76
77         syslog (severity, "%s", msg);
78 } /* void sl_log */
79
80 static int sl_shutdown (void)
81 {
82         closelog ();
83
84         return (0);
85 }
86
87 void module_register (void)
88 {
89         openlog ("collectd", LOG_CONS | LOG_PID, LOG_DAEMON);
90
91         plugin_register_config ("syslog", sl_config, config_keys, config_keys_num);
92         plugin_register_log ("syslog", sl_log, /* user_data = */ NULL);
93         plugin_register_shutdown ("syslog", sl_shutdown);
94 } /* void module_register(void) */