logfile plugin: Default to $localstatedir/log/collectd.log instead of STDOUT.
[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 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.log"
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 pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
38
39 static char *log_file = NULL;
40 static int print_timestamp = 1;
41
42 static const char *config_keys[] =
43 {
44         "LogLevel",
45         "File",
46         "Timestamp"
47 };
48 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
49
50 static int logfile_config (const char *key, const char *value)
51 {
52         if (0 == strcasecmp (key, "LogLevel")) {
53                 if ((0 == strcasecmp (value, "emerg"))
54                                 || (0 == strcasecmp (value, "alert"))
55                                 || (0 == strcasecmp (value, "crit"))
56                                 || (0 == strcasecmp (value, "err")))
57                         log_level = LOG_ERR;
58                 else if (0 == strcasecmp (value, "warning"))
59                         log_level = LOG_WARNING;
60                 else if (0 == strcasecmp (value, "notice"))
61                         log_level = LOG_NOTICE;
62                 else if (0 == strcasecmp (value, "info"))
63                         log_level = LOG_INFO;
64 #if COLLECT_DEBUG
65                 else if (0 == strcasecmp (value, "debug"))
66                         log_level = LOG_DEBUG;
67 #endif /* COLLECT_DEBUG */
68                 else
69                         return 1;
70         }
71         else if (0 == strcasecmp (key, "File")) {
72                 sfree (log_file);
73                 log_file = strdup (value);
74         }
75         else if (0 == strcasecmp (key, "File")) {
76                 if ((strcasecmp (value, "false") == 0)
77                                 || (strcasecmp (value, "no") == 0)
78                                 || (strcasecmp (value, "off") == 0))
79                         print_timestamp = 0;
80                 else
81                         print_timestamp = 1;
82         }
83         else {
84                 return -1;
85         }
86         return 0;
87 } /* int logfile_config (const char *, const char *) */
88
89 static void logfile_log (int severity, const char *msg)
90 {
91         FILE *fh;
92         int do_close = 0;
93         time_t timestamp_time;
94         struct tm timestamp_tm;
95         char timestamp_str[64];
96
97         if (severity > log_level)
98                 return;
99
100         if (print_timestamp)
101         {
102                 timestamp_time = time (NULL);
103                 localtime_r (&timestamp_time, &timestamp_tm);
104
105                 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
106                                 &timestamp_tm);
107                 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
108         }
109
110         pthread_mutex_lock (&file_lock);
111
112         if (log_file == NULL)
113         {
114                 fh = fopen (DEFAULT_LOGFILE, "a");
115                 do_close = 1;
116         }
117         else if (strcasecmp (log_file, "stderr") == 0)
118                 fh = stderr;
119         else if (strcasecmp (log_file, "stdout") == 0)
120                 fh = stdout;
121         else
122         {
123                 fh = fopen (log_file, "a");
124                 do_close = 1;
125         }
126
127         if (fh == NULL)
128         {
129                         char errbuf[1024];
130                         fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
131                                         (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
132                                         sstrerror (errno, errbuf, sizeof (errbuf)));
133         }
134         else
135         {
136                 if (print_timestamp)
137                         fprintf (fh, "[%s] %s\n", timestamp_str, msg);
138                 else
139                         fprintf (fh, "%s\n", msg);
140
141                 if (do_close != 0)
142                         fclose (fh);
143         }
144
145         pthread_mutex_unlock (&file_lock);
146
147         return;
148 } /* void logfile_log (int, const char *) */
149
150 void module_register (void)
151 {
152         plugin_register_config ("logfile", logfile_config,
153                         config_keys, config_keys_num);
154         plugin_register_log ("logfile", logfile_log);
155 } /* void module_register (void) */
156
157 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
158