From fb520c22601a4e02697f19638f175047fddea7c6 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Wed, 29 Apr 2020 17:41:23 +0200 Subject: [PATCH] added datetime stamp to prefix --- logger.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/logger.cpp b/logger.cpp index 2494c2c..31a78c3 100644 --- a/logger.cpp +++ b/logger.cpp @@ -8,6 +8,10 @@ #include "logger.hpp" #include +#include +#include +#include +#include using namespace sc; @@ -26,6 +30,16 @@ void logger::msg(loglevel level, const char* file, int line, const char* func) { } void logger::write_prefix(loglevel level, const char* file, int line, const char* func) { + // retrieve accurate time for milliseconds display + struct timeval secs; + (void)::gettimeofday(&secs, nullptr); + int ms = static_cast(std::roundf(secs.tv_usec / 1000.0)) % 1000; + + // create a date/time stamp + char dtstamp[20]; + std::strftime(dtstamp, 20, "%F %T", std::localtime(&secs.tv_sec)); + + std::cerr << dtstamp << "." << std::setw(3) << ms << " "; std::cerr << _name << "[" << ::getpid() << "] "; std::cerr << file << ":" << line << " " << func << "() "; std::cerr << '[' << loglevel_desc(level) << "] ";