Skip to content

Commit

Permalink
logger: Add custom timestamp format
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Olivier Dion committed Dec 14, 2021
1 parent 1bb4739 commit 800dbab
Showing 1 changed file with 45 additions and 13 deletions.
58 changes: 45 additions & 13 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__

std::ostringstream out;

out << '[';

// Timestamp
unsigned int secs, milli;
struct timeval tv;
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;

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);
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) {
Expand Down

0 comments on commit 800dbab

Please sign in to comment.