Code cleanup
[umurmur.git] / src / log.c
1 /* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
10    - Redistributions of source code must retain the above copyright notice,
11      this list of conditions and the following disclaimer.
12    - Redistributions in binary form must reproduce the above copyright notice,
13      this list of conditions and the following disclaimer in the documentation
14      and/or other materials provided with the distribution.
15    - Neither the name of the Developers nor the names of its contributors may
16      be used to endorse or promote products derived from this software without
17      specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <syslog.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39
40 #include "log.h"
41 #include "conf.h"
42 #include "util.h"
43
44 #define STRSIZE 254
45
46 static bool_t termprint, init;
47 static FILE *logfile;
48
49 static void openlogfile(const char *logfilename)
50 {
51         int fd, flags;
52         logfile = fopen(logfilename, "a");
53         if (logfile == NULL) {
54                 Log_fatal("Failed to open log file '%s' for writing: %s\n", logfilename, strerror(errno));
55         }
56
57         /* Set the stream as line buffered */
58         if (setvbuf(logfile, NULL, _IOLBF, 0) < 0)
59                 Log_fatal("setvbuf() failed: %s\n", strerror(errno));
60
61         /* XXX - Is it neccessary/appropriate that logging to file is non-blocking?
62          * If not, there's a risk that execution blocks, meaning that voice blocks
63          * as well since uMurmur is single threaded by design. OTOH, what could
64          * cause a block? If the disk causes blocking, it is probably br0ken, but
65          * the log could be on a nfs or smb share, so let's set it up as
66          * non-blocking and we'll see what happens.
67          */
68         fd = fileno(logfile);
69         flags = fcntl(fd, F_GETFL, 0);
70         fcntl(fd, F_SETFL, flags | O_NONBLOCK);
71 }
72
73 static char *timestring(void)
74 {
75         static char timebuf[32];
76         time_t t;
77         struct tm *timespec;
78
79         t= time(NULL);
80         timespec = localtime(&t);
81         strftime(timebuf, 32, "%b %e %T", timespec);
82         return timebuf;
83 }
84
85 void Log_init(bool_t terminal)
86 {
87         const char *logfilename;
88
89         termprint = terminal;
90         if (termprint)
91                 return;
92
93         logfilename = getStrConf(LOGFILE);
94         if (logfilename != NULL) {
95                 openlogfile(logfilename);
96         }
97         else openlog("uMurmurd", LOG_PID, LOG_DAEMON);
98         init = true;
99 }
100
101 void Log_free()
102 {
103         if (termprint)
104                 return;
105         else if (logfile)
106                 fclose(logfile);
107         else
108                 closelog();
109 }
110
111 void Log_reset()
112 {
113         const char *logfilename;
114
115         if (logfile) {
116                 logfilename = getStrConf(LOGFILE);
117                 fclose(logfile);
118                 openlogfile(logfilename);
119         }
120 }
121
122 void logthis(const char *logstring, ...)
123 {
124         va_list argp;
125         char buf[STRSIZE + 1];
126
127         va_start(argp, logstring);
128         vsnprintf(&buf[0], STRSIZE, logstring, argp);
129         va_end(argp);
130
131         if (termprint)
132                 fprintf(stderr, "%s\n", buf);
133         else if (logfile)
134                 fprintf(logfile, "%s %s\n", timestring(), buf);
135         else
136                 syslog(LOG_INFO, "%s", buf);
137 }
138
139 void Log_warn(const char *logstring, ...)
140 {
141         va_list argp;
142         char buf[STRSIZE + 1];
143         int offset = 0;
144
145         if (termprint || logfile)
146                 offset = sprintf(buf, "WARN: ");
147
148         va_start(argp, logstring);
149         vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
150         va_end(argp);
151
152         if (termprint)
153                 fprintf(stderr, "%s\n", buf);
154         else if (logfile)
155                 fprintf(logfile, "%s %s\n", timestring(), buf);
156         else
157                 syslog(LOG_WARNING, "%s", buf);
158 }
159
160 void Log_info(const char *logstring, ...)
161 {
162         va_list argp;
163         char buf[STRSIZE + 1];
164         int offset = 0;
165
166         if (termprint || logfile)
167                 offset = sprintf(buf, "INFO: ");
168
169         va_start(argp, logstring);
170         vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
171         va_end(argp);
172
173         if (termprint)
174                 fprintf(stderr, "%s\n", buf);
175         else if (logfile)
176                 fprintf(logfile, "%s %s\n", timestring(), buf);
177         else
178                 syslog(LOG_INFO, "%s", buf);
179 }
180
181 void Log_info_client(client_t *client, const char *logstring, ...)
182 {
183         va_list argp;
184         char buf[STRSIZE + 1];
185         int offset = 0;
186
187         if (termprint || logfile)
188                 offset = sprintf(buf, "INFO: ");
189
190         va_start(argp, logstring);
191         offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
192         va_end(argp);
193
194         offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d",
195                 client->sessionId,
196                 client->username == NULL ? "" : client->username,
197                 Util_clientAddressToString(client),
198                 Util_clientAddressToPortTCP(client));
199
200         if (termprint)
201                 fprintf(stderr, "%s\n", buf);
202         else if (logfile)
203                 fprintf(logfile, "%s %s\n", timestring(), buf);
204         else
205                 syslog(LOG_INFO, "%s", buf);
206 }
207
208 #ifdef DEBUG
209 void Log_debug(const char *logstring, ...)
210 {
211         va_list argp;
212         char buf[STRSIZE + 1];
213         int offset = 0;
214
215         if (termprint || logfile)
216                 offset = sprintf(buf, "DEBUG: ");
217
218         va_start(argp, logstring);
219         vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
220         va_end(argp);
221         if (termprint)
222                 fprintf(stderr, "%s\n", buf);
223         else if (logfile)
224                 fprintf(logfile, "%s %s\n", timestring(), buf);
225         else
226                 syslog(LOG_DEBUG, "%s", buf);
227 }
228 #endif
229
230 void Log_fatal(const char *logstring, ...)
231 {
232         va_list argp;
233         char buf[STRSIZE + 1];
234         int offset = 0;
235
236         if (termprint || logfile)
237                 offset = sprintf(buf, "FATAL: ");
238
239         va_start(argp, logstring);
240         vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
241         va_end(argp);
242
243         if (termprint)
244                 fprintf(stderr, "%s\n", buf);
245         else if (logfile)
246                 fprintf(logfile, "%s %s\n", timestring(), buf);
247         else { /* If logging subsystem is not initialized, fall back to stderr +
248                         * syslog logging for fatal errors.
249                         */
250                 if (!init) {
251                         openlog("uMurmurd", LOG_PID, LOG_DAEMON);
252                         fprintf(stderr, "%s\n", buf);
253                 }
254                 syslog(LOG_CRIT, "%s", buf);
255         }
256
257         exit(1);
258 }