2 * collectd - src/logfile.c
3 * Copyright (C) 2007 Sebastian Harl
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.
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.
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
19 * Sebastian Harl <sh at tokkee.org>
20 * Florian Forster <octo at verplant.org>
29 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.log"
32 static int log_level = LOG_DEBUG;
34 static int log_level = LOG_INFO;
35 #endif /* COLLECT_DEBUG */
37 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
39 static char *log_file = NULL;
40 static int print_timestamp = 1;
42 static const char *config_keys[] =
48 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
50 static int logfile_config (const char *key, const char *value)
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")))
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"))
65 else if (0 == strcasecmp (value, "debug"))
66 log_level = LOG_DEBUG;
67 #endif /* COLLECT_DEBUG */
71 else if (0 == strcasecmp (key, "File")) {
73 log_file = strdup (value);
75 else if (0 == strcasecmp (key, "File")) {
76 if ((strcasecmp (value, "false") == 0)
77 || (strcasecmp (value, "no") == 0)
78 || (strcasecmp (value, "off") == 0))
87 } /* int logfile_config (const char *, const char *) */
89 static void logfile_log (int severity, const char *msg)
93 time_t timestamp_time;
94 struct tm timestamp_tm;
95 char timestamp_str[64];
97 if (severity > log_level)
102 timestamp_time = time (NULL);
103 localtime_r (×tamp_time, ×tamp_tm);
105 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
107 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
110 pthread_mutex_lock (&file_lock);
112 if (log_file == NULL)
114 fh = fopen (DEFAULT_LOGFILE, "a");
117 else if (strcasecmp (log_file, "stderr") == 0)
119 else if (strcasecmp (log_file, "stdout") == 0)
123 fh = fopen (log_file, "a");
130 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
131 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
132 sstrerror (errno, errbuf, sizeof (errbuf)));
137 fprintf (fh, "[%s] %s\n", timestamp_str, msg);
139 fprintf (fh, "%s\n", msg);
145 pthread_mutex_unlock (&file_lock);
148 } /* void logfile_log (int, const char *) */
150 void module_register (void)
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) */
157 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */