From 3098fd8f47eae27a769f3f45696373f0d6eea995 Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Wed, 6 Sep 2023 23:31:43 +0200 Subject: [PATCH] iox-#1755 Add LogStream operator for 'char' --- .../detail/log/building_blocks/logformat.inl | 5 +++ .../include/iox/detail/log/logstream.inl | 7 ++++ .../iox/log/building_blocks/logformat.hpp | 2 + .../reporting/include/iox/log/logstream.hpp | 9 ++++- .../moduletests/test_reporting_logstream.cpp | 39 +++++++++++++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/iceoryx_hoofs/reporting/include/iox/detail/log/building_blocks/logformat.inl b/iceoryx_hoofs/reporting/include/iox/detail/log/building_blocks/logformat.inl index 1db04c4f1c..5e3c06281b 100644 --- a/iceoryx_hoofs/reporting/include/iox/detail/log/building_blocks/logformat.inl +++ b/iceoryx_hoofs/reporting/include/iox/detail/log/building_blocks/logformat.inl @@ -87,6 +87,11 @@ inline constexpr const char* logFormatDec() noexcept return nullptr; } template <> +inline constexpr const char* logFormatDec() noexcept +{ + return "%c"; +} +template <> inline constexpr const char* logFormatDec() noexcept { return "%hhi"; diff --git a/iceoryx_hoofs/reporting/include/iox/detail/log/logstream.inl b/iceoryx_hoofs/reporting/include/iox/detail/log/logstream.inl index bde9292dab..628ff6725f 100644 --- a/iceoryx_hoofs/reporting/include/iox/detail/log/logstream.inl +++ b/iceoryx_hoofs/reporting/include/iox/detail/log/logstream.inl @@ -135,6 +135,13 @@ inline LogStream& LogStream::operator<<(const bool val) noexcept // AXIVION DISABLE STYLE AutosarC++19_03-A3.9.1 : See at declaration in header +inline LogStream& LogStream::operator<<(const char val) noexcept +{ + m_logger.logDec(val); + m_isFlushed = false; + return *this; +} + inline LogStream& LogStream::operator<<(const signed char val) noexcept { m_logger.logDec(val); diff --git a/iceoryx_hoofs/reporting/include/iox/log/building_blocks/logformat.hpp b/iceoryx_hoofs/reporting/include/iox/log/building_blocks/logformat.hpp index c354ce8534..333629293c 100644 --- a/iceoryx_hoofs/reporting/include/iox/log/building_blocks/logformat.hpp +++ b/iceoryx_hoofs/reporting/include/iox/log/building_blocks/logformat.hpp @@ -52,6 +52,8 @@ namespace internal template constexpr const char* logFormatDec() noexcept; template <> +constexpr const char* logFormatDec() noexcept; +template <> constexpr const char* logFormatDec() noexcept; template <> constexpr const char* logFormatDec() noexcept; diff --git a/iceoryx_hoofs/reporting/include/iox/log/logstream.hpp b/iceoryx_hoofs/reporting/include/iox/log/logstream.hpp index 7f18e4b39e..cb00a42ac1 100644 --- a/iceoryx_hoofs/reporting/include/iox/log/logstream.hpp +++ b/iceoryx_hoofs/reporting/include/iox/log/logstream.hpp @@ -157,13 +157,18 @@ class LogStream /// and avoid the std::string dependency; alternatively this could be implemented as free function LogStream& operator<<(const std::string& str) noexcept; - // AXIVION DISABLE STYLE AutosarC++19_03-A3.9.1 : Basic numeric types are used in order to cover als basic numeric types, independent of the type alias - /// @brief Logging support for 'boolean' /// @param[in] val is the 'boolean' to log /// @return a reference to the LogStream instance LogStream& operator<<(const bool val) noexcept; + // AXIVION DISABLE STYLE AutosarC++19_03-A3.9.1 : Basic numeric types are used in order to cover als basic numeric types, independent of the type alias + + /// @brief Logging support for 'char' + /// @param[in] val is the 'char' to log + /// @return a reference to the LogStream instance + LogStream& operator<<(const char val) noexcept; + /// @brief Logging support for 'signed char' /// @param[in] val is the 'signed char' to log /// @return a reference to the LogStream instance diff --git a/iceoryx_hoofs/test/moduletests/test_reporting_logstream.cpp b/iceoryx_hoofs/test/moduletests/test_reporting_logstream.cpp index 41995cdd4c..a3f9d3c70f 100644 --- a/iceoryx_hoofs/test/moduletests/test_reporting_logstream.cpp +++ b/iceoryx_hoofs/test/moduletests/test_reporting_logstream.cpp @@ -129,6 +129,45 @@ TEST_F(IoxLogStream_test, StreamOperatorStdString) EXPECT_THAT(loggerMock.logs[1].message, StrEq(constLogValue)); } +TEST_F(IoxLogStream_test, StreamOperatorChar) +{ + ::testing::Test::RecordProperty("TEST_ID", "2a1fff17-e388-4f84-bb16-30bb3432ae9d"); + char logValue{'b'}; + const char constLogValue{'o'}; + constexpr char constexprLogValue{'b'}; + LogStreamSut(loggerMock) << logValue; + LogStreamSut(loggerMock) << constLogValue; + LogStreamSut(loggerMock) << constexprLogValue; + + ASSERT_THAT(loggerMock.logs.size(), Eq(3U)); + EXPECT_THAT(loggerMock.logs[0].message, StrEq("b")); + EXPECT_THAT(loggerMock.logs[1].message, StrEq("o")); + EXPECT_THAT(loggerMock.logs[2].message, StrEq("b")); +} + +TEST_F(IoxLogStream_test, StreamOperator8BitTypesWithCharAsCharacterAndEverythingElseAsNumber) +{ + ::testing::Test::RecordProperty("TEST_ID", "707d4c04-1999-4713-b930-e113969617e0"); + char cc{'a'}; + signed char sc{'a'}; + unsigned char uc{'a'}; + int8_t i8{'a'}; + uint8_t u8{'a'}; + + LogStreamSut(loggerMock) << cc; + LogStreamSut(loggerMock) << sc; + LogStreamSut(loggerMock) << uc; + LogStreamSut(loggerMock) << i8; + LogStreamSut(loggerMock) << u8; + + ASSERT_THAT(loggerMock.logs.size(), Eq(5U)); + EXPECT_THAT(loggerMock.logs[0].message, StrEq("a")); + EXPECT_THAT(loggerMock.logs[1].message, StrEq("97")); + EXPECT_THAT(loggerMock.logs[2].message, StrEq("97")); + EXPECT_THAT(loggerMock.logs[3].message, StrEq("97")); + EXPECT_THAT(loggerMock.logs[4].message, StrEq("97")); +} + TEST_F(IoxLogStream_test, StreamOperatorLogLevel) { ::testing::Test::RecordProperty("TEST_ID", "d85b7ef4-35de-4e11-b0fd-f0de6581a9e6");