iceoryx2
C++ 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< ConsoleLogger > iox2::legacy::log::ConsoleLogger

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< ConsoleLogger >
 Logger ()=default
 Logger (const Logger &)=delete
 Logger (Logger &&)=delete
Logger & operator= (const Logger &)=delete
Logger & operator= (Logger &&)=delete
 ~Logger ()=default
Public Member Functions inherited from iox2::legacy::log::ConsoleLogger
virtual ~ConsoleLogger ()=default
 ConsoleLogger (const ConsoleLogger &)=delete
 ConsoleLogger (ConsoleLogger &&)=delete
ConsoleLogger & operator= (const ConsoleLogger &)=delete
ConsoleLogger & operator= (ConsoleLogger &&)=delete

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< ConsoleLogger >
static Logger & get () 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.
Static Public Member Functions inherited from iox2::legacy::log::ConsoleLogger
static LogLevel getLogLevel () noexcept
 Obtain the current log level.
static void setLogLevel (const LogLevel logLevel) noexcept
 Sets a new log level.

Protected Member Functions

void flush () noexcept override
Protected Member Functions inherited from iox2::legacy::log::ConsoleLogger
 ConsoleLogger () noexcept=default
virtual void initLogger (const LogLevel) noexcept
virtual void createLogMessageHeader (const bb::detail::SourceLocation location, const LogLevel logLevel) noexcept
LogBuffer getLogBuffer () const noexcept
void assumeFlushed () noexcept
void logString (const char *message) noexcept
void logChar (const char value) noexcept
void logBool (const bool value) noexcept
template<typename T, typename std::enable_if_t< std::is_arithmetic< T >::value, bool > = 0>
void logDec (const T value) noexcept
template<typename T, typename std::enable_if_t<(std::is_integral< T >::value &&std::is_unsigned< T >::value)||std::is_floating_point< T >::value||std::is_pointer< T >::value, bool > = 0>
void logHex (const T value) noexcept
template<typename T, typename std::enable_if_t< std::is_integral< T >::value &&std::is_unsigned< T >::value, bool > = 0>
void logOct (const T value) noexcept
template<typename T, typename std::enable_if_t< std::is_integral< T >::value &&std::is_unsigned< T >::value, bool > = 0>
void logBin (const T value) noexcept
void logRaw (const void *const data, const uint64_t size) noexcept

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

References TestingLogger().

Member Function Documentation

◆ check_log_message_if_log_level_is_supported()

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()

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 ( )
inlineoverrideprotectedvirtualnoexcept

◆ get_number_of_log_messages()

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< ConsoleLogger >::get(), and TestingLogger().

◆ init()

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.

References iox2::legacy::log::equalStrings(), iox2::legacy::log::internal::Logger< ConsoleLogger >::init(), iox2::legacy::log::logLevelFromEnvOr(), iox2::legacy::log::internal::Logger< ConsoleLogger >::setActiveLogger(), TestingLogger(), and iox2::legacy::log::Trace.

◆ operator=() [1/2]

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

References TestingLogger().

◆ operator=() [2/2]

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

References TestingLogger().

◆ 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: