Skip to content
Snippets Groups Projects
Commit e6885654 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

logger: use {fmt} for header generation

Change-Id: I3ee8a5051e8aace1737d40364bd5defae044636c
parent a990260c
Branches
No related tags found
No related merge requests found
......@@ -27,6 +27,11 @@
#include "client/ring_signal.h"
#include <fmt/core.h>
#include <fmt/format.h>
#include <fmt/std.h>
#include <fmt/compile.h>
#ifdef _MSC_VER
#include <sys_time.h>
#else
......@@ -38,8 +43,6 @@
#include <functional>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <ios>
#include <mutex>
#include <thread>
......@@ -137,44 +140,14 @@ stripDirName(const char* path)
static std::string
contextHeader(const char* const file, int line)
{
static char* timestamp_fmt = getenv("JAMI_TIMESTAMP_FMT");
#ifdef __linux__
auto tid = syscall(__NR_gettid) & 0xffff;
#else
auto tid = std::this_thread::get_id();
#endif // __linux__
std::ostringstream out;
out << '[';
// Timestamp
if (timestamp_fmt) {
time_t t;
struct tm tm;
char buf[128];
time(&t);
#ifdef _WIN32
/* NOTE! localtime(3) is MT-Safe on win32 */
tm = *localtime(&t);
#else
localtime_r(&t, &tm);
#endif
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
strftime(buf, sizeof(buf), timestamp_fmt, &tm);
#pragma GCC diagnostic pop
out << buf;
} else {
unsigned int secs, milli;
struct timeval tv;
if (!gettimeofday(&tv, NULL)) {
secs = tv.tv_sec;
milli = tv.tv_usec / 1000; // suppose that milli < 1000
......@@ -183,27 +156,11 @@ contextHeader(const char* const file, int line)
milli = 0;
}
const auto prev_fill = out.fill();
out << secs << '.' << std::right << std::setw(3) << std::setfill('0') << milli << std::left;
out.fill(prev_fill);
}
out << '|' << std::right << std::setw(5) << std::setfill(' ') << tid << std::left;
// Context
if (file) {
#ifdef RING_UWP
constexpr auto width = 26;
#else
constexpr auto width = 18;
#endif
out << "|" << std::setw(width) << stripDirName(file) << ":" << std::setw(5)
<< std::setfill(' ') << line;
return fmt::format(FMT_COMPILE("[{: >3d}.{:0<3d}|{: >4}|{: <18s}:{: <4d}]"), secs, milli, tid, stripDirName(file), line);
} else {
return fmt::format(FMT_COMPILE("[{: >3d}.{:0<3d}|{: >4}]"), secs, milli, tid);
}
out << "] ";
return out.str();
}
struct BufDeleter
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment