9#include <framework/utilities/testhelpers/Fixtures.h>
10#include <framework/logging/LogConnectionBase.h>
11#include <framework/logging/LogConnectionConsole.h>
13#include <boost/algorithm/string.hpp>
14#include <boost/range/adaptors.hpp>
21 std::vector<Belle2::LogMessage>& m_messages;
24 explicit LogInterceptor(std::vector<Belle2::LogMessage>& messages): m_messages(messages) {}
26 bool isConnected()
override {
return true; }
28 bool sendMessage(
Belle2::LogMessage const& message)
override {m_messages.push_back(message);
return false; }
50 return m.getLogLevel() == level;
53 <<
", found " << messages;
61 ASSERT_FALSE(
m_messages.empty()) <<
"No message available to check";
63 for (
const auto& var : msg.getLogVariables()) {
64 if (
auto it = variables.find(var.getName()); it != variables.end()) {
65 EXPECT_EQ(it->second, var.getValue());
68 EXPECT_FALSE(strict) <<
"extra variable: " << var.getName() << std::endl;
71 EXPECT_TRUE(variables.empty()) <<
"Some requested log variables were not present in the log message: "
72 << boost::algorithm::join(variables | boost::adaptors::map_keys,
", ");
84 ASSERT_FALSE(
m_messages.empty()) <<
"No message available to check";
86 EXPECT_EQ(msg.getLogLevel(), level);
87 EXPECT_EQ(msg.getMessage(), message);
ELogLevel
Definition of the supported log levels.
@ c_Error
Error: for things that went wrong and have to be fixed.
static const char * logLevelToString(ELogLevel logLevelType)
Converts a log level type to a string.
Abstract base class for the different types of log connections.
Implements a log connection to an IO Stream.
void resetLogging()
Reset logging system to defaults: empty all log messages and reset connections to the default.
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
void resetLogConnections()
Removes all log connections.
void addLogConnection(LogConnectionBase *logConnection)
Adds a log connection object which is used to the send the logging messages.
void expectMessageVariables(std::map< std::string, std::string > &&variables, bool strict=false)
check if the last message emitted contains the given variables and their values.
std::vector< LogMessage > m_messages
list of log messages
void clearMessages()
clear the list of save log messages
void expectMessageContent(LogConfig::ELogLevel level, const std::string &message)
check the content of the last log message against the given values
void SetUp() override
Add a log message interceptor.
void expectErrorWithVariables(std::map< std::string, std::string > &&variables, bool strict=false)
Expect an error with variables: This is a shorthand to check that there is exactly one error message ...
void expectMessage(LogConfig::ELogLevel level=LogConfig::c_Error, int count=1, bool strict=false)
check if there is a certain number of log messages of a given level
void TearDown() override
And try to reset logging system to default.
Some utilities to help with writing unit tests.