30e50d98fb3013f9440ad489af2317dc1cebb1c8
[collectd.git] / src / stderr.c
1 /**
2  * collectd - src/stderr.c
3  * Copyright (C) 2007  Sebastian Harl
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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Sebastian Harl <sh at tokkee.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #if COLLECT_DEBUG
27 static int log_level = LOG_DEBUG;
28 #else
29 static int log_level = LOG_INFO;
30 #endif /* COLLECT_DEBUG */
31
32 static const char *config_keys[] =
33 {
34         "LogLevel"
35 };
36 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
37
38 static int stderr_config (const char *key, const char *value)
39 {
40         if (0 == strcasecmp (key, "LogLevel")) {
41                 if ((0 == strcasecmp (value, "emerg"))
42                                 || (0 == strcasecmp (value, "alert"))
43                                 || (0 == strcasecmp (value, "crit"))
44                                 || (0 == strcasecmp (value, "err")))
45                         log_level = LOG_ERR;
46                 else if (0 == strcasecmp (value, "warning"))
47                         log_level = LOG_WARNING;
48                 else if (0 == strcasecmp (value, "notice"))
49                         log_level = LOG_NOTICE;
50                 else if (0 == strcasecmp (value, "info"))
51                         log_level = LOG_INFO;
52 #if COLLECT_DEBUG
53                 else if (0 == strcasecmp (value, "debug"))
54                         log_level = LOG_DEBUG;
55 #endif /* COLLECT_DEBUG */
56                 else
57                         return 1;
58         }
59         else {
60                 return -1;
61         }
62         return 0;
63 } /* int stderr_config (const char *, const char *) */
64
65 static void stderr_log (int severity, const char *msg)
66 {
67         if (severity > log_level)
68                 return;
69
70         fprintf (stderr, "%s\n", msg);
71         return;
72 } /* void stderr_log (int, const char *) */
73
74 void module_register (void)
75 {
76         plugin_register_config ("stderr", stderr_config,
77                         config_keys, config_keys_num);
78         plugin_register_log ("stderr", stderr_log);
79         return;
80 } /* void module_register (void) */
81
82 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
83