added datetime stamp to prefix

This commit is contained in:
Bob Polis 2020-04-29 17:41:23 +02:00
parent 0173d1fe10
commit fb520c2260

View File

@ -8,6 +8,10 @@
#include "logger.hpp"
#include <unistd.h>
#include <sys/time.h>
#include <ctime>
#include <cmath>
#include <iomanip>
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<int>(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) << "] ";