-/* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
- Copyright (C) 2005-2012, Thorvald Natvig <thorvald@natvig.com>
+/* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
+ Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
All rights reserved.
#include "log.h"
#include "conf.h"
+#include "util.h"
#define STRSIZE 254
/* Set the stream as line buffered */
if (setvbuf(logfile, NULL, _IOLBF, 0) < 0)
Log_fatal("setvbuf() failed: %s\n", strerror(errno));
-
+
/* XXX - Is it neccessary/appropriate that logging to file is non-blocking?
* If not, there's a risk that execution blocks, meaning that voice blocks
* as well since uMurmur is single threaded by design. OTOH, what could
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
+static char *timestring(void)
+{
+ static char timebuf[32];
+ time_t t;
+ struct tm *timespec;
+
+ t= time(NULL);
+ timespec = localtime(&t);
+ strftime(timebuf, 32, "%b %e %T", timespec);
+ return timebuf;
+}
+
void Log_init(bool_t terminal)
{
const char *logfilename;
-
- termprint = terminal;
+
+ termprint = terminal;
if (termprint)
return;
-
+
logfilename = getStrConf(LOGFILE);
if (logfilename != NULL) {
openlogfile(logfilename);
return;
else if (logfile)
fclose(logfile);
- else
+ else
closelog();
}
-
+
void Log_reset()
{
const char *logfilename;
-
+
if (logfile) {
logfilename = getStrConf(LOGFILE);
fclose(logfile);
{
va_list argp;
char buf[STRSIZE + 1];
-
+
va_start(argp, logstring);
vsnprintf(&buf[0], STRSIZE, logstring, argp);
va_end(argp);
+
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
- fprintf(logfile, "%s\n", buf);
+ fprintf(logfile, "%s %s\n", timestring(), buf);
else
syslog(LOG_INFO, "%s", buf);
}
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
-
+
+ if (termprint || logfile)
+ offset = sprintf(buf, "WARN: ");
+
va_start(argp, logstring);
- offset = sprintf(buf, "WARN: ");
vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
+
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
- fprintf(logfile, "%s\n", buf);
+ fprintf(logfile, "%s %s\n", timestring(), buf);
else
syslog(LOG_WARNING, "%s", buf);
}
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
-
+
+ if (termprint || logfile)
+ offset = sprintf(buf, "INFO: ");
+
va_start(argp, logstring);
- offset = sprintf(buf, "INFO: ");
vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
+
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
- fprintf(logfile, "%s\n", buf);
+ fprintf(logfile, "%s %s\n", timestring(), buf);
else
syslog(LOG_INFO, "%s", buf);
}
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
-
+
+ if (termprint || logfile)
+ offset = sprintf(buf, "INFO: ");
+
va_start(argp, logstring);
- offset = sprintf(buf, "INFO: ");
offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
+
+ char *clientAddressString = Util_clientAddressToString(client);
offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d",
- client->sessionId,
- client->username == NULL ? "" : client->username,
- inet_ntoa(client->remote_tcp.sin_addr),
- ntohs(client->remote_tcp.sin_port));
+ client->sessionId,
+ client->username == NULL ? "" : client->username,
+ clientAddressString,
+ Util_clientAddressToPortTCP(client));
+ free(clientAddressString);
+
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
- fprintf(logfile, "%s\n", buf);
+ fprintf(logfile, "%s %s\n", timestring(), buf);
else
syslog(LOG_INFO, "%s", buf);
}
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
-
+
+ if (termprint || logfile)
+ offset = sprintf(buf, "DEBUG: ");
+
va_start(argp, logstring);
- offset = sprintf(buf, "DEBUG: ");
vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
- fprintf(logfile, "%s\n", buf);
+ fprintf(logfile, "%s %s\n", timestring(), buf);
else
syslog(LOG_DEBUG, "%s", buf);
}
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
+
+ if (termprint || logfile)
+ offset = sprintf(buf, "FATAL: ");
+
va_start(argp, logstring);
- offset = sprintf(buf, "FATAL: ");
vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
+
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
- fprintf(logfile, "%s\n", buf);
+ fprintf(logfile, "%s %s\n", timestring(), buf);
else { /* If logging subsystem is not initialized, fall back to stderr +
- * syslog logging for fatal errors.
+ * syslog logging for fatal errors.
*/
if (!init) {
openlog("uMurmurd", LOG_PID, LOG_DAEMON);
}
syslog(LOG_CRIT, "%s", buf);
}
-
+
exit(1);
}