logfile plugin: Added Florian to the list of copyright holders.
[collectd.git] / src / logfile.c
1 /**
2  * collectd - src/logfile.c
3  * Copyright (C) 2007  Sebastian Harl
4  * Copyright (C) 2007  Florian Forster
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
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  *   Sebastian Harl <sh at tokkee.org>
21  *   Florian Forster <octo at verplant.org>
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #include <pthread.h>
29
30 #if COLLECT_DEBUG
31 static int log_level = LOG_DEBUG;
32 #else
33 static int log_level = LOG_INFO;
34 #endif /* COLLECT_DEBUG */
35
36 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
37
38 static char *log_file = NULL;
39
40 static const char *config_keys[] =
41 {
42         "LogLevel",
43         "File"
44 };
45 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
46
47 static int logfile_config (const char *key, const char *value)
48 {
49         if (0 == strcasecmp (key, "LogLevel")) {
50                 if ((0 == strcasecmp (value, "emerg"))
51                                 || (0 == strcasecmp (value, "alert"))
52                                 || (0 == strcasecmp (value, "crit"))
53                                 || (0 == strcasecmp (value, "err")))
54                         log_level = LOG_ERR;
55                 else if (0 == strcasecmp (value, "warning"))
56                         log_level = LOG_WARNING;
57                 else if (0 == strcasecmp (value, "notice"))
58                         log_level = LOG_NOTICE;
59                 else if (0 == strcasecmp (value, "info"))
60                         log_level = LOG_INFO;
61 #if COLLECT_DEBUG
62                 else if (0 == strcasecmp (value, "debug"))
63                         log_level = LOG_DEBUG;
64 #endif /* COLLECT_DEBUG */
65                 else
66                         return 1;
67         }
68         else if (0 == strcasecmp (key, "File")) {
69                 sfree (log_file);
70                 log_file = strdup (value);
71         }
72         else {
73                 return -1;
74         }
75         return 0;
76 } /* int logfile_config (const char *, const char *) */
77
78 static void logfile_log (int severity, const char *msg)
79 {
80         FILE *fh;
81         int do_close = 0;
82
83         if (severity > log_level)
84                 return;
85
86         pthread_mutex_lock (&file_lock);
87
88         if ((log_file == NULL) || (strcasecmp (log_file, "stderr") == 0))
89                 fh = stderr;
90         else if (strcasecmp (log_file, "stdout") == 0)
91                 fh = stdout;
92         else
93         {
94                 fh = fopen (log_file, "a");
95                 do_close = 1;
96         }
97
98         if (fh == NULL)
99         {
100                         char errbuf[1024];
101                         fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
102                                         (log_file == NULL) ? "<null>" : log_file,
103                                         sstrerror (errno, errbuf, sizeof (errbuf)));
104         }
105         else
106         {
107                 fprintf (fh, "%s\n", msg);
108                 if (do_close != 0)
109                         fclose (fh);
110         }
111
112         pthread_mutex_unlock (&file_lock);
113
114         return;
115 } /* void logfile_log (int, const char *) */
116
117 void module_register (void)
118 {
119         plugin_register_config ("logfile", logfile_config,
120                         config_keys, config_keys_num);
121         plugin_register_log ("logfile", logfile_log);
122 } /* void module_register (void) */
123
124 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
125