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>
30 static int log_level = LOG_DEBUG;
32 static int log_level = LOG_INFO;
33 #endif /* COLLECT_DEBUG */
35 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
37 static char *log_file = NULL;
38 static int print_timestamp = 1;
40 static const char *config_keys[] =
46 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
48 static int logfile_config (const char *key, const char *value)
50 if (0 == strcasecmp (key, "LogLevel")) {
51 if ((0 == strcasecmp (value, "emerg"))
52 || (0 == strcasecmp (value, "alert"))
53 || (0 == strcasecmp (value, "crit"))
54 || (0 == strcasecmp (value, "err")))
56 else if (0 == strcasecmp (value, "warning"))
57 log_level = LOG_WARNING;
58 else if (0 == strcasecmp (value, "notice"))
59 log_level = LOG_NOTICE;
60 else if (0 == strcasecmp (value, "info"))
63 else if (0 == strcasecmp (value, "debug"))
64 log_level = LOG_DEBUG;
65 #endif /* COLLECT_DEBUG */
69 else if (0 == strcasecmp (key, "File")) {
71 log_file = strdup (value);
73 else if (0 == strcasecmp (key, "File")) {
74 if ((strcasecmp (value, "false") == 0)
75 || (strcasecmp (value, "no") == 0)
76 || (strcasecmp (value, "off") == 0))
85 } /* int logfile_config (const char *, const char *) */
87 static void logfile_log (int severity, const char *msg)
91 time_t timestamp_time;
92 struct tm timestamp_tm;
93 char timestamp_str[64];
95 if (severity > log_level)
100 timestamp_time = time (NULL);
101 localtime_r (×tamp_time, ×tamp_tm);
103 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
105 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
108 pthread_mutex_lock (&file_lock);
110 if ((log_file == NULL) || (strcasecmp (log_file, "stderr") == 0))
112 else if (strcasecmp (log_file, "stdout") == 0)
116 fh = fopen (log_file, "a");
123 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
124 (log_file == NULL) ? "<null>" : log_file,
125 sstrerror (errno, errbuf, sizeof (errbuf)));
130 fprintf (fh, "[%s] %s\n", timestamp_str, msg);
132 fprintf (fh, "%s\n", msg);
138 pthread_mutex_unlock (&file_lock);
141 } /* void logfile_log (int, const char *) */
143 void module_register (void)
145 plugin_register_config ("logfile", logfile_config,
146 config_keys, config_keys_num);
147 plugin_register_log ("logfile", logfile_log);
148 } /* void module_register (void) */
150 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */