summaryrefslogtreecommitdiff
path: root/src/common/log.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/log.c')
-rw-r--r--src/common/log.c40
1 files changed, 27 insertions, 13 deletions
diff --git a/src/common/log.c b/src/common/log.c
index ff6475e..2301e19 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -25,7 +25,6 @@
#include "common/log.h"
#include "common/lists.h"
-#include "knot/common.h"
#include "knot/conf/conf.h"
/*! Log source table. */
@@ -210,19 +209,31 @@ static int _log_msg(logsrc_t src, int level, const char *msg)
// Convert level to mask
level = LOG_MASK(level);
-
+
/* Prefix date and time. */
char tstr[128] = {0};
int tlen = 0;
struct tm lt;
struct timeval tv;
gettimeofday(&tv, NULL);
- if (localtime_r(&tv.tv_sec, &lt) != NULL) {
- tlen = strftime(tstr, sizeof(tstr) - 1,
- "%Y-%m-%dT%H:%M:%S", &lt);
- if (tlen > 0) {
- char pm = (lt.tm_gmtoff > 0)?'+':'-';
- snprintf(tstr+tlen, 128-tlen-1, ".%.6lu%c%.2u:%.2u ", (unsigned long)tv.tv_usec, pm, (unsigned int)lt.tm_gmtoff/3600, (unsigned int)(lt.tm_gmtoff/60)%60);
+ time_t sec = tv.tv_sec;
+ if (localtime_r(&sec, &lt) != NULL) {
+ bool precise = false;
+
+#ifdef ENABLE_MICROSECONDS_LOG
+ precise = true;
+#endif /* ENABLE_MICROSECONDS_LOG */
+
+ tlen = strftime(tstr, sizeof(tstr),
+ "%Y-%m-%dT%H:%M:%S ", &lt);
+
+ if (precise && tlen > 0) {
+ char pm = (lt.tm_gmtoff > 0) ? '+' : '-';
+ snprintf(tstr + tlen - 1, sizeof(tstr) - tlen + 1,
+ ".%.6lu%c%.2u:%.2u ",
+ (unsigned long)tv.tv_usec, pm,
+ (unsigned int)lt.tm_gmtoff / 3600,
+ (unsigned int)(lt.tm_gmtoff / 60) % 60);
}
}
@@ -273,7 +284,7 @@ int log_msg(logsrc_t src, int level, const char *msg, ...)
case LOG_FATAL: prefix = "[fatal] "; break;
default: break;
}
-
+
/* Prepend prefix. */
int plen = strlen(prefix);
if (plen > buflen) {
@@ -323,9 +334,12 @@ void hex_log(int source, const char *data, int length)
log_msg(source, LOG_DEBUG, "%s\n", lbuf);
llen = 0;
}
- int n = sprintf(lbuf + llen, "0x%02x ",
- (unsigned char)*(data + ptr));
- llen += n;
+ int ret = snprintf(lbuf + llen, sizeof(lbuf) - llen, "0x%02x ",
+ (unsigned char)*(data + ptr));
+ if (ret < 0 || ret >= sizeof(lbuf) - llen) {
+ return;
+ }
+ llen += ret;
}
if (llen > 0) {
log_msg(source, LOG_DEBUG, "%s\n", lbuf);
@@ -338,7 +352,7 @@ int log_update_privileges(int uid, int gid)
if (fchown(fileno(LOG_FDS[i]), uid, gid) < 0) {
return KNOT_ERROR;
}
-
+
}
return KNOT_EOK;
}