projects
/
umurmur.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
closed memory leak
[umurmur.git]
/
src
/
log.c
diff --git
a/src/log.c
b/src/log.c
index 39b9cf9e48bc3f8fcc0bf943735d9ce2c798bcfd..61a92d24994109f34c06f23a1b165a9440ebc096 100644
(file)
--- a/
src/log.c
+++ b/
src/log.c
@@
-1,5
+1,5
@@
-/* Copyright (C) 2009-201
0
, Martin Johansson <martin@fatbob.nu>
- Copyright (C) 2005-201
0
, Thorvald Natvig <thorvald@natvig.com>
+/* Copyright (C) 2009-201
4
, Martin Johansson <martin@fatbob.nu>
+ Copyright (C) 2005-201
4
, Thorvald Natvig <thorvald@natvig.com>
All rights reserved.
All rights reserved.
@@
-39,6
+39,7
@@
#include "log.h"
#include "conf.h"
#include "log.h"
#include "conf.h"
+#include "util.h"
#define STRSIZE 254
#define STRSIZE 254
@@
-53,10
+54,14
@@
static void openlogfile(const char *logfilename)
Log_fatal("Failed to open log file '%s' for writing: %s\n", logfilename, strerror(errno));
}
Log_fatal("Failed to open log file '%s' for writing: %s\n", logfilename, strerror(errno));
}
+ /* 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
/* 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
- * cause a block? If the disk causes blocking, it is probably br0ken
.
but
+ * cause a block? If the disk causes blocking, it is probably br0ken
,
but
* the log could be on a nfs or smb share, so let's set it up as
* non-blocking and we'll see what happens.
*/
* the log could be on a nfs or smb share, so let's set it up as
* non-blocking and we'll see what happens.
*/
@@
-65,14
+70,26
@@
static void openlogfile(const char *logfilename)
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
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;
void Log_init(bool_t terminal)
{
const char *logfilename;
-
- termprint = terminal;
+
+ termprint = terminal;
if (termprint)
return;
if (termprint)
return;
-
+
logfilename = getStrConf(LOGFILE);
if (logfilename != NULL) {
openlogfile(logfilename);
logfilename = getStrConf(LOGFILE);
if (logfilename != NULL) {
openlogfile(logfilename);
@@
-87,14
+104,14
@@
void Log_free()
return;
else if (logfile)
fclose(logfile);
return;
else if (logfile)
fclose(logfile);
- else
+ else
closelog();
}
closelog();
}
-
+
void Log_reset()
{
const char *logfilename;
void Log_reset()
{
const char *logfilename;
-
+
if (logfile) {
logfilename = getStrConf(LOGFILE);
fclose(logfile);
if (logfile) {
logfilename = getStrConf(LOGFILE);
fclose(logfile);
@@
-106,14
+123,15
@@
void logthis(const char *logstring, ...)
{
va_list argp;
char buf[STRSIZE + 1];
{
va_list argp;
char buf[STRSIZE + 1];
-
+
va_start(argp, logstring);
vsnprintf(&buf[0], STRSIZE, logstring, argp);
va_end(argp);
va_start(argp, logstring);
vsnprintf(&buf[0], STRSIZE, logstring, argp);
va_end(argp);
+
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
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);
}
else
syslog(LOG_INFO, "%s", buf);
}
@@
-123,15
+141,18
@@
void Log_warn(const char *logstring, ...)
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
-
+
+ if (termprint || logfile)
+ offset = sprintf(buf, "WARN: ");
+
va_start(argp, logstring);
va_start(argp, logstring);
- offset = sprintf(buf, "WARN: ");
vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
+
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
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);
}
else
syslog(LOG_WARNING, "%s", buf);
}
@@
-141,17
+162,18
@@
void Log_info(const char *logstring, ...)
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
-
+
+ if (termprint || logfile)
+ offset = sprintf(buf, "INFO: ");
+
va_start(argp, logstring);
va_start(argp, logstring);
- offset = sprintf(buf, "INFO: ");
vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
+
if (termprint)
fprintf(stderr, "%s\n", buf);
if (termprint)
fprintf(stderr, "%s\n", buf);
- else if (logfile) {
- fprintf(logfile, "%s\n", buf);
- fflush(logfile);
- }
+ else if (logfile)
+ fprintf(logfile, "%s %s\n", timestring(), buf);
else
syslog(LOG_INFO, "%s", buf);
}
else
syslog(LOG_INFO, "%s", buf);
}
@@
-161,20
+183,24
@@
void Log_info_client(client_t *client, const char *logstring, ...)
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
-
+
+ if (termprint || logfile)
+ offset = sprintf(buf, "INFO: ");
+
va_start(argp, logstring);
va_start(argp, logstring);
- offset = sprintf(buf, "INFO: ");
offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
+
offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d",
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,
+ Util_clientAddressToString(client),
+ Util_clientAddressToPortTCP(client));
+
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
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);
}
else
syslog(LOG_INFO, "%s", buf);
}
@@
-185,15
+211,17
@@
void Log_debug(const char *logstring, ...)
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
-
+
+ if (termprint || logfile)
+ offset = sprintf(buf, "DEBUG: ");
+
va_start(argp, logstring);
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)
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);
}
else
syslog(LOG_DEBUG, "%s", buf);
}
@@
-204,21
+232,27
@@
void Log_fatal(const char *logstring, ...)
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
va_list argp;
char buf[STRSIZE + 1];
int offset = 0;
+
+ if (termprint || logfile)
+ offset = sprintf(buf, "FATAL: ");
+
va_start(argp, logstring);
va_start(argp, logstring);
- offset = sprintf(buf, "FATAL: ");
vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
va_end(argp);
+
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
if (termprint)
fprintf(stderr, "%s\n", buf);
else if (logfile)
- fprintf(logfile, "%s
\n"
, buf);
- else { /* If logging subsystem is not initialized, fall back to s
yslog logging
- *
for fatal errors. Only config file reading that needs this currently
.
+ fprintf(logfile, "%s
%s\n", timestring()
, buf);
+ else { /* If logging subsystem is not initialized, fall back to s
tderr +
+ *
syslog logging for fatal errors
.
*/
*/
- if (!init)
+ if (!init)
{
openlog("uMurmurd", LOG_PID, LOG_DAEMON);
openlog("uMurmurd", LOG_PID, LOG_DAEMON);
+ fprintf(stderr, "%s\n", buf);
+ }
syslog(LOG_CRIT, "%s", buf);
}
syslog(LOG_CRIT, "%s", buf);
}
-
+
exit(1);
}
exit(1);
}