logs: simplify timestr
authorRobert Swiecki <robert@swiecki.net>
Mon, 12 Feb 2018 02:29:25 +0000 (03:29 +0100)
committerRobert Swiecki <robert@swiecki.net>
Mon, 12 Feb 2018 02:29:25 +0000 (03:29 +0100)
Makefile
logs.cc

index 05084c09df075e17bfbebf44406478ed8fd85204..3d45ab7878cf3b8630f3b2c6ff78b100efa1335c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -99,7 +99,7 @@ config.o: mnt.h user.h util.h
 contain.o: contain.h nsjail.h logs.h caps.h cgroup.h cpu.h mnt.h net.h pid.h
 contain.o: user.h uts.h
 cpu.o: cpu.h nsjail.h logs.h util.h
-logs.o: logs.h nsjail.h
+logs.o: logs.h util.h nsjail.h
 mnt.o: mnt.h nsjail.h logs.h macros.h subproc.h util.h
 net.o: net.h nsjail.h logs.h subproc.h
 nsjail.o: nsjail.h logs.h cmdline.h macros.h net.h sandbox.h subproc.h util.h
diff --git a/logs.cc b/logs.cc
index 591dd1509ddd9982e8d21305fb6cbea42af3bf6e..06ffd8ee01c366295138524ddfbb2e0d6c7571d7 100644 (file)
--- a/logs.cc
+++ b/logs.cc
@@ -35,7 +35,7 @@
 #include <time.h>
 #include <unistd.h>
 
-#include "nsjail.h"
+#include "util.h"
 
 #include <string.h>
 
@@ -100,20 +100,13 @@ void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt
            {"HB", "\033[1m", false, false},
        };
 
-       time_t ltstamp = time(NULL);
-       struct tm utctime;
-       localtime_r(&ltstamp, &utctime);
-       char timestr[32];
-       if (strftime(timestr, sizeof(timestr) - 1, "%FT%T%z", &utctime) == 0) {
-               timestr[0] = '\0';
-       }
-
        /* Start printing logs */
        if (_log_fd_isatty) {
                dprintf(_log_fd, "%s", logLevels[ll].prefix);
        }
        if (logLevels[ll].print_time) {
-               dprintf(_log_fd, "[%s] ", timestr);
+               const auto timestr = util::timeToStr(time(NULL));
+               dprintf(_log_fd, "[%s] ", timestr.c_str());
        }
        if (logLevels[ll].print_funcline) {
                dprintf(_log_fd, "[%s][%d] %s():%d ", logLevels[ll].descr, (int)getpid(), fn, ln);