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
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,11 @@ ...@@ -27,6 +27,11 @@
#include "client/ring_signal.h" #include "client/ring_signal.h"
#include <fmt/core.h>
#include <fmt/format.h>
#include <fmt/std.h>
#include <fmt/compile.h>
#ifdef _MSC_VER #ifdef _MSC_VER
#include <sys_time.h> #include <sys_time.h>
#else #else
...@@ -38,8 +43,6 @@ ...@@ -38,8 +43,6 @@
#include <functional> #include <functional>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <sstream>
#include <iomanip>
#include <ios> #include <ios>
#include <mutex> #include <mutex>
#include <thread> #include <thread>
...@@ -137,44 +140,14 @@ stripDirName(const char* path) ...@@ -137,44 +140,14 @@ stripDirName(const char* path)
static std::string static std::string
contextHeader(const char* const file, int line) contextHeader(const char* const file, int line)
{ {
static char* timestamp_fmt = getenv("JAMI_TIMESTAMP_FMT");
#ifdef __linux__ #ifdef __linux__
auto tid = syscall(__NR_gettid) & 0xffff; auto tid = syscall(__NR_gettid) & 0xffff;
#else #else
auto tid = std::this_thread::get_id(); auto tid = std::this_thread::get_id();
#endif // __linux__ #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; unsigned int secs, milli;
struct timeval tv; struct timeval tv;
if (!gettimeofday(&tv, NULL)) { if (!gettimeofday(&tv, NULL)) {
secs = tv.tv_sec; secs = tv.tv_sec;
milli = tv.tv_usec / 1000; // suppose that milli < 1000 milli = tv.tv_usec / 1000; // suppose that milli < 1000
...@@ -183,27 +156,11 @@ contextHeader(const char* const file, int line) ...@@ -183,27 +156,11 @@ contextHeader(const char* const file, int line)
milli = 0; 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) { if (file) {
#ifdef RING_UWP return fmt::format(FMT_COMPILE("[{: >3d}.{:0<3d}|{: >4}|{: <18s}:{: <4d}]"), secs, milli, tid, stripDirName(file), line);
constexpr auto width = 26; } else {
#else return fmt::format(FMT_COMPILE("[{: >3d}.{:0<3d}|{: >4}]"), secs, milli, tid);
constexpr auto width = 18;
#endif
out << "|" << std::setw(width) << stripDirName(file) << ":" << std::setw(5)
<< std::setfill(' ') << line;
} }
out << "] ";
return out.str();
} }
struct BufDeleter struct BufDeleter
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment