2 * collectd - src/logfile.c
3 * Copyright (C) 2007 Sebastian Harl
4 * Copyright (C) 2007,2008 Florian Forster
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.
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.
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
20 * Sebastian Harl <sh at tokkee.org>
21 * Florian Forster <octo at verplant.org>
30 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.log"
33 static int log_level = LOG_DEBUG;
35 static int log_level = LOG_INFO;
36 #endif /* COLLECT_DEBUG */
38 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
40 static char *log_file = NULL;
41 static int print_timestamp = 1;
42 static int print_severity = 0;
44 static const char *config_keys[] =
51 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
53 static int logfile_config (const char *key, const char *value)
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")))
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"))
68 else if (0 == strcasecmp (value, "debug"))
69 log_level = LOG_DEBUG;
70 #endif /* COLLECT_DEBUG */
74 else if (0 == strcasecmp (key, "File")) {
76 log_file = strdup (value);
78 else if (0 == strcasecmp (key, "Timestamp")) {
83 } else if (0 == strcasecmp(key, "PrintSeverity")) {
93 } /* int logfile_config (const char *, const char *) */
95 static void logfile_print (const char *msg, int severity, time_t timestamp_time)
99 struct tm timestamp_tm;
100 char timestamp_str[64];
101 char level_str[16] = "";
108 snprintf(level_str, sizeof (level_str), "[error] ");
111 snprintf(level_str, sizeof (level_str), "[warning] ");
114 snprintf(level_str, sizeof (level_str), "[notice] ");
117 snprintf(level_str, sizeof (level_str), "[info] ");
120 snprintf(level_str, sizeof (level_str), "[debug] ");
129 localtime_r (×tamp_time, ×tamp_tm);
131 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
133 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
136 pthread_mutex_lock (&file_lock);
138 if (log_file == NULL)
140 fh = fopen (DEFAULT_LOGFILE, "a");
143 else if (strcasecmp (log_file, "stderr") == 0)
145 else if (strcasecmp (log_file, "stdout") == 0)
149 fh = fopen (log_file, "a");
156 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
157 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
158 sstrerror (errno, errbuf, sizeof (errbuf)));
163 fprintf (fh, "[%s] %s%s\n", timestamp_str, level_str, msg);
165 fprintf (fh, "%s%s\n", level_str, msg);
171 pthread_mutex_unlock (&file_lock);
174 } /* void logfile_print */
176 static void logfile_log (int severity, const char *msg,
177 user_data_t __attribute__((unused)) *user_data)
179 if (severity > log_level)
182 logfile_print (msg, severity, time (NULL));
183 } /* void logfile_log (int, const char *) */
185 static int logfile_notification (const notification_t *n,
186 user_data_t __attribute__((unused)) *user_data)
190 int buf_len = sizeof (buf);
193 status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s",
194 (n->severity == NOTIF_FAILURE) ? "FAILURE"
195 : ((n->severity == NOTIF_WARNING) ? "WARNING"
196 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
203 #define APPEND(bufptr, buflen, key, value) \
204 if ((buflen > 0) && (strlen (value) > 0)) { \
205 int status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \
211 APPEND (buf_ptr, buf_len, "host", n->host);
212 APPEND (buf_ptr, buf_len, "plugin", n->plugin);
213 APPEND (buf_ptr, buf_len, "plugin_instance", n->plugin_instance);
214 APPEND (buf_ptr, buf_len, "type", n->type);
215 APPEND (buf_ptr, buf_len, "type_instance", n->type_instance);
216 APPEND (buf_ptr, buf_len, "message", n->message);
218 buf[sizeof (buf) - 1] = '\0';
220 logfile_print (buf, LOG_INFO,
221 (n->time > 0) ? n->time : time (NULL));
224 } /* int logfile_notification */
226 void module_register (void)
228 plugin_register_config ("logfile", logfile_config,
229 config_keys, config_keys_num);
230 plugin_register_log ("logfile", logfile_log, /* user_data = */ NULL);
231 plugin_register_notification ("logfile", logfile_notification,
232 /* user_data = */ NULL);
233 } /* void module_register (void) */
235 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */