1 /* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2010, 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 void Log_init(bool_t terminal)
74 const char *logfilename;
80 logfilename = getStrConf(LOGFILE);
81 if (logfilename != NULL) {
82 openlogfile(logfilename);
84 else openlog("uMurmurd", LOG_PID, LOG_DAEMON);
100 const char *logfilename;
103 logfilename = getStrConf(LOGFILE);
105 openlogfile(logfilename);
109 void logthis(const char *logstring, ...)
112 char buf[STRSIZE + 1];
114 va_start(argp, logstring);
115 vsnprintf(&buf[0], STRSIZE, logstring, argp);
118 fprintf(stderr, "%s\n", buf);
120 fprintf(logfile, "%s\n", buf);
122 syslog(LOG_INFO, "%s", buf);
125 void Log_warn(const char *logstring, ...)
128 char buf[STRSIZE + 1];
131 va_start(argp, logstring);
132 offset = sprintf(buf, "WARN: ");
133 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
136 fprintf(stderr, "%s\n", buf);
138 fprintf(logfile, "%s\n", buf);
140 syslog(LOG_WARNING, "%s", buf);
143 void Log_info(const char *logstring, ...)
146 char buf[STRSIZE + 1];
149 va_start(argp, logstring);
150 offset = sprintf(buf, "INFO: ");
151 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
154 fprintf(stderr, "%s\n", buf);
156 fprintf(logfile, "%s\n", buf);
158 syslog(LOG_INFO, "%s", buf);
161 void Log_info_client(client_t *client, const char *logstring, ...)
164 char buf[STRSIZE + 1];
167 va_start(argp, logstring);
168 offset = sprintf(buf, "INFO: ");
169 offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
171 offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d",
173 client->username == NULL ? "" : client->username,
174 inet_ntoa(client->remote_tcp.sin_addr),
175 ntohs(client->remote_tcp.sin_port));
177 fprintf(stderr, "%s\n", buf);
179 fprintf(logfile, "%s\n", buf);
181 syslog(LOG_INFO, "%s", buf);
185 void Log_debug(const char *logstring, ...)
188 char buf[STRSIZE + 1];
191 va_start(argp, logstring);
192 offset = sprintf(buf, "DEBUG: ");
193 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
196 fprintf(stderr, "%s\n", buf);
198 fprintf(logfile, "%s\n", buf);
200 syslog(LOG_DEBUG, "%s", buf);
204 void Log_fatal(const char *logstring, ...)
207 char buf[STRSIZE + 1];
209 va_start(argp, logstring);
210 offset = sprintf(buf, "FATAL: ");
211 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
214 fprintf(stderr, "%s\n", buf);
216 fprintf(logfile, "%s\n", buf);
217 else { /* If logging subsystem is not initialized, fall back to stderr +
218 * syslog logging for fatal errors.
221 openlog("uMurmurd", LOG_PID, LOG_DAEMON);
222 fprintf(stderr, "%s\n", buf);
224 syslog(LOG_CRIT, "%s", buf);