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.
45 static bool_t termprint, init;
48 static void openlogfile(const char *logfilename)
51 logfile = fopen(logfilename, "a");
52 if (logfile == NULL) {
53 Log_fatal("Failed to open log file '%s' for writing: %s\n", logfilename, strerror(errno));
56 /* Set the stream as line buffered */
57 if (setvbuf(logfile, NULL, _IOLBF, 0) < 0)
58 Log_fatal("setvbuf() failed: %s\n", strerror(errno));
60 /* XXX - Is it neccessary/appropriate that logging to file is non-blocking?
61 * If not, there's a risk that execution blocks, meaning that voice blocks
62 * as well since uMurmur is single threaded by design. OTOH, what could
63 * cause a block? If the disk causes blocking, it is probably br0ken, but
64 * the log could be on a nfs or smb share, so let's set it up as
65 * non-blocking and we'll see what happens.
68 flags = fcntl(fd, F_GETFL, 0);
69 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
72 static char *timestring(void)
74 static char timebuf[32];
79 timespec = localtime(&t);
80 strftime(timebuf, 32, "%b %e %T", timespec);
84 void Log_init(bool_t terminal)
86 const char *logfilename;
92 logfilename = getStrConf(LOGFILE);
93 if (logfilename != NULL) {
94 openlogfile(logfilename);
96 else openlog("uMurmurd", LOG_PID, LOG_DAEMON);
112 const char *logfilename;
115 logfilename = getStrConf(LOGFILE);
117 openlogfile(logfilename);
121 void logthis(const char *logstring, ...)
124 char buf[STRSIZE + 1];
126 va_start(argp, logstring);
127 vsnprintf(&buf[0], STRSIZE, logstring, argp);
131 fprintf(stderr, "%s\n", buf);
133 fprintf(logfile, "%s %s\n", timestring(), buf);
135 syslog(LOG_INFO, "%s", buf);
138 void Log_warn(const char *logstring, ...)
141 char buf[STRSIZE + 1];
144 if (termprint || logfile)
145 offset = sprintf(buf, "WARN: ");
147 va_start(argp, logstring);
148 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
152 fprintf(stderr, "%s\n", buf);
154 fprintf(logfile, "%s %s\n", timestring(), buf);
156 syslog(LOG_WARNING, "%s", buf);
159 void Log_info(const char *logstring, ...)
162 char buf[STRSIZE + 1];
165 if (termprint || logfile)
166 offset = sprintf(buf, "INFO: ");
168 va_start(argp, logstring);
169 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
173 fprintf(stderr, "%s\n", buf);
175 fprintf(logfile, "%s %s\n", timestring(), buf);
177 syslog(LOG_INFO, "%s", buf);
180 void Log_info_client(client_t *client, const char *logstring, ...)
183 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 if(client->remote_tcp.ss_family == AF_INET)
195 port = ntohs(((struct sockaddr_in*)&client->remote_tcp)->sin_port);
197 port = ntohs(((struct sockaddr_in6*)&client->remote_tcp)->sin6_port);
199 offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d",
201 client->username == NULL ? "" : client->username,
202 client->addressString,
206 fprintf(stderr, "%s\n", buf);
208 fprintf(logfile, "%s %s\n", timestring(), buf);
210 syslog(LOG_INFO, "%s", buf);
214 void Log_debug(const char *logstring, ...)
217 char buf[STRSIZE + 1];
220 if (termprint || logfile)
221 offset = sprintf(buf, "DEBUG: ");
223 va_start(argp, logstring);
224 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
227 fprintf(stderr, "%s\n", buf);
229 fprintf(logfile, "%s %s\n", timestring(), buf);
231 syslog(LOG_DEBUG, "%s", buf);
235 void Log_fatal(const char *logstring, ...)
238 char buf[STRSIZE + 1];
241 if (termprint || logfile)
242 offset = sprintf(buf, "FATAL: ");
244 va_start(argp, logstring);
245 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
249 fprintf(stderr, "%s\n", buf);
251 fprintf(logfile, "%s %s\n", timestring(), buf);
252 else { /* If logging subsystem is not initialized, fall back to stderr +
253 * syslog logging for fatal errors.
256 openlog("uMurmurd", LOG_PID, LOG_DAEMON);
257 fprintf(stderr, "%s\n", buf);
259 syslog(LOG_CRIT, "%s", buf);