gave start of prefix output identical implementation

This commit is contained in:
Bob Polis 2020-04-29 17:49:43 +02:00
parent fb520c2260
commit cfec8e9d3c
2 changed files with 19 additions and 12 deletions

View File

@ -16,6 +16,9 @@
#include <cstdio> #include <cstdio>
#include <cstdarg> #include <cstdarg>
#include <cstdlib> #include <cstdlib>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
@ -29,16 +32,20 @@ mutex m;
void print_timestamp() void print_timestamp()
{ {
// retrieve accurate time for milliseconds display
struct timeval secs; struct timeval secs;
(void)gettimeofday(&secs, NULL); (void)::gettimeofday(&secs, nullptr);
time_t now = time(NULL); int ms = static_cast<int>(std::roundf(secs.tv_usec / 1000.0)) % 1000;
char dtstamp[32];
strftime(dtstamp, 32, "%F %T", localtime(&now)); // create a date/time stamp
long millisecs = (long)((double)secs.tv_usec / 1000.0 + 0.5) % 1000; char dtstamp[20];
std::strftime(dtstamp, 20, "%F %T", std::localtime(&secs.tv_sec));
std::cerr << dtstamp << "." << std::setw(3) << ms << " ";
#if __APPLE__ #if __APPLE__
fprintf(stderr, "%s.%03ld %s[%ld] ", dtstamp, millisecs, getprogname(), (long)getpid()); std::cerr << getprogname() << "[" << getpid() << "] ";
#elif __linux__ #elif __linux__
fprintf(stderr, "%s.%03ld %s[%ld] ", dtstamp, millisecs, program_invocation_name, (long)getpid()); std::cerr << program_invocation_name << "[" << getpid() << "] ";
#endif #endif
} }

View File

@ -31,13 +31,13 @@ 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) { void logger::write_prefix(loglevel level, const char* file, int line, const char* func) {
// retrieve accurate time for milliseconds display // retrieve accurate time for milliseconds display
struct timeval secs; struct timeval secs;
(void)::gettimeofday(&secs, nullptr); (void)::gettimeofday(&secs, nullptr);
int ms = static_cast<int>(std::roundf(secs.tv_usec / 1000.0)) % 1000; int ms = static_cast<int>(std::roundf(secs.tv_usec / 1000.0)) % 1000;
// create a date/time stamp // create a date/time stamp
char dtstamp[20]; char dtstamp[20];
std::strftime(dtstamp, 20, "%F %T", std::localtime(&secs.tv_sec)); std::strftime(dtstamp, 20, "%F %T", std::localtime(&secs.tv_sec));
std::cerr << dtstamp << "." << std::setw(3) << ms << " "; std::cerr << dtstamp << "." << std::setw(3) << ms << " ";
std::cerr << _name << "[" << ::getpid() << "] "; std::cerr << _name << "[" << ::getpid() << "] ";