1 /**
2 * collectd - src/logfile.c
3 * Copyright (C) 2007 Sebastian Harl
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Sebastian Harl <sh at tokkee.org>
20 * Florian Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #include <pthread.h>
29 #if COLLECT_DEBUG
30 static int log_level = LOG_DEBUG;
31 #else
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[] =
40 {
41 "LogLevel",
42 "File"
43 };
44 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
46 static int logfile_config (const char *key, const char *value)
47 {
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")))
53 log_level = LOG_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"))
59 log_level = LOG_INFO;
60 #if COLLECT_DEBUG
61 else if (0 == strcasecmp (value, "debug"))
62 log_level = LOG_DEBUG;
63 #endif /* COLLECT_DEBUG */
64 else
65 return 1;
66 }
67 else if (0 == strcasecmp (key, "File")) {
68 sfree (log_file);
69 log_file = strdup (value);
70 }
71 else {
72 return -1;
73 }
74 return 0;
75 } /* int logfile_config (const char *, const char *) */
77 static void logfile_log (int severity, const char *msg)
78 {
79 FILE *fh;
80 int do_close = 0;
82 if (severity > log_level)
83 return;
85 pthread_mutex_lock (&file_lock);
87 if ((log_file == NULL) || (strcasecmp (log_file, "stderr") == 0))
88 fh = stderr;
89 else if (strcasecmp (log_file, "stdout") == 0)
90 fh = stdout;
91 else
92 {
93 fh = fopen (log_file, "a");
94 do_close = 1;
95 }
97 if (fh == NULL)
98 {
99 char errbuf[1024];
100 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
101 (log_file == NULL) ? "<null>" : log_file,
102 sstrerror (errno, errbuf, sizeof (errbuf)));
103 }
104 else
105 {
106 fprintf (fh, "%s\n", msg);
107 if (do_close != 0)
108 fclose (fh);
109 }
111 pthread_mutex_unlock (&file_lock);
113 return;
114 } /* void logfile_log (int, const char *) */
116 void module_register (void)
117 {
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 : */