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