Skip to content

Commit 3ba3c8d

Browse files
committed
Add support for tags
1 parent 789e594 commit 3ba3c8d

File tree

4 files changed

+136
-8
lines changed

4 files changed

+136
-8
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ Outputs something like this:
111111

112112
`Sat Oct 13 22:38:42 2018 D: A debug thing happened`
113113

114+
## Log with a tag
115+
116+
```
117+
using juzzlin::L;
118+
119+
L::setLoggingLevel(L::Level::Info);
120+
121+
L("MyTag").info() << "Something happened";
122+
```
123+
124+
Outputs something like this:
125+
126+
`Sat Oct 13 22:38:42 2018 I: MyTag: Something happened`
127+
114128
## Set custom level symbols
115129

116130
```

src/simple_logger.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ namespace juzzlin {
3838
class SimpleLogger::Impl
3939
{
4040
public:
41-
4241
Impl();
4342

43+
Impl(const std::string & tag);
44+
4445
~Impl();
4546

4647
std::ostringstream & traceStream();
@@ -82,6 +83,8 @@ class SimpleLogger::Impl
8283

8384
void flushEchoIfEnabled();
8485

86+
void prefixWithLevelAndTag(SimpleLogger::Level level);
87+
8588
void prefixWithTimestamp();
8689

8790
bool shouldFlush() const;
@@ -110,6 +113,8 @@ class SimpleLogger::Impl
110113

111114
std::lock_guard<std::recursive_mutex> m_lock;
112115

116+
std::string m_tag;
117+
113118
std::ostringstream m_message;
114119
};
115120

@@ -152,6 +157,12 @@ SimpleLogger::Impl::Impl()
152157
{
153158
}
154159

160+
SimpleLogger::Impl::Impl(const std::string & tag)
161+
: m_lock(m_mutex)
162+
, m_tag(tag)
163+
{
164+
}
165+
155166
SimpleLogger::Impl::~Impl()
156167
{
157168
flush();
@@ -166,7 +177,7 @@ std::ostringstream & SimpleLogger::Impl::prepareStreamForLoggingLevel(SimpleLogg
166177
{
167178
m_activeLevel = level;
168179
prefixWithTimestamp();
169-
m_message << m_symbols[level] << " ";
180+
prefixWithLevelAndTag(level);
170181
return m_message;
171182
}
172183

@@ -204,6 +215,11 @@ std::string SimpleLogger::Impl::currentDateTime(std::chrono::time_point<std::chr
204215
return oss.str();
205216
}
206217

218+
void SimpleLogger::Impl::prefixWithLevelAndTag(SimpleLogger::Level level)
219+
{
220+
m_message << m_symbols[level] << (!m_tag.empty() ? " " + m_tag + ":" : "") << " ";
221+
}
222+
207223
void SimpleLogger::Impl::prefixWithTimestamp()
208224
{
209225
std::string timestamp;
@@ -322,6 +338,11 @@ SimpleLogger::SimpleLogger()
322338
{
323339
}
324340

341+
SimpleLogger::SimpleLogger(const std::string & tag)
342+
: m_impl(std::make_unique<SimpleLogger::Impl>(tag))
343+
{
344+
}
345+
325346
void SimpleLogger::init(std::string filename, bool append)
326347
{
327348
Impl::init(filename, append);

src/simple_logger.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class SimpleLogger
7070
//! Constructor.
7171
SimpleLogger();
7272

73+
//! Constructor.
74+
//! \param tag Tag that will be added to the message.
75+
SimpleLogger(const std::string & tag);
76+
7377
//! Destructor.
7478
~SimpleLogger();
7579

src/tests/stream_test/stream_test.cpp

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,37 @@
3131

3232
#include <cassert>
3333
#include <cstdlib>
34+
#include <iostream>
3435
#include <regex>
3536
#include <sstream>
37+
#include <stdexcept>
3638

3739
namespace juzzlin::StreamTest {
3840

41+
void assertString(std::stringstream & stream, const std::string & message)
42+
{
43+
if (stream.str().find(message) == std::string::npos) {
44+
throw std::runtime_error("ERROR!!: '" + message + "' not found in '" + stream.str() + "'");
45+
}
46+
}
47+
3948
void assertMessage(std::stringstream & stream, const std::string & message, const std::string & timestampSeparator)
4049
{
41-
assert(stream.str().find(message) != std::string::npos);
42-
assert(stream.str().find(timestampSeparator) != std::string::npos);
50+
assertString(stream, message);
51+
assertString(stream, timestampSeparator);
52+
}
53+
54+
void assertNotString(std::stringstream & stream, const std::string & message)
55+
{
56+
if (stream.str().find(message) != std::string::npos) {
57+
throw std::runtime_error("ERROR!!: '" + message + "' was found in '" + stream.str() + "'");
58+
}
4359
}
4460

4561
void assertNotMessage(std::stringstream & stream, const std::string & message, const std::string & timestampSeparator)
4662
{
47-
assert(stream.str().find(message) == std::string::npos);
48-
assert(stream.str().find(timestampSeparator) == std::string::npos);
63+
assertNotString(stream, message);
64+
assertNotString(stream, timestampSeparator);
4965
}
5066

5167
void testFatal_noneLoggingLevel_shouldNotPrintMessage(const std::string & message, const std::string & timestampSeparator)
@@ -156,6 +172,66 @@ void testTrace_traceLoggingLevel_shouldPrintMessage(const std::string & message,
156172
assertMessage(ss, message, timestampSeparator);
157173
}
158174

175+
void testTag_fatalLevel_shouldPrintTag(const std::string & message, const std::string & timestampSeparator)
176+
{
177+
std::stringstream ss;
178+
L::setStream(L::Level::Fatal, ss);
179+
L::setLoggingLevel(L::Level::Fatal);
180+
const std::string tag = "TAG";
181+
L(tag).fatal() << message;
182+
assertMessage(ss, tag + ": " + message, timestampSeparator);
183+
}
184+
185+
void testTag_errorLevel_shouldPrintTag(const std::string & message, const std::string & timestampSeparator)
186+
{
187+
std::stringstream ss;
188+
L::setStream(L::Level::Error, ss);
189+
L::setLoggingLevel(L::Level::Error);
190+
const std::string tag = "TAG";
191+
L(tag).fatal() << message;
192+
assertMessage(ss, tag + ": " + message, timestampSeparator);
193+
}
194+
195+
void testTag_warningLevel_shouldPrintTag(const std::string & message, const std::string & timestampSeparator)
196+
{
197+
std::stringstream ss;
198+
L::setStream(L::Level::Warning, ss);
199+
L::setLoggingLevel(L::Level::Warning);
200+
const std::string tag = "TAG";
201+
L(tag).fatal() << message;
202+
assertMessage(ss, tag + ": " + message, timestampSeparator);
203+
}
204+
205+
void testTag_infoLevel_shouldPrintTag(const std::string & message, const std::string & timestampSeparator)
206+
{
207+
std::stringstream ss;
208+
L::setStream(L::Level::Info, ss);
209+
L::setLoggingLevel(L::Level::Info);
210+
const std::string tag = "TAG";
211+
L(tag).fatal() << message;
212+
assertMessage(ss, tag + ": " + message, timestampSeparator);
213+
}
214+
215+
void testTag_debugLevel_shouldPrintTag(const std::string & message, const std::string & timestampSeparator)
216+
{
217+
std::stringstream ss;
218+
L::setStream(L::Level::Debug, ss);
219+
L::setLoggingLevel(L::Level::Debug);
220+
const std::string tag = "TAG";
221+
L(tag).fatal() << message;
222+
assertMessage(ss, tag + ": " + message, timestampSeparator);
223+
}
224+
225+
void testTag_traceLevel_shouldPrintTag(const std::string & message, const std::string & timestampSeparator)
226+
{
227+
std::stringstream ss;
228+
L::setStream(L::Level::Trace, ss);
229+
L::setLoggingLevel(L::Level::Trace);
230+
const std::string tag = "TAG";
231+
L(tag).fatal() << message;
232+
assertMessage(ss, tag + ": " + message, timestampSeparator);
233+
}
234+
159235
void initializeLogger(const std::string & timestampSeparator)
160236
{
161237
L::enableEchoMode(true);
@@ -168,9 +244,10 @@ void testTimestampMode_none_shouldNotPrintTimestamp(const std::string & message)
168244
L::setTimestampMode(L::TimestampMode::None);
169245
std::stringstream ss;
170246
L::setStream(L::Level::Info, ss);
247+
L::setLoggingLevel(L::Level::Info);
171248
L().info() << message;
172-
assert(ss.str().find(message) != std::string::npos);
173-
assert(ss.str().find(" ## ") == std::string::npos);
249+
assertString(ss, message);
250+
assertNotString(ss, "##");
174251
}
175252

176253
void testTimestampMode_dateTime_shouldPrintDateTimeTimestamp(const std::string & message)
@@ -284,6 +361,18 @@ void runTests()
284361

285362
testTrace_traceLoggingLevel_shouldPrintMessage(message, timestampSeparator);
286363

364+
testTag_fatalLevel_shouldPrintTag(message, timestampSeparator);
365+
366+
testTag_errorLevel_shouldPrintTag(message, timestampSeparator);
367+
368+
testTag_warningLevel_shouldPrintTag(message, timestampSeparator);
369+
370+
testTag_infoLevel_shouldPrintTag(message, timestampSeparator);
371+
372+
testTag_debugLevel_shouldPrintTag(message, timestampSeparator);
373+
374+
testTag_traceLevel_shouldPrintTag(message, timestampSeparator);
375+
287376
testTimestampMode_none_shouldNotPrintTimestamp(message);
288377

289378
testTimestampMode_dateTime_shouldPrintDateTimeTimestamp(message);

0 commit comments

Comments
 (0)