@@ -76,13 +76,13 @@ class SimpleLogger::Impl
76
76
std::ostringstream & prepareStreamForLoggingLevel (SimpleLogger::Level level);
77
77
78
78
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 ;
80
80
81
81
void flushFileIfOpen ();
82
82
83
83
void flushEchoIfEnabled ();
84
84
85
- void prefixTimestamp ();
85
+ void prefixWithTimestamp ();
86
86
87
87
bool shouldFlush () const ;
88
88
@@ -165,7 +165,7 @@ void SimpleLogger::Impl::enableEchoMode(bool enable)
165
165
std::ostringstream & SimpleLogger::Impl::prepareStreamForLoggingLevel (SimpleLogger::Level level)
166
166
{
167
167
m_activeLevel = level;
168
- prefixTimestamp ();
168
+ prefixWithTimestamp ();
169
169
m_message << m_symbols[level] << " " ;
170
170
return m_message;
171
171
}
@@ -195,50 +195,49 @@ void SimpleLogger::Impl::setTimestampSeparator(std::string separator)
195
195
m_timestampSeparator = separator;
196
196
}
197
197
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
199
199
{
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
-
204
200
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 ());
206
203
207
204
return oss.str ();
208
205
}
209
206
210
- void SimpleLogger::Impl::prefixTimestamp ()
207
+ void SimpleLogger::Impl::prefixWithTimestamp ()
211
208
{
212
- std::string timeStr ;
209
+ std::string timestamp ;
213
210
214
- const auto now = std::chrono::system_clock::now ();
215
211
using std::chrono::duration_cast;
212
+ using std::chrono::system_clock;
216
213
217
214
switch (m_timestampMode) {
218
215
case SimpleLogger::TimestampMode::None:
219
216
break ;
220
217
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" );
222
219
} break ;
223
220
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" );
225
222
} break ;
226
223
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 ());
228
225
break ;
229
226
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 ());
231
229
break ;
232
230
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 ());
234
233
break ;
235
234
case SimpleLogger::TimestampMode::Custom:
236
- timeStr = currentDateTime (m_customTimestampFormat);
235
+ timestamp = currentDateTime (system_clock::now (), m_customTimestampFormat);
237
236
break ;
238
237
}
239
238
240
- if (!timeStr .empty ()) {
241
- m_message << timeStr << m_timestampSeparator;
239
+ if (!timestamp .empty ()) {
240
+ m_message << timestamp << m_timestampSeparator;
242
241
}
243
242
}
244
243
0 commit comments