1 /**
2 * collectd - src/logfile.c
3 * Copyright (C) 2007 Sebastian Harl
4 * Copyright (C) 2007,2008 Florian Forster
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Sebastian Harl <sh at tokkee.org>
26 * Florian Forster <octo at collectd.org>
27 **/
29 #include "collectd.h"
31 #include "common.h"
32 #include "plugin.h"
34 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.log"
36 #if COLLECT_DEBUG
37 static int log_level = LOG_DEBUG;
38 #else
39 static int log_level = LOG_INFO;
40 #endif /* COLLECT_DEBUG */
42 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
44 static char *log_file = NULL;
45 static int print_timestamp = 1;
46 static int print_severity = 0;
48 static const char *config_keys[] =
49 {
50 "LogLevel",
51 "File",
52 "Timestamp",
53 "PrintSeverity"
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57 static int logfile_config (const char *key, const char *value)
58 {
59 if (0 == strcasecmp (key, "LogLevel")) {
60 log_level = parse_log_severity(value);
61 if (log_level < 0) {
62 log_level = LOG_INFO;
63 ERROR ("logfile: invalid loglevel [%s] defaulting to 'info'", value);
64 return (1);
65 }
66 }
67 else if (0 == strcasecmp (key, "File")) {
68 sfree (log_file);
69 log_file = strdup (value);
70 }
71 else if (0 == strcasecmp (key, "Timestamp")) {
72 if (IS_FALSE (value))
73 print_timestamp = 0;
74 else
75 print_timestamp = 1;
76 } else if (0 == strcasecmp(key, "PrintSeverity")) {
77 if (IS_FALSE (value))
78 print_severity = 0;
79 else
80 print_severity = 1;
81 }
82 else {
83 return -1;
84 }
85 return 0;
86 } /* int logfile_config (const char *, const char *) */
88 static void logfile_print (const char *msg, int severity,
89 cdtime_t timestamp_time)
90 {
91 FILE *fh;
92 _Bool do_close = 0;
93 struct tm timestamp_tm;
94 char timestamp_str[64];
95 char level_str[16] = "";
97 if (print_severity)
98 {
99 switch (severity)
100 {
101 case LOG_ERR:
102 snprintf(level_str, sizeof (level_str), "[error] ");
103 break;
104 case LOG_WARNING:
105 snprintf(level_str, sizeof (level_str), "[warning] ");
106 break;
107 case LOG_NOTICE:
108 snprintf(level_str, sizeof (level_str), "[notice] ");
109 break;
110 case LOG_INFO:
111 snprintf(level_str, sizeof (level_str), "[info] ");
112 break;
113 case LOG_DEBUG:
114 snprintf(level_str, sizeof (level_str), "[debug] ");
115 break;
116 default:
117 break;
118 }
119 }
121 if (print_timestamp)
122 {
123 time_t tt = CDTIME_T_TO_TIME_T (timestamp_time);
124 localtime_r (&tt, ×tamp_tm);
126 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
127 ×tamp_tm);
128 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
129 }
131 pthread_mutex_lock (&file_lock);
133 if (log_file == NULL)
134 {
135 fh = fopen (DEFAULT_LOGFILE, "a");
136 do_close = 1;
137 }
138 else if (strcasecmp (log_file, "stderr") == 0)
139 fh = stderr;
140 else if (strcasecmp (log_file, "stdout") == 0)
141 fh = stdout;
142 else
143 {
144 fh = fopen (log_file, "a");
145 do_close = 1;
146 }
148 if (fh == NULL)
149 {
150 char errbuf[1024];
151 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
152 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
153 sstrerror (errno, errbuf, sizeof (errbuf)));
154 }
155 else
156 {
157 if (print_timestamp)
158 fprintf (fh, "[%s] %s%s\n", timestamp_str, level_str, msg);
159 else
160 fprintf (fh, "%s%s\n", level_str, msg);
162 if (do_close) {
163 fclose (fh);
164 } else {
165 fflush(fh);
166 }
167 }
169 pthread_mutex_unlock (&file_lock);
171 return;
172 } /* void logfile_print */
174 static void logfile_log (int severity, const char *msg,
175 user_data_t __attribute__((unused)) *user_data)
176 {
177 if (severity > log_level)
178 return;
180 logfile_print (msg, severity, cdtime ());
181 } /* void logfile_log (int, const char *) */
183 static int logfile_notification (const notification_t *n,
184 user_data_t __attribute__((unused)) *user_data)
185 {
186 char buf[1024] = "";
187 char *buf_ptr = buf;
188 int buf_len = sizeof (buf);
189 int status;
191 status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s",
192 (n->severity == NOTIF_FAILURE) ? "FAILURE"
193 : ((n->severity == NOTIF_WARNING) ? "WARNING"
194 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
195 if (status > 0)
196 {
197 buf_ptr += status;
198 buf_len -= status;
199 }
201 #define APPEND(bufptr, buflen, key, value) \
202 if ((buflen > 0) && (strlen (value) > 0)) { \
203 status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \
204 if (status > 0) { \
205 bufptr += status; \
206 buflen -= status; \
207 } \
208 }
209 APPEND (buf_ptr, buf_len, "host", n->host);
210 APPEND (buf_ptr, buf_len, "plugin", n->plugin);
211 APPEND (buf_ptr, buf_len, "plugin_instance", n->plugin_instance);
212 APPEND (buf_ptr, buf_len, "type", n->type);
213 APPEND (buf_ptr, buf_len, "type_instance", n->type_instance);
214 APPEND (buf_ptr, buf_len, "message", n->message);
216 buf[sizeof (buf) - 1] = '\0';
218 logfile_print (buf, LOG_INFO,
219 (n->time != 0) ? n->time : cdtime ());
221 return (0);
222 } /* int logfile_notification */
224 void module_register (void)
225 {
226 plugin_register_config ("logfile", logfile_config,
227 config_keys, config_keys_num);
228 plugin_register_log ("logfile", logfile_log, /* user_data = */ NULL);
229 plugin_register_notification ("logfile", logfile_notification,
230 /* user_data = */ NULL);
231 } /* void module_register (void) */
233 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */