ceee7b80c10d8aeddb40f2a75d961c64a0927f9c
[collectd.git] / src / logfile.c
1 /**
2  * collectd - src/logfile.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  *   Florian Forster <octo at verplant.org>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #include <pthread.h>
28
29 #if COLLECT_DEBUG
30 static int log_level = LOG_DEBUG;
31 #else
32 static int log_level = LOG_INFO;
33 #endif /* COLLECT_DEBUG */
34
35 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
36
37 static char *log_file = NULL;
38
39 static const char *config_keys[] =
40 {
41         "LogLevel",
42         "File"
43 };
44 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
45
46 static int logfile_config (const char *key, const char *value)
47 {
48         if (0 == strcasecmp (key, "LogLevel")) {
49                 if ((0 == strcasecmp (value, "emerg"))
50                                 || (0 == strcasecmp (value, "alert"))
51                                 || (0 == strcasecmp (value, "crit"))
52                                 || (0 == strcasecmp (value, "err")))
53                         log_level = LOG_ERR;
54                 else if (0 == strcasecmp (value, "warning"))
55                         log_level = LOG_WARNING;
56                 else if (0 == strcasecmp (value, "notice"))
57                         log_level = LOG_NOTICE;
58                 else if (0 == strcasecmp (value, "info"))
59                         log_level = LOG_INFO;
60 #if COLLECT_DEBUG
61                 else if (0 == strcasecmp (value, "debug"))
62                         log_level = LOG_DEBUG;
63 #endif /* COLLECT_DEBUG */
64                 else
65                         return 1;
66         }
67         else if (0 == strcasecmp (key, "File")) {
68                 sfree (log_file);
69
70                 if ((strcasecmp (value, "stdout") == 0)
71                                 || (strcasecmp (value, "stderr") == 0)
72                                 || (access (value, W_OK) == 0))
73                         log_file = strdup (value);
74                 else {
75                         char errbuf[1024];
76                         /* We can't use `ERROR' yet.. */
77                         fprintf (stderr, "logfile plugin: Access to %s denied: %s\n",
78                                         value, sstrerror (errno, errbuf, sizeof (errbuf)));
79                         return 1;
80                 }
81         }
82         else {
83                 return -1;
84         }
85         return 0;
86 } /* int logfile_config (const char *, const char *) */
87
88 static void logfile_log (int severity, const char *msg)
89 {
90         FILE *fh;
91         int do_close = 0;
92
93         if (severity > log_level)
94                 return;
95
96         pthread_mutex_lock (&file_lock);
97
98         if ((log_file == NULL) || (strcasecmp (log_file, "stderr") == 0))
99                 fh = stderr;
100         else if (strcasecmp (log_file, "stdout") == 0)
101                 fh = stdout;
102         else
103         {
104                 fh = fopen (log_file, "a");
105                 do_close = 1;
106         }
107
108         if (fh == NULL)
109         {
110                         char errbuf[1024];
111                         fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
112                                         (log_file == NULL) ? "<null>" : log_file,
113                                         sstrerror (errno, errbuf, sizeof (errbuf)));
114         }
115         else
116         {
117                 fprintf (fh, "%s\n", msg);
118                 if (do_close != 0)
119                         fclose (fh);
120         }
121
122         pthread_mutex_unlock (&file_lock);
123
124         return;
125 } /* void logfile_log (int, const char *) */
126
127 void module_register (void)
128 {
129         plugin_register_config ("logfile", logfile_config,
130                         config_keys, config_keys_num);
131         plugin_register_log ("logfile", logfile_log);
132         return;
133 } /* void module_register (void) */
134
135 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
136