2 * collectd - src/logfile.c
3 * Copyright (C) 2007 Sebastian Harl
4 * Copyright (C) 2007 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>
31 static int log_level = LOG_DEBUG;
33 static int log_level = LOG_INFO;
34 #endif /* COLLECT_DEBUG */
36 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
38 static char *log_file = NULL;
40 static const char *config_keys[] =
45 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
47 static int logfile_config (const char *key, const char *value)
49 if (0 == strcasecmp (key, "LogLevel")) {
50 if ((0 == strcasecmp (value, "emerg"))
51 || (0 == strcasecmp (value, "alert"))
52 || (0 == strcasecmp (value, "crit"))
53 || (0 == strcasecmp (value, "err")))
55 else if (0 == strcasecmp (value, "warning"))
56 log_level = LOG_WARNING;
57 else if (0 == strcasecmp (value, "notice"))
58 log_level = LOG_NOTICE;
59 else if (0 == strcasecmp (value, "info"))
62 else if (0 == strcasecmp (value, "debug"))
63 log_level = LOG_DEBUG;
64 #endif /* COLLECT_DEBUG */
68 else if (0 == strcasecmp (key, "File")) {
70 log_file = strdup (value);
76 } /* int logfile_config (const char *, const char *) */
78 static void logfile_log (int severity, const char *msg)
83 if (severity > log_level)
86 pthread_mutex_lock (&file_lock);
88 if ((log_file == NULL) || (strcasecmp (log_file, "stderr") == 0))
90 else if (strcasecmp (log_file, "stdout") == 0)
94 fh = fopen (log_file, "a");
101 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
102 (log_file == NULL) ? "<null>" : log_file,
103 sstrerror (errno, errbuf, sizeof (errbuf)));
107 fprintf (fh, "%s\n", msg);
112 pthread_mutex_unlock (&file_lock);
115 } /* void logfile_log (int, const char *) */
117 void module_register (void)
119 plugin_register_config ("logfile", logfile_config,
120 config_keys, config_keys_num);
121 plugin_register_log ("logfile", logfile_log);
122 } /* void module_register (void) */
124 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */