1 /* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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.
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.
46 static bool_t termprint, init;
49 static void openlogfile(const char *logfilename)
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));
57 /* Set the stream as line buffered */
58 if (setvbuf(logfile, NULL, _IOLBF, 0) < 0)
59 Log_fatal("setvbuf() failed: %s\n", strerror(errno));
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.
69 flags = fcntl(fd, F_GETFL, 0);
70 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
73 static char *timestring(void)
75 static char timebuf[32];
80 timespec = localtime(&t);
81 strftime(timebuf, 32, "%b %e %T", timespec);
85 void Log_init(bool_t terminal)
87 const char *logfilename;
93 logfilename = getStrConf(LOGFILE);
94 if (logfilename != NULL) {
95 openlogfile(logfilename);
97 else openlog("uMurmurd", LOG_PID, LOG_DAEMON);
113 const char *logfilename;
116 logfilename = getStrConf(LOGFILE);
118 openlogfile(logfilename);
122 void logthis(const char *logstring, ...)
125 char buf[STRSIZE + 1];
127 va_start(argp, logstring);
128 vsnprintf(&buf[0], STRSIZE, logstring, argp);
132 fprintf(stderr, "%s\n", buf);
134 fprintf(logfile, "%s %s\n", timestring(), buf);
136 syslog(LOG_INFO, "%s", buf);
139 void Log_warn(const char *logstring, ...)
142 char buf[STRSIZE + 1];
145 if (termprint || logfile)
146 offset = sprintf(buf, "WARN: ");
148 va_start(argp, logstring);
149 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
153 fprintf(stderr, "%s\n", buf);
155 fprintf(logfile, "%s %s\n", timestring(), buf);
157 syslog(LOG_WARNING, "%s", buf);
160 void Log_info(const char *logstring, ...)
163 char buf[STRSIZE + 1];
166 if (termprint || logfile)
167 offset = sprintf(buf, "INFO: ");
169 va_start(argp, logstring);
170 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
174 fprintf(stderr, "%s\n", buf);
176 fprintf(logfile, "%s %s\n", timestring(), buf);
178 syslog(LOG_INFO, "%s", buf);
181 void Log_info_client(client_t *client, const char *logstring, ...)
184 char buf[STRSIZE + 1];
187 if (termprint || logfile)
188 offset = sprintf(buf, "INFO: ");
190 va_start(argp, logstring);
191 offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
194 offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d",
196 client->username == NULL ? "" : client->username,
197 Util_clientAddressToString(client),
198 Util_clientAddressToPortTCP(client));
201 fprintf(stderr, "%s\n", buf);
203 fprintf(logfile, "%s %s\n", timestring(), buf);
205 syslog(LOG_INFO, "%s", buf);
209 void Log_debug(const char *logstring, ...)
212 char buf[STRSIZE + 1];
215 if (termprint || logfile)
216 offset = sprintf(buf, "DEBUG: ");
218 va_start(argp, logstring);
219 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
222 fprintf(stderr, "%s\n", buf);
224 fprintf(logfile, "%s %s\n", timestring(), buf);
226 syslog(LOG_DEBUG, "%s", buf);
230 void Log_fatal(const char *logstring, ...)
233 char buf[STRSIZE + 1];
236 if (termprint || logfile)
237 offset = sprintf(buf, "FATAL: ");
239 va_start(argp, logstring);
240 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
244 fprintf(stderr, "%s\n", buf);
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.
251 openlog("uMurmurd", LOG_PID, LOG_DAEMON);
252 fprintf(stderr, "%s\n", buf);
254 syslog(LOG_CRIT, "%s", buf);