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;
39 static const char *config_keys[] =
44 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
46 static int logfile_config (const char *key, const char *value)
48 if (0 == strcasecmp (key, "LogLevel")) {
49 if ((0 == strcasecmp (value, "emerg"))
50 || (0 == strcasecmp (value, "alert"))
51 || (0 == strcasecmp (value, "crit"))
52 || (0 == strcasecmp (value, "err")))
54 else if (0 == strcasecmp (value, "warning"))
55 log_level = LOG_WARNING;
56 else if (0 == strcasecmp (value, "notice"))
57 log_level = LOG_NOTICE;
58 else if (0 == strcasecmp (value, "info"))
61 else if (0 == strcasecmp (value, "debug"))
62 log_level = LOG_DEBUG;
63 #endif /* COLLECT_DEBUG */
67 else if (0 == strcasecmp (key, "File")) {
69 log_file = strdup (value);
75 } /* int logfile_config (const char *, const char *) */
77 static void logfile_log (int severity, const char *msg)
82 if (severity > log_level)
85 pthread_mutex_lock (&file_lock);
87 if ((log_file == NULL) || (strcasecmp (log_file, "stderr") == 0))
89 else if (strcasecmp (log_file, "stdout") == 0)
93 fh = fopen (log_file, "a");
100 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
101 (log_file == NULL) ? "<null>" : log_file,
102 sstrerror (errno, errbuf, sizeof (errbuf)));
106 fprintf (fh, "%s\n", msg);
111 pthread_mutex_unlock (&file_lock);
114 } /* void logfile_log (int, const char *) */
116 void module_register (void)
118 plugin_register_config ("logfile", logfile_config,
119 config_keys, config_keys_num);
120 plugin_register_log ("logfile", logfile_log);
121 } /* void module_register (void) */
123 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */