Skip to content

Commit 789e594

Browse files
committed
Refactor simple_logger.cpp
1 parent a5c8ff7 commit 789e594

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/simple_logger.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ class SimpleLogger::Impl
7676
std::ostringstream & prepareStreamForLoggingLevel(SimpleLogger::Level level);
7777

7878
private:
79-
std::string currentDateTime(const std::string & dateTimeFormat) const;
79+
std::string currentDateTime(std::chrono::time_point<std::chrono::system_clock> now, const std::string & dateTimeFormat) const;
8080

8181
void flushFileIfOpen();
8282

8383
void flushEchoIfEnabled();
8484

85-
void prefixTimestamp();
85+
void prefixWithTimestamp();
8686

8787
bool shouldFlush() const;
8888

@@ -165,7 +165,7 @@ void SimpleLogger::Impl::enableEchoMode(bool enable)
165165
std::ostringstream & SimpleLogger::Impl::prepareStreamForLoggingLevel(SimpleLogger::Level level)
166166
{
167167
m_activeLevel = level;
168-
prefixTimestamp();
168+
prefixWithTimestamp();
169169
m_message << m_symbols[level] << " ";
170170
return m_message;
171171
}
@@ -195,50 +195,49 @@ void SimpleLogger::Impl::setTimestampSeparator(std::string separator)
195195
m_timestampSeparator = separator;
196196
}
197197

198-
std::string SimpleLogger::Impl::currentDateTime(const std::string & dateTimeFormat) const
198+
std::string SimpleLogger::Impl::currentDateTime(std::chrono::time_point<std::chrono::system_clock> now, const std::string & dateTimeFormat) const
199199
{
200-
const auto now = std::chrono::system_clock::now();
201-
const auto rawTime = std::chrono::system_clock::to_time_t(now);
202-
const auto timeInfo = std::localtime(&rawTime);
203-
204200
std::ostringstream oss;
205-
oss << std::put_time(timeInfo, dateTimeFormat.c_str());
201+
const auto rawTime = std::chrono::system_clock::to_time_t(now);
202+
oss << std::put_time(std::localtime(&rawTime), dateTimeFormat.c_str());
206203

207204
return oss.str();
208205
}
209206

210-
void SimpleLogger::Impl::prefixTimestamp()
207+
void SimpleLogger::Impl::prefixWithTimestamp()
211208
{
212-
std::string timeStr;
209+
std::string timestamp;
213210

214-
const auto now = std::chrono::system_clock::now();
215211
using std::chrono::duration_cast;
212+
using std::chrono::system_clock;
216213

217214
switch (m_timestampMode) {
218215
case SimpleLogger::TimestampMode::None:
219216
break;
220217
case SimpleLogger::TimestampMode::DateTime: {
221-
timeStr = currentDateTime("%a %b %e %H:%M:%S %Y");
218+
timestamp = currentDateTime(system_clock::now(), "%a %b %e %H:%M:%S %Y");
222219
} break;
223220
case SimpleLogger::TimestampMode::ISODateTime: {
224-
timeStr = currentDateTime("%Y-%m-%dT%H:%M:%S");
221+
timestamp = currentDateTime(system_clock::now(), "%Y-%m-%dT%H:%M:%S");
225222
} break;
226223
case SimpleLogger::TimestampMode::EpochSeconds:
227-
timeStr = std::to_string(duration_cast<std::chrono::seconds>(now.time_since_epoch()).count());
224+
timestamp = std::to_string(duration_cast<std::chrono::seconds>(system_clock::now().time_since_epoch()).count());
228225
break;
229226
case SimpleLogger::TimestampMode::EpochMilliseconds:
230-
timeStr = std::to_string(duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
227+
using std::chrono::duration_cast;
228+
timestamp = std::to_string(duration_cast<std::chrono::milliseconds>(system_clock::now().time_since_epoch()).count());
231229
break;
232230
case SimpleLogger::TimestampMode::EpochMicroseconds:
233-
timeStr = std::to_string(duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count());
231+
using std::chrono::duration_cast;
232+
timestamp = std::to_string(duration_cast<std::chrono::microseconds>(system_clock::now().time_since_epoch()).count());
234233
break;
235234
case SimpleLogger::TimestampMode::Custom:
236-
timeStr = currentDateTime(m_customTimestampFormat);
235+
timestamp = currentDateTime(system_clock::now(), m_customTimestampFormat);
237236
break;
238237
}
239238

240-
if (!timeStr.empty()) {
241-
m_message << timeStr << m_timestampSeparator;
239+
if (!timestamp.empty()) {
240+
m_message << timestamp << m_timestampSeparator;
242241
}
243242
}
244243

0 commit comments

Comments
 (0)