Merge branch 'master' into collectd-4
[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 static int log_level = LOG_DEBUG;
32
33 static const char *config_keys[] =
34 {
35         "LogLevel"
36 };
37 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
38
39 static int sl_config (const char *key, const char *value)
40 {
41         if (strcasecmp (key, "LogLevel") == 0)
42         {
43                 if ((strcasecmp (value, "emerg") == 0)
44                                 || (strcasecmp (value, "alert") == 0)
45                                 || (strcasecmp (value, "crit") == 0)
46                                 || (strcasecmp (value, "err") == 0))
47                         log_level = LOG_ERR;
48                 else if (strcasecmp (value, "warning") == 0)
49                         log_level = LOG_WARNING;
50                 else if (strcasecmp (value, "notice") == 0)
51                         log_level = LOG_NOTICE;
52                 else if (strcasecmp (value, "info") == 0)
53                         log_level = LOG_INFO;
54 #if COLLECT_DEBUG
55                 else if (strcasecmp (value, "debug") == 0)
56                         log_level = LOG_DEBUG;
57 #endif
58                 else
59                         return (1);
60         }
61         else
62                 return (-1);
63
64         return (0);
65 } /* int sl_config */
66
67 static int sl_init (void)
68 {
69         openlog ("collectd", LOG_CONS | LOG_PID, LOG_DAEMON);
70
71         return (0);
72 }
73
74 static void sl_log (int severity, const char *msg)
75 {
76         if (severity > log_level)
77                 return;
78
79         syslog (severity, "%s", msg);
80 } /* void sl_log */
81
82 static int sl_shutdown (void)
83 {
84         closelog ();
85
86         return (0);
87 }
88
89 void module_register (modreg_e load)
90 {
91         plugin_register_config ("syslog", sl_config, config_keys, config_keys_num);
92         plugin_register_init ("syslog", sl_init);
93         plugin_register_log ("syslog", sl_log);
94         plugin_register_shutdown ("syslog", sl_shutdown);
95 } /* void module_register(void) */