Use Client_find_by_session() instead of a few open-coded loops.
[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         char *clientAddressString = Util_clientAddressToString(client);
195         offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d",
196                 client->sessionId,
197                 client->username == NULL ? "" : client->username,
198                 clientAddressString,
199                 Util_clientAddressToPortTCP(client));
200         free(clientAddressString);
201
202         if (termprint)
203                 fprintf(stderr, "%s\n", buf);
204         else if (logfile)
205                 fprintf(logfile, "%s %s\n", timestring(), buf);
206         else
207                 syslog(LOG_INFO, "%s", buf);
208 }
209
210 #ifdef DEBUG
211 void Log_debug(const char *logstring, ...)
212 {
213         va_list argp;
214         char buf[STRSIZE + 1];
215         int offset = 0;
216
217         if (termprint || logfile)
218                 offset = sprintf(buf, "DEBUG: ");
219
220         va_start(argp, logstring);
221         vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
222         va_end(argp);
223         if (termprint)
224                 fprintf(stderr, "%s\n", buf);
225         else if (logfile)
226                 fprintf(logfile, "%s %s\n", timestring(), buf);
227         else
228                 syslog(LOG_DEBUG, "%s", buf);
229 }
230 #endif
231
232 void Log_fatal(const char *logstring, ...)
233 {
234         va_list argp;
235         char buf[STRSIZE + 1];
236         int offset = 0;
237
238         if (termprint || logfile)
239                 offset = sprintf(buf, "FATAL: ");
240
241         va_start(argp, logstring);
242         vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
243         va_end(argp);
244
245         if (termprint)
246                 fprintf(stderr, "%s\n", buf);
247         else if (logfile)
248                 fprintf(logfile, "%s %s\n", timestring(), buf);
249         else { /* If logging subsystem is not initialized, fall back to stderr +
250                         * syslog logging for fatal errors.
251                         */
252                 if (!init) {
253                         openlog("uMurmurd", LOG_PID, LOG_DAEMON);
254                         fprintf(stderr, "%s\n", buf);
255                 }
256                 syslog(LOG_CRIT, "%s", buf);
257         }
258
259         exit(1);
260 }