mirror of
https://git.jami.net/savoirfairelinux/jami-daemon.git
synced 2025-08-12 22:09:25 +08:00
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:
@ -103,12 +103,43 @@ 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;
|
||||
|
||||
@ -120,11 +151,12 @@ contextHeader(const char* const file, int line)
|
||||
milli = 0;
|
||||
}
|
||||
|
||||
std::ostringstream out;
|
||||
const auto prev_fill = out.fill();
|
||||
out << '[' << secs << '.' << std::right << std::setw(3) << std::setfill('0') << milli
|
||||
|
||||
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) {
|
||||
|
Reference in New Issue
Block a user