Support tagged messages.

This changes the design of the LogBackend class and its subclasses,
now the main virtual method is addTaggedMessage(). The former virtual
method addMessage() is now a regular non-virtual method forwarding to
addTaggedMessage().

You can configure message limiting based on tags by passing a MessageLimiter
to the configureMessageLimiter() method, which will then be used by the
includeMessage() method. That method now takes an additional tag argument.

The most user-visible part of this is that there are new overloads of the
static methods OpmLog::warning(), OpmLog::error() etc, that take message
tags. To tie things together the OpmLog and Logger classes have also gotten
new addTaggedMessage() methods, but they should mostly be used through the
convenience methods such as OpmLog::warning().
This commit is contained in:
Atgeirr Flø Rasmussen
2016-05-18 13:27:12 +02:00
parent 9c84967734
commit d2564ff838
15 changed files with 185 additions and 22 deletions

View File

@@ -118,7 +118,7 @@ public:
m_specialMessages = 0;
}
void addMessage(int64_t messageType , const std::string& /* message */) {
void addTaggedMessage(int64_t messageType , const std::string& /* messageTag */, const std::string& /* message */) {
if (messageType & Log::DefaultMessageTypes)
m_defaultMessages +=1;
else
@@ -303,3 +303,59 @@ BOOST_AUTO_TEST_CASE(TestOpmLogWithColors)
std::cout << log_stream.str() << std::endl;
}
BOOST_AUTO_TEST_CASE(TestOpmLogWithLimits)
{
OpmLog::removeAllBackends();
std::ostringstream log_stream1;
std::ostringstream log_stream2;
{
std::shared_ptr<StreamLog> streamLog1 = std::make_shared<StreamLog>(log_stream1, Log::DefaultMessageTypes);
std::shared_ptr<StreamLog> streamLog2 = std::make_shared<StreamLog>(log_stream2, Log::DefaultMessageTypes);
OpmLog::addBackend("STREAM1" , streamLog1);
OpmLog::addBackend("STREAM2" , streamLog2);
BOOST_CHECK_EQUAL( true , OpmLog::hasBackend("STREAM1"));
BOOST_CHECK_EQUAL( true , OpmLog::hasBackend("STREAM2"));
streamLog1->configureDecoration(std::make_shared<SimpleMessageFormatter>(false, true));
streamLog1->configureMessageLimiter(std::make_shared<MessageLimiter>(2));
streamLog2->configureDecoration(std::make_shared<SimpleMessageFormatter>(false, true));
streamLog2->configureMessageLimiter(std::make_shared<MessageLimiter>()); // no limit
}
const std::string tag = "ExampleTag";
OpmLog::warning(tag, "Warning");
OpmLog::error("Error");
OpmLog::info("Info");
OpmLog::bug("Bug");
OpmLog::warning(tag, "Warning");
OpmLog::warning(tag, "Warning");
OpmLog::warning(tag, "Warning");
const std::string expected1 = Log::colorCodeMessage(Log::MessageType::Warning, "Warning") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Error, "Error") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Info, "Info") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Bug, "Bug") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Warning, "Warning") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Warning, "Message limit reached for message tag: " + tag) + "\n";
BOOST_CHECK_EQUAL(log_stream1.str(), expected1);
const std::string expected2 = Log::colorCodeMessage(Log::MessageType::Warning, "Warning") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Error, "Error") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Info, "Info") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Bug, "Bug") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Warning, "Warning") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Warning, "Warning") + "\n"
+ Log::colorCodeMessage(Log::MessageType::Warning, "Warning") + "\n";
BOOST_CHECK_EQUAL(log_stream2.str(), expected2);
std::cout << log_stream1.str() << std::endl;
std::cout << log_stream2.str() << std::endl;
}