Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git] / src / logfile.c
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  **/
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "plugin.h"
33
34 #define DEFAULT_LOGFILE LOCALSTATEDIR "/log/collectd.log"
35
36 #if COLLECT_DEBUG
37 static int log_level = LOG_DEBUG;
38 #else
39 static int log_level = LOG_INFO;
40 #endif /* COLLECT_DEBUG */
41
42 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
43
44 static char *log_file = NULL;
45 static int print_timestamp = 1;
46 static int print_severity = 0;
47
48 static const char *config_keys[] = {"LogLevel", "File", "Timestamp",
49                                     "PrintSeverity"};
50 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
51
52 static int logfile_config(const char *key, const char *value) {
53   if (0 == strcasecmp(key, "LogLevel")) {
54     log_level = parse_log_severity(value);
55     if (log_level < 0) {
56       log_level = LOG_INFO;
57       ERROR("logfile: invalid loglevel [%s] defaulting to 'info'", value);
58       return (1);
59     }
60   } else if (0 == strcasecmp(key, "File")) {
61     sfree(log_file);
62     log_file = strdup(value);
63   } else if (0 == strcasecmp(key, "Timestamp")) {
64     if (IS_FALSE(value))
65       print_timestamp = 0;
66     else
67       print_timestamp = 1;
68   } else if (0 == strcasecmp(key, "PrintSeverity")) {
69     if (IS_FALSE(value))
70       print_severity = 0;
71     else
72       print_severity = 1;
73   } else {
74     return -1;
75   }
76   return 0;
77 } /* int logfile_config (const char *, const char *) */
78
79 static void logfile_print(const char *msg, int severity,
80                           cdtime_t timestamp_time) {
81   FILE *fh;
82   _Bool do_close = 0;
83   struct tm timestamp_tm;
84   char timestamp_str[64];
85   char level_str[16] = "";
86
87   if (print_severity) {
88     switch (severity) {
89     case LOG_ERR:
90       snprintf(level_str, sizeof(level_str), "[error] ");
91       break;
92     case LOG_WARNING:
93       snprintf(level_str, sizeof(level_str), "[warning] ");
94       break;
95     case LOG_NOTICE:
96       snprintf(level_str, sizeof(level_str), "[notice] ");
97       break;
98     case LOG_INFO:
99       snprintf(level_str, sizeof(level_str), "[info] ");
100       break;
101     case LOG_DEBUG:
102       snprintf(level_str, sizeof(level_str), "[debug] ");
103       break;
104     default:
105       break;
106     }
107   }
108
109   if (print_timestamp) {
110     time_t tt = CDTIME_T_TO_TIME_T(timestamp_time);
111     localtime_r(&tt, &timestamp_tm);
112
113     strftime(timestamp_str, sizeof(timestamp_str), "%Y-%m-%d %H:%M:%S",
114              &timestamp_tm);
115     timestamp_str[sizeof(timestamp_str) - 1] = '\0';
116   }
117
118   pthread_mutex_lock(&file_lock);
119
120   if (log_file == NULL) {
121     fh = fopen(DEFAULT_LOGFILE, "a");
122     do_close = 1;
123   } else if (strcasecmp(log_file, "stderr") == 0)
124     fh = stderr;
125   else if (strcasecmp(log_file, "stdout") == 0)
126     fh = stdout;
127   else {
128     fh = fopen(log_file, "a");
129     do_close = 1;
130   }
131
132   if (fh == NULL) {
133     char errbuf[1024];
134     fprintf(stderr, "logfile plugin: fopen (%s) failed: %s\n",
135             (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
136             sstrerror(errno, errbuf, sizeof(errbuf)));
137   } else {
138     if (print_timestamp)
139       fprintf(fh, "[%s] %s%s\n", timestamp_str, level_str, msg);
140     else
141       fprintf(fh, "%s%s\n", level_str, msg);
142
143     if (do_close) {
144       fclose(fh);
145     } else {
146       fflush(fh);
147     }
148   }
149
150   pthread_mutex_unlock(&file_lock);
151
152   return;
153 } /* void logfile_print */
154
155 static void logfile_log(int severity, const char *msg,
156                         user_data_t __attribute__((unused)) * user_data) {
157   if (severity > log_level)
158     return;
159
160   logfile_print(msg, severity, cdtime());
161 } /* void logfile_log (int, const char *) */
162
163 static int logfile_notification(const notification_t *n,
164                                 user_data_t __attribute__((unused)) *
165                                     user_data) {
166   char buf[1024] = "";
167   char *buf_ptr = buf;
168   int buf_len = sizeof(buf);
169   int status;
170
171   status = ssnprintf(
172       buf_ptr, buf_len, "Notification: severity = %s",
173       (n->severity == NOTIF_FAILURE)
174           ? "FAILURE"
175           : ((n->severity == NOTIF_WARNING)
176                  ? "WARNING"
177                  : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
178   if (status > 0) {
179     buf_ptr += status;
180     buf_len -= status;
181   }
182
183 #define APPEND(bufptr, buflen, key, value)                                     \
184   if ((buflen > 0) && (strlen(value) > 0)) {                                   \
185     status = ssnprintf(bufptr, buflen, ", %s = %s", key, value);               \
186     if (status > 0) {                                                          \
187       bufptr += status;                                                        \
188       buflen -= status;                                                        \
189     }                                                                          \
190   }
191   APPEND(buf_ptr, buf_len, "host", n->host);
192   APPEND(buf_ptr, buf_len, "plugin", n->plugin);
193   APPEND(buf_ptr, buf_len, "plugin_instance", n->plugin_instance);
194   APPEND(buf_ptr, buf_len, "type", n->type);
195   APPEND(buf_ptr, buf_len, "type_instance", n->type_instance);
196   APPEND(buf_ptr, buf_len, "message", n->message);
197
198   buf[sizeof(buf) - 1] = '\0';
199
200   logfile_print(buf, LOG_INFO, (n->time != 0) ? n->time : cdtime());
201
202   return (0);
203 } /* int logfile_notification */
204
205 void module_register(void) {
206   plugin_register_config("logfile", logfile_config, config_keys,
207                          config_keys_num);
208   plugin_register_log("logfile", logfile_log, /* user_data = */ NULL);
209   plugin_register_notification("logfile", logfile_notification,
210                                /* user_data = */ NULL);
211 } /* void module_register (void) */
212
213 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */