Skip to content

Commit 41fa158

Browse files
authored
Add a method to the Logger class to enable local time displaying (#240)
1 parent ae4fa2e commit 41fa158

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

trantor/utils/Logger.cc

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,51 @@ void Logger::formatTime()
8888
if (now != lastSecond_)
8989
{
9090
lastSecond_ = now;
91+
if (displayLocalTime_())
92+
{
9193
#ifndef _MSC_VER
92-
strncpy(lastTimeString_,
93-
date_.toFormattedString(false).c_str(),
94+
strncpy(lastTimeString_,
95+
date_.toFormattedStringLocal(false).c_str(),
96+
sizeof(lastTimeString_) - 1);
97+
#else
98+
strncpy_s<sizeof lastTimeString_>(
99+
lastTimeString_,
100+
date_.toFormattedStringLocal(false).c_str(),
94101
sizeof(lastTimeString_) - 1);
102+
#endif
103+
}
104+
else
105+
{
106+
#ifndef _MSC_VER
107+
strncpy(lastTimeString_,
108+
date_.toFormattedString(false).c_str(),
109+
sizeof(lastTimeString_) - 1);
95110
#else
96-
strncpy_s<sizeof lastTimeString_>(
97-
lastTimeString_,
98-
date_.toFormattedString(false).c_str(),
99-
sizeof(lastTimeString_) - 1);
111+
strncpy_s<sizeof lastTimeString_>(
112+
lastTimeString_,
113+
date_.toFormattedString(false).c_str(),
114+
sizeof(lastTimeString_) - 1);
100115
#endif
116+
}
101117
}
102118
logStream_ << T(lastTimeString_, 17);
103119
char tmp[32];
104-
snprintf(tmp,
105-
sizeof(tmp),
106-
".%06llu UTC ",
107-
static_cast<long long unsigned int>(microSec));
108-
logStream_ << T(tmp, 12);
120+
if (displayLocalTime_())
121+
{
122+
snprintf(tmp,
123+
sizeof(tmp),
124+
".%06llu ",
125+
static_cast<long long unsigned int>(microSec));
126+
logStream_ << T(tmp, 8);
127+
}
128+
else
129+
{
130+
snprintf(tmp,
131+
sizeof(tmp),
132+
".%06llu UTC ",
133+
static_cast<long long unsigned int>(microSec));
134+
logStream_ << T(tmp, 12);
135+
}
109136
#ifdef __linux__
110137
if (threadId_ == 0)
111138
threadId_ = static_cast<pid_t>(::syscall(SYS_gettid));

trantor/utils/Logger.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ class TRANTOR_EXPORT Logger : public NonCopyable
141141
return logLevel_();
142142
}
143143

144+
/**
145+
* @brief Check whether it shows local time or UTC time.
146+
*/
147+
static bool displayLocalTime()
148+
{
149+
return displayLocalTime_();
150+
}
151+
152+
/**
153+
* @brief Set whether it shows local time or UTC time. the default is UTC.
154+
*/
155+
static void setDisplayLocalTime(bool showLocalTime)
156+
{
157+
displayLocalTime_() = showLocalTime;
158+
}
159+
144160
protected:
145161
static void defaultOutputFunction(const char *msg, const uint64_t len)
146162
{
@@ -151,6 +167,12 @@ class TRANTOR_EXPORT Logger : public NonCopyable
151167
fflush(stdout);
152168
}
153169
void formatTime();
170+
static bool &displayLocalTime_()
171+
{
172+
static bool showLocalTime = false;
173+
return showLocalTime;
174+
}
175+
154176
static LogLevel &logLevel_()
155177
{
156178
#ifdef RELEASE

0 commit comments

Comments
 (0)