d251284ba1ed89b22d976002ada5e635c4e09ee3
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);
70 if (access (value, W_OK) == 0)
71 log_file = strdup (value);
72 else {
73 char errbuf[1024];
74 /* We can't use `ERROR' yet.. */
75 fprintf (stderr, "logfile plugin: Access to %s denied: %s\n",
76 value, sstrerror (errno, errbuf, sizeof (errbuf)));
77 return 1;
78 }
79 }
80 else {
81 return -1;
82 }
83 return 0;
84 } /* int logfile_config (const char *, const char *) */
86 static void logfile_log (int severity, const char *msg)
87 {
88 FILE *fh;
89 int do_close = 0;
91 if (severity > log_level)
92 return;
94 pthread_mutex_lock (&file_lock);
96 if ((log_file == NULL) || (strcasecmp (log_file, "stderr") == 0))
97 fh = stderr;
98 else if (strcasecmp (log_file, "stdout") == 0)
99 fh = stdout;
100 else
101 {
102 fh = fopen (log_file, "a");
103 do_close = 1;
104 }
106 if (fh == NULL)
107 {
108 char errbuf[1024];
109 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
110 (log_file == NULL) ? "<null>" : log_file,
111 sstrerror (errno, errbuf, sizeof (errbuf)));
112 }
113 else
114 {
115 fprintf (fh, "%s\n", msg);
116 if (do_close != 0)
117 fclose (fh);
118 }
120 pthread_mutex_unlock (&file_lock);
122 return;
123 } /* void logfile_log (int, const char *) */
125 void module_register (void)
126 {
127 plugin_register_config ("logfile", logfile_config,
128 config_keys, config_keys_num);
129 plugin_register_log ("logfile", logfile_log);
130 return;
131 } /* void module_register (void) */
133 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */