@@ -59,28 +59,30 @@ class Logger::Impl
5959
6060 static void enableEchoMode (bool enable);
6161
62- static void enableDateTime (bool enable);
63-
6462 static void setLevelSymbol (Logger::Level level, std::string symbol);
6563
6664 static void setLoggingLevel (Logger::Level level);
6765
66+ static void setTimestampMode (Logger::TimestampMode timestampMode, std::string separator);
67+
6868 static void init (std::string filename, bool append);
6969
7070 void flush ();
7171
7272 std::ostringstream & getStream (Logger::Level level);
7373
74- void prefixDateTime ();
74+ void prefixTimestamp ();
7575
7676private:
7777
7878 static bool m_echoMode;
7979
80- static bool m_dateTime;
81-
8280 static Logger::Level m_level;
8381
82+ static Logger::TimestampMode m_timestampMode;
83+
84+ static std::string m_timestampSeparator;
85+
8486 static std::ofstream m_fout;
8587
8688 using SymbolMap = std::map<Logger::Level, std::string>;
@@ -98,10 +100,12 @@ class Logger::Impl
98100
99101bool Logger::Impl::m_echoMode = true ;
100102
101- bool Logger::Impl::m_dateTime = true ;
102-
103103Logger::Level Logger::Impl::m_level = Logger::Level::Info;
104104
105+ Logger::TimestampMode Logger::Impl::m_timestampMode = Logger::TimestampMode::DateTime;
106+
107+ std::string Logger::Impl::m_timestampSeparator = " : " ;
108+
105109std::ofstream Logger::Impl::m_fout;
106110
107111// Default level symbols
@@ -143,15 +147,10 @@ void Logger::Impl::enableEchoMode(bool enable)
143147 Impl::m_echoMode = enable;
144148}
145149
146- void Logger::Impl::enableDateTime (bool enable)
147- {
148- Impl::m_dateTime = enable;
149- }
150-
151150std::ostringstream & Logger::Impl::getStream (Logger::Level level)
152151{
153152 m_activeLevel = level;
154- Impl::prefixDateTime ();
153+ Impl::prefixTimestamp ();
155154 m_oss << Impl::m_symbols[level] << " " ;
156155 return m_oss;
157156}
@@ -166,15 +165,45 @@ void Logger::Impl::setLoggingLevel(Logger::Level level)
166165 Impl::m_level = level;
167166}
168167
169- void Logger::Impl::prefixDateTime ()
168+ void Logger::Impl::setTimestampMode (TimestampMode timestampMode, std::string separator)
169+ {
170+ Impl::m_timestampMode = timestampMode;
171+ Impl::m_timestampSeparator = separator;
172+ }
173+
174+ void Logger::Impl::prefixTimestamp ()
170175{
171- if (Impl::m_dateTime)
176+ std::string timeStr;
177+
178+ using std::chrono::duration_cast;
179+ using std::chrono::system_clock;
180+
181+ switch (Impl::m_timestampMode)
182+ {
183+ case Logger::TimestampMode::None:
184+ break ;
185+ case Logger::TimestampMode::DateTime:
172186 {
173187 time_t rawTime;
174188 time (&rawTime);
175- std::string timeStr ( ctime (&rawTime) );
189+ timeStr = ctime (&rawTime);
176190 timeStr.erase (timeStr.length () - 1 );
177- m_oss << " [" << timeStr << " ] " ;
191+ }
192+ break ;
193+ case Logger::TimestampMode::EpochSeconds:
194+ timeStr = std::to_string (duration_cast<std::chrono::seconds>(system_clock::now ().time_since_epoch ()).count ());
195+ break ;
196+ case Logger::TimestampMode::EpochMilliseconds:
197+ timeStr = std::to_string (duration_cast<std::chrono::milliseconds>(system_clock::now ().time_since_epoch ()).count ());
198+ break ;
199+ case Logger::TimestampMode::EpochMicroseconds:
200+ timeStr = std::to_string (duration_cast<std::chrono::microseconds>(system_clock::now ().time_since_epoch ()).count ());
201+ break ;
202+ }
203+
204+ if (!timeStr.empty ())
205+ {
206+ m_oss << timeStr << m_timestampSeparator;
178207 }
179208}
180209
@@ -267,11 +296,6 @@ void Logger::enableEchoMode(bool enable)
267296 Impl::enableEchoMode (enable);
268297}
269298
270- void Logger::enableDateTime (bool enable)
271- {
272- Impl::enableDateTime (enable);
273- }
274-
275299void Logger::setLoggingLevel (Level level)
276300{
277301 Impl::setLoggingLevel (level);
@@ -282,6 +306,11 @@ void Logger::setLevelSymbol(Level level, std::string symbol)
282306 Impl::setLevelSymbol (level, symbol);
283307}
284308
309+ void Logger::setTimestampMode (TimestampMode timestampMode, std::string separator)
310+ {
311+ Impl::setTimestampMode (timestampMode, separator);
312+ }
313+
285314std::ostringstream & Logger::trace ()
286315{
287316 return m_impl->trace ();
0 commit comments