Bumped version to 5.1.2; Updated ChangeLog.
[collectd.git] / src / logfile.c
1 /**
2  * collectd - src/logfile.c
3  * Copyright (C) 2007  Sebastian Harl
4  * Copyright (C) 2007,2008  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 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.log"
31
32 #if COLLECT_DEBUG
33 static int log_level = LOG_DEBUG;
34 #else
35 static int log_level = LOG_INFO;
36 #endif /* COLLECT_DEBUG */
37
38 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
39
40 static char *log_file = NULL;
41 static int print_timestamp = 1;
42 static int print_severity = 0;
43
44 static const char *config_keys[] =
45 {
46         "LogLevel",
47         "File",
48         "Timestamp",
49         "PrintSeverity"
50 };
51 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
52
53 static int logfile_config (const char *key, const char *value)
54 {
55         if (0 == strcasecmp (key, "LogLevel")) {
56                 log_level = parse_log_severity(value);
57                 if (log_level == -1) return 1; /* to keep previous behaviour */
58         }
59         else if (0 == strcasecmp (key, "File")) {
60                 sfree (log_file);
61                 log_file = strdup (value);
62         }
63         else if (0 == strcasecmp (key, "Timestamp")) {
64                 if (IS_FALSE (value))
65                         print_timestamp = 0;
66                 else
67                         print_timestamp = 1;
68         } else if (0 == strcasecmp(key, "PrintSeverity")) {
69                 if (IS_FALSE (value))
70                         print_severity = 0;
71                 else
72                         print_severity = 1;
73         }
74         else {
75                 return -1;
76         }
77         return 0;
78 } /* int logfile_config (const char *, const char *) */
79
80 static void logfile_print (const char *msg, int severity,
81                 cdtime_t timestamp_time)
82 {
83         FILE *fh;
84         int do_close = 0;
85         struct tm timestamp_tm;
86         char timestamp_str[64];
87         char level_str[16] = "";
88
89         if (print_severity)
90         {
91                 switch (severity)
92                 {
93                 case LOG_ERR:
94                         snprintf(level_str, sizeof (level_str), "[error] ");
95                         break;  
96                 case LOG_WARNING:
97                         snprintf(level_str, sizeof (level_str), "[warning] ");
98                         break;
99                 case LOG_NOTICE:
100                         snprintf(level_str, sizeof (level_str), "[notice] ");
101                         break;  
102                 case LOG_INFO:
103                         snprintf(level_str, sizeof (level_str), "[info] ");
104                         break;  
105                 case LOG_DEBUG:
106                         snprintf(level_str, sizeof (level_str), "[debug] ");
107                         break;  
108                 default:
109                         break;
110                 }
111         }
112
113         if (print_timestamp)
114         {
115                 time_t tt = CDTIME_T_TO_TIME_T (timestamp_time);
116                 localtime_r (&tt, &timestamp_tm);
117
118                 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
119                                 &timestamp_tm);
120                 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
121         }
122
123         pthread_mutex_lock (&file_lock);
124
125         if (log_file == NULL)
126         {
127                 fh = fopen (DEFAULT_LOGFILE, "a");
128                 do_close = 1;
129         }
130         else if (strcasecmp (log_file, "stderr") == 0)
131                 fh = stderr;
132         else if (strcasecmp (log_file, "stdout") == 0)
133                 fh = stdout;
134         else
135         {
136                 fh = fopen (log_file, "a");
137                 do_close = 1;
138         }
139
140         if (fh == NULL)
141         {
142                         char errbuf[1024];
143                         fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
144                                         (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
145                                         sstrerror (errno, errbuf, sizeof (errbuf)));
146         }
147         else
148         {
149                 if (print_timestamp)
150                         fprintf (fh, "[%s] %s%s\n", timestamp_str, level_str, msg);
151                 else
152                         fprintf (fh, "%s%s\n", level_str, msg);
153
154                 if (do_close != 0)
155                         fclose (fh);
156         }
157
158         pthread_mutex_unlock (&file_lock);
159
160         return;
161 } /* void logfile_print */
162
163 static void logfile_log (int severity, const char *msg,
164                 user_data_t __attribute__((unused)) *user_data)
165 {
166         if (severity > log_level)
167                 return;
168
169         logfile_print (msg, severity, cdtime ());
170 } /* void logfile_log (int, const char *) */
171
172 static int logfile_notification (const notification_t *n,
173                 user_data_t __attribute__((unused)) *user_data)
174 {
175         char  buf[1024] = "";
176         char *buf_ptr = buf;
177         int   buf_len = sizeof (buf);
178         int status;
179
180         status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s",
181                         (n->severity == NOTIF_FAILURE) ? "FAILURE"
182                         : ((n->severity == NOTIF_WARNING) ? "WARNING"
183                                 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
184         if (status > 0)
185         {
186                 buf_ptr += status;
187                 buf_len -= status;
188         }
189
190 #define APPEND(bufptr, buflen, key, value) \
191         if ((buflen > 0) && (strlen (value) > 0)) { \
192                 int status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \
193                 if (status > 0) { \
194                         bufptr += status; \
195                         buflen -= status; \
196                 } \
197         }
198         APPEND (buf_ptr, buf_len, "host", n->host);
199         APPEND (buf_ptr, buf_len, "plugin", n->plugin);
200         APPEND (buf_ptr, buf_len, "plugin_instance", n->plugin_instance);
201         APPEND (buf_ptr, buf_len, "type", n->type);
202         APPEND (buf_ptr, buf_len, "type_instance", n->type_instance);
203         APPEND (buf_ptr, buf_len, "message", n->message);
204
205         buf[sizeof (buf) - 1] = '\0';
206
207         logfile_print (buf, LOG_INFO,
208                         (n->time != 0) ? n->time : cdtime ());
209
210         return (0);
211 } /* int logfile_notification */
212
213 void module_register (void)
214 {
215         plugin_register_config ("logfile", logfile_config,
216                         config_keys, config_keys_num);
217         plugin_register_log ("logfile", logfile_log, /* user_data = */ NULL);
218         plugin_register_notification ("logfile", logfile_notification,
219                         /* user_data = */ NULL);
220 } /* void module_register (void) */
221
222 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
223