1 /**
2 * collectd - src/logfile.c
3 * Copyright (C) 2007 Sebastian Harl
4 * Copyright (C) 2007 Florian Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Sebastian Harl <sh at tokkee.org>
21 * Florian Forster <octo at verplant.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 #include <pthread.h>
30 #if COLLECT_DEBUG
31 static int log_level = LOG_DEBUG;
32 #else
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;
39 static int print_timestamp = 1;
41 static const char *config_keys[] =
42 {
43 "LogLevel",
44 "File",
45 "Timestamp"
46 };
47 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
49 static int logfile_config (const char *key, const char *value)
50 {
51 if (0 == strcasecmp (key, "LogLevel")) {
52 if ((0 == strcasecmp (value, "emerg"))
53 || (0 == strcasecmp (value, "alert"))
54 || (0 == strcasecmp (value, "crit"))
55 || (0 == strcasecmp (value, "err")))
56 log_level = LOG_ERR;
57 else if (0 == strcasecmp (value, "warning"))
58 log_level = LOG_WARNING;
59 else if (0 == strcasecmp (value, "notice"))
60 log_level = LOG_NOTICE;
61 else if (0 == strcasecmp (value, "info"))
62 log_level = LOG_INFO;
63 #if COLLECT_DEBUG
64 else if (0 == strcasecmp (value, "debug"))
65 log_level = LOG_DEBUG;
66 #endif /* COLLECT_DEBUG */
67 else
68 return 1;
69 }
70 else if (0 == strcasecmp (key, "File")) {
71 sfree (log_file);
72 log_file = strdup (value);
73 }
74 else if (0 == strcasecmp (key, "Timestamp")) {
75 if ((strcasecmp (value, "false") == 0)
76 || (strcasecmp (value, "no") == 0)
77 || (strcasecmp (value, "off") == 0))
78 print_timestamp = 0;
79 else
80 print_timestamp = 1;
81 }
82 else {
83 return -1;
84 }
85 return 0;
86 } /* int logfile_config (const char *, const char *) */
88 static void logfile_log (int severity, const char *msg)
89 {
90 FILE *fh;
91 int do_close = 0;
92 time_t timestamp_time;
93 struct tm timestamp_tm;
94 char timestamp_str[64];
96 if (severity > log_level)
97 return;
99 if (print_timestamp)
100 {
101 timestamp_time = time (NULL);
102 localtime_r (×tamp_time, ×tamp_tm);
104 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
105 ×tamp_tm);
106 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
107 }
109 pthread_mutex_lock (&file_lock);
111 if ((log_file == NULL) || (strcasecmp (log_file, "stderr") == 0))
112 fh = stderr;
113 else if (strcasecmp (log_file, "stdout") == 0)
114 fh = stdout;
115 else
116 {
117 fh = fopen (log_file, "a");
118 do_close = 1;
119 }
121 if (fh == NULL)
122 {
123 char errbuf[1024];
124 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
125 (log_file == NULL) ? "<null>" : log_file,
126 sstrerror (errno, errbuf, sizeof (errbuf)));
127 }
128 else
129 {
130 if (print_timestamp)
131 fprintf (fh, "[%s] %s\n", timestamp_str, msg);
132 else
133 fprintf (fh, "%s\n", msg);
135 if (do_close != 0)
136 fclose (fh);
137 }
139 pthread_mutex_unlock (&file_lock);
141 return;
142 } /* void logfile_log (int, const char *) */
144 void module_register (void)
145 {
146 plugin_register_config ("logfile", logfile_config,
147 config_keys, config_keys_num);
148 plugin_register_log ("logfile", logfile_log);
149 } /* void module_register (void) */
151 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */