1 /* Copyright (C) 2009-2013, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2013, 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];
186 if (termprint || logfile)
187 offset = sprintf(buf, "INFO: ");
189 va_start(argp, logstring);
190 offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
193 offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d",
195 client->username == NULL ? "" : client->username,
196 inet_ntoa(client->remote_tcp.sin_addr),
197 ntohs(client->remote_tcp.sin_port));
199 fprintf(stderr, "%s\n", buf);
201 fprintf(logfile, "%s %s\n", timestring(), buf);
203 syslog(LOG_INFO, "%s", buf);
207 void Log_debug(const char *logstring, ...)
210 char buf[STRSIZE + 1];
213 if (termprint || logfile)
214 offset = sprintf(buf, "DEBUG: ");
216 va_start(argp, logstring);
217 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
220 fprintf(stderr, "%s\n", buf);
222 fprintf(logfile, "%s %s\n", timestring(), buf);
224 syslog(LOG_DEBUG, "%s", buf);
228 void Log_fatal(const char *logstring, ...)
231 char buf[STRSIZE + 1];
234 if (termprint || logfile)
235 offset = sprintf(buf, "FATAL: ");
237 va_start(argp, logstring);
238 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
242 fprintf(stderr, "%s\n", buf);
244 fprintf(logfile, "%s %s\n", timestring(), buf);
245 else { /* If logging subsystem is not initialized, fall back to stderr +
246 * syslog logging for fatal errors.
249 openlog("uMurmurd", LOG_PID, LOG_DAEMON);
250 fprintf(stderr, "%s\n", buf);
252 syslog(LOG_CRIT, "%s", buf);