Skip to content

Commit 79ecfe6

Browse files
committed
Fix GitHub Issue #6: Add option to use a microsecond timestamp
1 parent c2a2b27 commit 79ecfe6

File tree

5 files changed

+99
-34
lines changed

5 files changed

+99
-34
lines changed

CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
1.3.0
2+
=====
3+
4+
New features:
5+
6+
* Fix GitHub Issue #6: Add option to use a microsecond timestamp
7+
- More generic setTimestampMode() replaces enableDateTime()
8+
19
1.2.0 - "Thread-safe Taco"
210
==========================
311

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Looking for a simple logger for your C++ project? `SimpleLogger` might be for yo
77

88
* Based on RAII
99
* Configurable level symbols
10-
* Date / time
10+
* Datetime / EPOCH timestamps
1111
* Logging levels: `Trace`, `Debug`, `Info`, `Warning`, `Error`, `Fatal`
1212
* Log to file and/or console
1313
* Thread-safe
@@ -46,7 +46,7 @@ L().info() << "Something happened";
4646

4747
Outputs something like this:
4848

49-
`[Sat Oct 13 22:38:42 2018] I: Something happened`
49+
`Sat Oct 13 22:38:42 2018 I: Something happened`
5050

5151
## Log to file and console
5252

@@ -69,7 +69,7 @@ L::enableEchoMode(false);
6969
L().info() << "Something happened";
7070
```
7171

72-
## Set logging level
72+
## Set logging level
7373

7474
```
7575
using juzzlin::L;
@@ -82,9 +82,9 @@ L().debug() << "A debug thing happened";
8282

8383
Outputs something like this:
8484

85-
`[Sat Oct 13 22:38:42 2018] I: Something happened`
85+
`Sat Oct 13 22:38:42 2018 I: Something happened`
8686

87-
`[Sat Oct 13 22:38:42 2018] D: A debug thing happened`
87+
`Sat Oct 13 22:38:42 2018 D: A debug thing happened`
8888

8989
## Set custom level symbols
9090

@@ -99,7 +99,23 @@ L().debug() << "A debug thing happened";
9999

100100
Outputs something like this:
101101

102-
`[Sat Oct 13 22:38:42 2018] <DEBUG> A debug thing happened`
102+
`Sat Oct 13 22:38:42 2018 <DEBUG> A debug thing happened`
103+
104+
## Set timestamp mode and optional custom separator
105+
106+
Possible modes: `None`, `EpochSeconds`, `EpochMilliseconds`, `EpochMicroseconds`, `DateTime`.
107+
108+
```
109+
using juzzlin::L;
110+
111+
L::setTimestampMode(L::TimestampMode::EpochMilliseconds, " ## ");
112+
113+
L().info() << "Something happened";
114+
```
115+
116+
Outputs something like this:
117+
118+
`1562955750677 ## I: Something happened`
103119

104120
# Requirements
105121

src/simple_logger.cpp

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7676
private:
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

99101
bool Logger::Impl::m_echoMode = true;
100102

101-
bool Logger::Impl::m_dateTime = true;
102-
103103
Logger::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+
105109
std::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-
151150
std::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-
275299
void 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+
285314
std::ostringstream & Logger::trace()
286315
{
287316
return m_impl->trace();

src/simple_logger.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ class Logger
5757
Fatal
5858
};
5959

60+
enum class TimestampMode
61+
{
62+
None = 0,
63+
DateTime,
64+
EpochSeconds,
65+
EpochMilliseconds,
66+
EpochMicroseconds
67+
};
68+
6069
//! Constructor.
6170
Logger();
6271

@@ -73,10 +82,6 @@ class Logger
7382
//! \param enable Echo everything if true. Default is false.
7483
static void enableEchoMode(bool enable);
7584

76-
//! Enable/disable date and time prefix.
77-
//! \param enable Prefix with date and time if true. Default is true.
78-
static void enableDateTime(bool enable);
79-
8085
//! Set the logging level.
8186
//! \param level The minimum level. Default is Info.
8287
static void setLoggingLevel(Level level);
@@ -86,6 +91,11 @@ class Logger
8691
//! \param symbol The symbol outputted for the messages of this level.
8792
static void setLevelSymbol(Level level, std::string symbol);
8893

94+
//! Set/enable timestamp mode.
95+
//! \param timestampMode Timestamp mode enumeration.
96+
//! \param separator Separator string outputted after timestamp.
97+
static void setTimestampMode(TimestampMode timestampMode, std::string separator = " ");
98+
8999
//! Get stream to the trace log message.
90100
std::ostringstream & trace();
91101

src/tests/hello_world/hello_world.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ int main(int, char **)
4141

4242
L::init(logFile);
4343
L::enableEchoMode(true);
44-
L::enableDateTime(true);
4544
L::setLoggingLevel(L::Level::Trace);
4645

47-
std::string message = "Hello, world!";
46+
const std::string message = "Hello, world!";
47+
const std::string timestampSeparator = " ## ";
48+
L::setTimestampMode(L::TimestampMode::DateTime, timestampSeparator);
4849

4950
L().trace() << message;
5051
L().debug() << message;
@@ -61,6 +62,7 @@ int main(int, char **)
6162
while (std::getline(fin, line))
6263
{
6364
assert(line.find(message) != std::string::npos);
65+
assert(line.find(timestampSeparator) != std::string::npos);
6466
lines++;
6567
}
6668

0 commit comments

Comments
 (0)