iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
iox2::bb::testing::TestingLogger Class Reference

This logger is used for tests. It caches all the log messages and prints them to the console when a test fails. For debug purposes this behaviour can be overwritten with the 'IOX2_TESTING_ALLOW_LOG' environment variable, e.g. 'IOX2_TESTING_ALLOW_LOG=ON ./hoofs_moduletests –gtest_filter=SharedMemoryObject_Test*'. Furthermore, it can also be used to check for the occurrence on specific log messages, e.g. when a function is expected to log an error. More...

#include <testing_logger.hpp>

Inheritance diagram for iox2::bb::testing::TestingLogger:
iox2::legacy::log::internal::Logger< BaseLogger >

Public Member Functions

 ~TestingLogger () override=default
 
 TestingLogger (const TestingLogger &)=delete
 
 TestingLogger (TestingLogger &&)=delete
 
auto operator= (const TestingLogger &) -> TestingLogger &=delete
 
auto operator= (TestingLogger &&) -> TestingLogger &=delete
 
auto clear_log_buffer () noexcept -> void
 Removes all log messages from the internal cache. This is automatically done at the start of each test.
 
auto print_log_buffer () noexcept -> void
 Prints all log messages from the internal cache. This is automatically done at the end of a failed test.
 
- Public Member Functions inherited from iox2::legacy::log::internal::Logger< BaseLogger >
 Logger ()=default
 
 Logger (const Logger &)=delete
 
 Logger (Logger &&)=delete
 
Loggeroperator= (const Logger &)=delete
 
Loggeroperator= (Logger &&)=delete
 
 ~Logger ()=default
 

Static Public Member Functions

static auto init () noexcept -> void
 Initialized the logger. This should be called in the main function of the test binary.
 
static auto get_number_of_log_messages () noexcept -> uint64_t
 Number of caches log messages.
 
static auto check_log_message_if_log_level_is_supported (legacy::log::LogLevel log_level, const std::function< void(const std::vector< std::string > &)> &check) -> void
 Runs the provided checker function for the collected log messages.
 
static constexpr auto does_logger_support_log_level (const legacy::log::LogLevel log_level) noexcept -> bool
 Checks if the the LogLevel is above the minimal supported LogLevel compiled into the binary.
 
- Static Public Member Functions inherited from iox2::legacy::log::internal::Logger< BaseLogger >
static Loggerget () noexcept
 Access to the logger singleton instance.
 
static void init (const LogLevel logLevel=logLevelFromEnvOr(LogLevel::Info)) noexcept
 Initializes the logger.
 
static void setActiveLogger (Logger &newLogger) noexcept
 Replaces the default logger with the specified one.
 

Protected Member Functions

void flush () noexcept override
 

Detailed Description

This logger is used for tests. It caches all the log messages and prints them to the console when a test fails. For debug purposes this behaviour can be overwritten with the 'IOX2_TESTING_ALLOW_LOG' environment variable, e.g. 'IOX2_TESTING_ALLOW_LOG=ON ./hoofs_moduletests –gtest_filter=SharedMemoryObject_Test*'. Furthermore, it can also be used to check for the occurrence on specific log messages, e.g. when a function is expected to log an error.

call_to_function_Which_logs_an_error();
[](const auto& log_messages){
ASSERT_THAT(log_messages.size(), Eq(1U));
EXPECT_THAT(log_messages[0], HasSubstr(expected_output));
});
static auto check_log_message_if_log_level_is_supported(legacy::log::LogLevel log_level, const std::function< void(const std::vector< std::string > &)> &check) -> void
Runs the provided checker function for the collected log messages.

Definition at line 65 of file testing_logger.hpp.

Constructor & Destructor Documentation

◆ ~TestingLogger()

iox2::bb::testing::TestingLogger::~TestingLogger ( )
overridedefault

◆ TestingLogger() [1/2]

iox2::bb::testing::TestingLogger::TestingLogger ( const TestingLogger )
delete

◆ TestingLogger() [2/2]

iox2::bb::testing::TestingLogger::TestingLogger ( TestingLogger &&  )
delete

Member Function Documentation

◆ check_log_message_if_log_level_is_supported()

static auto iox2::bb::testing::TestingLogger::check_log_message_if_log_level_is_supported ( legacy::log::LogLevel  log_level,
const std::function< void(const std::vector< std::string > &)> &  check 
) -> void
inlinestatic

Runs the provided checker function for the collected log messages.

Note
This can be used in tests to verify the collected log messages

Definition at line 153 of file testing_logger.hpp.

References does_logger_support_log_level().

◆ clear_log_buffer()

auto iox2::bb::testing::TestingLogger::clear_log_buffer ( ) -> void
inlinenoexcept

Removes all log messages from the internal cache. This is automatically done at the start of each test.

Definition at line 123 of file testing_logger.hpp.

◆ does_logger_support_log_level()

static constexpr auto iox2::bb::testing::TestingLogger::does_logger_support_log_level ( const legacy::log::LogLevel  log_level) -> bool
inlinestaticconstexprnoexcept

Checks if the the LogLevel is above the minimal supported LogLevel compiled into the binary.

Parameters
[in]log_levelis the log level to check if it is supported
Returns
true if the log level support is compiled into the binary, false otherwise
Note
This can be used in tests which check for a specific log output

Definition at line 164 of file testing_logger.hpp.

References iox2::legacy::log::MINIMAL_LOG_LEVEL.

Referenced by check_log_message_if_log_level_is_supported().

◆ flush()

void iox2::bb::testing::TestingLogger::flush ( )
inlineoverrideprotectednoexcept

Definition at line 169 of file testing_logger.hpp.

◆ get_number_of_log_messages()

static auto iox2::bb::testing::TestingLogger::get_number_of_log_messages ( ) -> uint64_t
inlinestaticnoexcept

Number of caches log messages.

Returns
the number of the log messages from the internal cache
Note
This can be used in tests which check for a specific log output

Definition at line 145 of file testing_logger.hpp.

References iox2::legacy::log::internal::Logger< BaseLogger >::get().

◆ init()

static auto iox2::bb::testing::TestingLogger::init ( ) -> void
inlinestaticnoexcept

Initialized the logger. This should be called in the main function of the test binary.

#include "test.hpp"
int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
static auto init() noexcept -> void
Initialized the logger. This should be called in the main function of the test binary.

Definition at line 91 of file testing_logger.hpp.

◆ operator=() [1/2]

auto iox2::bb::testing::TestingLogger::operator= ( const TestingLogger ) -> TestingLogger &=delete
delete

◆ operator=() [2/2]

auto iox2::bb::testing::TestingLogger::operator= ( TestingLogger &&  ) -> TestingLogger &=delete
delete

◆ print_log_buffer()

auto iox2::bb::testing::TestingLogger::print_log_buffer ( ) -> void
inlinenoexcept

Prints all log messages from the internal cache. This is automatically done at the end of a failed test.

Definition at line 129 of file testing_logger.hpp.

References iox2::log().


The documentation for this class was generated from the following file: