Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1755 Add LogStream operator for 'char'
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Sep 6, 2023
1 parent cc844be commit 3098fd8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ inline constexpr const char* logFormatDec() noexcept
return nullptr;
}
template <>
inline constexpr const char* logFormatDec<char>() noexcept
{
return "%c";
}
template <>
inline constexpr const char* logFormatDec<signed char>() noexcept
{
return "%hhi";
Expand Down
7 changes: 7 additions & 0 deletions iceoryx_hoofs/reporting/include/iox/detail/log/logstream.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ namespace internal
template <typename T>
constexpr const char* logFormatDec() noexcept;
template <>
constexpr const char* logFormatDec<char>() noexcept;
template <>
constexpr const char* logFormatDec<signed char>() noexcept;
template <>
constexpr const char* logFormatDec<unsigned char>() noexcept;
Expand Down
9 changes: 7 additions & 2 deletions iceoryx_hoofs/reporting/include/iox/log/logstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions iceoryx_hoofs/test/moduletests/test_reporting_logstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 3098fd8

Please sign in to comment.