curl's numeric options are always at minimum long, never int
[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                 if ((0 == strcasecmp (value, "emerg"))
57                                 || (0 == strcasecmp (value, "alert"))
58                                 || (0 == strcasecmp (value, "crit"))
59                                 || (0 == strcasecmp (value, "err")))
60                         log_level = LOG_ERR;
61                 else if (0 == strcasecmp (value, "warning"))
62                         log_level = LOG_WARNING;
63                 else if (0 == strcasecmp (value, "notice"))
64                         log_level = LOG_NOTICE;
65                 else if (0 == strcasecmp (value, "info"))
66                         log_level = LOG_INFO;
67 #if COLLECT_DEBUG
68                 else if (0 == strcasecmp (value, "debug"))
69                         log_level = LOG_DEBUG;
70 #endif /* COLLECT_DEBUG */
71                 else
72                         return 1;
73         }
74         else if (0 == strcasecmp (key, "File")) {
75                 sfree (log_file);
76                 log_file = strdup (value);
77         }
78         else if (0 == strcasecmp (key, "Timestamp")) {
79                 if (IS_FALSE (value))
80                         print_timestamp = 0;
81                 else
82                         print_timestamp = 1;
83         } else if (0 == strcasecmp(key, "PrintSeverity")) {
84                 if (IS_FALSE (value))
85                         print_severity = 0;
86                 else
87                         print_severity = 1;
88         }
89         else {
90                 return -1;
91         }
92         return 0;
93 } /* int logfile_config (const char *, const char *) */
94
95 static void logfile_print (const char *msg, int severity, time_t timestamp_time)
96 {
97         FILE *fh;
98         _Bool do_close = 0;
99         struct tm timestamp_tm;
100         char timestamp_str[64];
101         char level_str[16] = "";
102
103         if (print_severity)
104         {
105                 switch (severity)
106                 {
107                 case LOG_ERR:
108                         snprintf(level_str, sizeof (level_str), "[error] ");
109                         break;  
110                 case LOG_WARNING:
111                         snprintf(level_str, sizeof (level_str), "[warning] ");
112                         break;
113                 case LOG_NOTICE:
114                         snprintf(level_str, sizeof (level_str), "[notice] ");
115                         break;  
116                 case LOG_INFO:
117                         snprintf(level_str, sizeof (level_str), "[info] ");
118                         break;  
119                 case LOG_DEBUG:
120                         snprintf(level_str, sizeof (level_str), "[debug] ");
121                         break;  
122                 default:
123                         break;
124                 }
125         }
126
127         if (print_timestamp)
128         {
129                 localtime_r (&timestamp_time, &timestamp_tm);
130
131                 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
132                                 &timestamp_tm);
133                 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
134         }
135
136         pthread_mutex_lock (&file_lock);
137
138         if (log_file == NULL)
139         {
140                 fh = fopen (DEFAULT_LOGFILE, "a");
141                 do_close = 1;
142         }
143         else if (strcasecmp (log_file, "stderr") == 0)
144                 fh = stderr;
145         else if (strcasecmp (log_file, "stdout") == 0)
146                 fh = stdout;
147         else
148         {
149                 fh = fopen (log_file, "a");
150                 do_close = 1;
151         }
152
153         if (fh == NULL)
154         {
155                         char errbuf[1024];
156                         fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
157                                         (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
158                                         sstrerror (errno, errbuf, sizeof (errbuf)));
159         }
160         else
161         {
162                 if (print_timestamp)
163                         fprintf (fh, "[%s] %s%s\n", timestamp_str, level_str, msg);
164                 else
165                         fprintf (fh, "%s%s\n", level_str, msg);
166
167                 if (do_close) {
168                         fclose (fh);
169                 } else {
170                         fflush(fh);
171                 }
172         }
173
174         pthread_mutex_unlock (&file_lock);
175
176         return;
177 } /* void logfile_print */
178
179 static void logfile_log (int severity, const char *msg,
180                 user_data_t __attribute__((unused)) *user_data)
181 {
182         if (severity > log_level)
183                 return;
184
185         logfile_print (msg, severity, time (NULL));
186 } /* void logfile_log (int, const char *) */
187
188 static int logfile_notification (const notification_t *n,
189                 user_data_t __attribute__((unused)) *user_data)
190 {
191         char  buf[1024] = "";
192         char *buf_ptr = buf;
193         int   buf_len = sizeof (buf);
194         int status;
195
196         status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s",
197                         (n->severity == NOTIF_FAILURE) ? "FAILURE"
198                         : ((n->severity == NOTIF_WARNING) ? "WARNING"
199                                 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
200         if (status > 0)
201         {
202                 buf_ptr += status;
203                 buf_len -= status;
204         }
205
206 #define APPEND(bufptr, buflen, key, value) \
207         if ((buflen > 0) && (strlen (value) > 0)) { \
208                 int status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \
209                 if (status > 0) { \
210                         bufptr += status; \
211                         buflen -= status; \
212                 } \
213         }
214         APPEND (buf_ptr, buf_len, "host", n->host);
215         APPEND (buf_ptr, buf_len, "plugin", n->plugin);
216         APPEND (buf_ptr, buf_len, "plugin_instance", n->plugin_instance);
217         APPEND (buf_ptr, buf_len, "type", n->type);
218         APPEND (buf_ptr, buf_len, "type_instance", n->type_instance);
219         APPEND (buf_ptr, buf_len, "message", n->message);
220
221         buf[sizeof (buf) - 1] = '\0';
222
223         logfile_print (buf, LOG_INFO,
224                         (n->time > 0) ? n->time : time (NULL));
225
226         return (0);
227 } /* int logfile_notification */
228
229 void module_register (void)
230 {
231         plugin_register_config ("logfile", logfile_config,
232                         config_keys, config_keys_num);
233         plugin_register_log ("logfile", logfile_log, /* user_data = */ NULL);
234         plugin_register_notification ("logfile", logfile_notification,
235                         /* user_data = */ NULL);
236 } /* void module_register (void) */
237
238 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
239