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 /* XXX - Is it neccessary/appropriate that logging to file is non-blocking?
57 * If not, there's a risk that execution blocks, meaning that voice blocks
58 * as well since uMurmur is single threaded by design. OTOH, what could
59 * cause a block? If the disk causes blocking, it is probably br0ken. but
60 * the log could be on a nfs or smb share, so let's set it up as
61 * non-blocking and we'll see what happens.
64 flags = fcntl(fd, F_GETFL, 0);
65 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
68 void Log_init(bool_t terminal)
70 const char *logfilename;
76 logfilename = getStrConf(LOGFILE);
77 if (logfilename != NULL) {
78 openlogfile(logfilename);
80 else openlog("uMurmurd", LOG_PID, LOG_DAEMON);
96 const char *logfilename;
99 logfilename = getStrConf(LOGFILE);
101 openlogfile(logfilename);
105 void logthis(const char *logstring, ...)
108 char buf[STRSIZE + 1];
110 va_start(argp, logstring);
111 vsnprintf(&buf[0], STRSIZE, logstring, argp);
114 fprintf(stderr, "%s\n", buf);
116 fprintf(logfile, "%s\n", buf);
118 syslog(LOG_INFO, "%s", buf);
121 void Log_warn(const char *logstring, ...)
124 char buf[STRSIZE + 1];
127 va_start(argp, logstring);
128 offset = sprintf(buf, "WARN: ");
129 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
132 fprintf(stderr, "%s\n", buf);
134 fprintf(logfile, "%s\n", buf);
136 syslog(LOG_WARNING, "%s", buf);
139 void Log_info(const char *logstring, ...)
142 char buf[STRSIZE + 1];
145 va_start(argp, logstring);
146 offset = sprintf(buf, "INFO: ");
147 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
150 fprintf(stderr, "%s\n", buf);
152 fprintf(logfile, "%s\n", buf);
156 syslog(LOG_INFO, "%s", buf);
159 void Log_info_client(client_t *client, const char *logstring, ...)
162 char buf[STRSIZE + 1];
165 va_start(argp, logstring);
166 offset = sprintf(buf, "INFO: ");
167 offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
169 offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d",
171 client->username == NULL ? "" : client->username,
172 inet_ntoa(client->remote_tcp.sin_addr),
173 ntohs(client->remote_tcp.sin_port));
175 fprintf(stderr, "%s\n", buf);
177 fprintf(logfile, "%s\n", buf);
179 syslog(LOG_INFO, "%s", buf);
183 void Log_debug(const char *logstring, ...)
186 char buf[STRSIZE + 1];
189 va_start(argp, logstring);
190 offset = sprintf(buf, "DEBUG: ");
191 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
194 fprintf(stderr, "%s\n", buf);
196 fprintf(logfile, "%s\n", buf);
198 syslog(LOG_DEBUG, "%s", buf);
202 void Log_fatal(const char *logstring, ...)
205 char buf[STRSIZE + 1];
207 va_start(argp, logstring);
208 offset = sprintf(buf, "FATAL: ");
209 vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
212 fprintf(stderr, "%s\n", buf);
214 fprintf(logfile, "%s\n", buf);
215 else { /* If logging subsystem is not initialized, fall back to syslog logging
216 * for fatal errors. Only config file reading that needs this currently.
219 openlog("uMurmurd", LOG_PID, LOG_DAEMON);
220 syslog(LOG_CRIT, "%s", buf);