logger: Add custom timestamp format

Using JAMI_TIMESTAMP_FMT, developers can now format the log's timestamp
according to their needs and taste.  The formatting is the same as strftime(3).

Change-Id: Ibea56852b2efc37f66aeeeda857e307130099720
This commit is contained in:
Olivier Dion
2021-11-01 21:05:24 -04:00
parent 1bb4739f59
commit 800dbaba99

View File

@ -103,28 +103,60 @@ 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__
// Timestamp
unsigned int secs, milli;
struct timeval tv;
if (!gettimeofday(&tv, NULL)) {
secs = tv.tv_sec;
milli = tv.tv_usec / 1000; // suppose that milli < 1000
} else {
secs = time(NULL);
milli = 0;
}
std::ostringstream out;
const auto prev_fill = out.fill();
out << '[' << secs << '.' << std::right << std::setw(3) << std::setfill('0') << milli
<< std::left << '|' << std::right << std::setw(5) << std::setfill(' ') << tid << std::left;
out.fill(prev_fill);
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
} else {
secs = time(NULL);
milli = 0;
}
const auto prev_fill = out.fill();
out << secs << '.' << std::right << std::setw(3) << std::setfill('0') << milli
<< std::left << '|' << std::right << std::setw(5) << std::setfill(' ') << tid << std::left;
out.fill(prev_fill);
}
// Context
if (file) {