Skip to content

Commit

Permalink
Merge pull request #2043 from elBoberido/iox-1755-add-log-bin-and-log…
Browse files Browse the repository at this point in the history
…-raw

iox-#1755 Add log bin and log raw
  • Loading branch information
elBoberido authored Oct 25, 2023
2 parents c5f549f + 9cc8c20 commit 89ea8f7
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 50 deletions.
8 changes: 5 additions & 3 deletions doc/website/release-notes/iceoryx-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -806,12 +806,14 @@
```cpp
// before
LogInfo() << iox::log::HexFormat(42);
LogInfo() << iox::log::BinFormat(73); // currently not supported
LogInfo() << iox::log::RawBuffer(buf); // currently not supported
LogInfo() << iox::log::BinFormat(73);
LogInfo() << iox::log::RawBuffer(buf);

// after
IOX_LOG(INFO, iox::log::hex(42));
IOX_LOG(INFO, iox::log::oct(42));
IOX_LOG(INFO, iox::log::oct(37));
IOX_LOG(INFO, iox::log::bin(73));
IOX_LOG(INFO, iox::log::raw(buf));
```

36. Creating an instance of `LogStream` does not work anymore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2023 by Mathias Kraus <[email protected]>. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,6 +39,31 @@ inline constexpr void ConsoleLogger::unused(T&&) noexcept
{
}

// AXIVION Next Construct AutosarC++19_03-M9.3.3 : This is the default implementation for a logger. The design requires
// this to be non-static to not restrict custom implementations
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
inline void ConsoleLogger::logChar(const char value) noexcept
{
auto& data = getThreadLocalData();
const auto bufferWriteIndex = data.bufferWriteIndex;
const auto bufferWriteIndexNext = bufferWriteIndex + 1;
if (bufferWriteIndexNext <= ThreadLocalData::BUFFER_SIZE)
{
// NOLINTJUSTIFICATION it is ensured that the index cannot be out of bounds
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-constant-array-index)
data.buffer[bufferWriteIndex] = value;
data.buffer[bufferWriteIndexNext] = 0;
// NOLINTEND(cppcoreguidelines-pro-bounds-constant-array-index)
data.bufferWriteIndex = bufferWriteIndexNext;
}
else
{
/// @todo iox-#1755 currently we don't support log messages larger than the log buffer and everything larger
/// than the log buffer will be truncated;
/// it is intended to flush the buffer and create a new log message later on
}
}

template <typename T, typename std::enable_if_t<std::is_arithmetic<T>::value, bool>>
inline void ConsoleLogger::logDec(const T value) noexcept
{
Expand All @@ -59,6 +85,38 @@ inline void ConsoleLogger::logOct(const T value) noexcept
logArithmetic(value, LOG_FORMAT_OCT<T>);
}

template <typename T, typename std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value, bool>>
inline void ConsoleLogger::logBin(const T value) noexcept
{
constexpr uint32_t NUMBER_OF_BITS{std::numeric_limits<decltype(value)>::digits};

auto& data = getThreadLocalData();

auto bufferWriteIndexNext = data.bufferWriteIndex;
auto bufferWriteIndexEnd = bufferWriteIndexNext + NUMBER_OF_BITS;
if (bufferWriteIndexEnd > ThreadLocalData::BUFFER_SIZE)
{
/// @todo iox-#1755 currently we don't support log messages larger than the log buffer and everything larger
/// than the log buffer will be truncated;
/// it is intended to flush the buffer and create a new log message later on
bufferWriteIndexEnd = ThreadLocalData::BUFFER_SIZE;
}

constexpr T ONE{1};
T mask{ONE << (NUMBER_OF_BITS - 1)};
for (; bufferWriteIndexNext < bufferWriteIndexEnd; ++bufferWriteIndexNext)
{
// NOLINTJUSTIFICATION it is ensured that the index cannot be out of bounds
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
data.buffer[bufferWriteIndexNext] = (value & mask) ? '1' : '0';
mask = static_cast<T>(mask >> 1);
}
// NOLINTJUSTIFICATION it is ensured that the index cannot be out of bounds
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
data.buffer[bufferWriteIndexNext] = 0;
data.bufferWriteIndex = bufferWriteIndexNext;
}

// AXIVION Next Construct AutosarC++19_03-A3.9.1 : See at declaration in header
template <typename T>
inline void ConsoleLogger::logArithmetic(const T value, const char* format) noexcept
Expand Down
63 changes: 56 additions & 7 deletions iceoryx_hoofs/reporting/include/iox/detail/log/logstream.inl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace log
{
template <typename T>
template <typename>
constexpr LogHex<T>::LogHex(const T value) noexcept
inline constexpr LogHex<T>::LogHex(const T value) noexcept
: m_value(value)
{
}
Expand All @@ -44,14 +44,14 @@ inline constexpr LogHex<T> hex(const T value) noexcept
}

// AXIVION Next Construct AutosarC++19_03-M17.0.3 : See at declaration in header
inline LogHex<const void* const> hex(const void* const ptr) noexcept
inline constexpr LogHex<const void* const> hex(const void* const ptr) noexcept
{
return LogHex<const void* const>(ptr);
}

template <typename T>
template <typename>
constexpr LogOct<T>::LogOct(const T value) noexcept
inline constexpr LogOct<T>::LogOct(const T value) noexcept
: m_value(value)
{
}
Expand All @@ -63,6 +63,39 @@ inline constexpr LogOct<T> oct(const T value) noexcept
return LogOct<T>(value);
}

template <typename T>
template <typename>
inline constexpr LogBin<T>::LogBin(const T value) noexcept
: m_value(value)
{
}

// AXIVION Next Construct AutosarC++19_03-M17.0.3 : See at declaration in header
template <typename T, typename>
inline constexpr LogBin<T> bin(const T value) noexcept
{
return LogBin<T>(value);
}

inline constexpr LogRaw::LogRaw(const void* const data, uint64_t size) noexcept
: m_data(data)
, m_size(size)
{
}

// AXIVION Next Construct AutosarC++19_03-M17.0.3 : See at declaration in header
template <typename T>
inline constexpr typename std::enable_if<!std::is_pointer<T>::value, LogRaw>::type raw(const T& object) noexcept
{
return LogRaw(&object, sizeof(T));
}

// AXIVION Next Construct AutosarC++19_03-M17.0.3 : See at declaration in header
inline constexpr LogRaw raw(const void* const data, const uint64_t size) noexcept
{
return LogRaw(data, size);
}

/// @todo iox-#1755 use something like 'source_location'
// AXIVION Next Construct AutosarC++19_03-A3.9.1 : See at declaration in header
// NOLINTNEXTLINE(readability-function-size)
Expand Down Expand Up @@ -225,7 +258,7 @@ inline LogStream& LogStream::operator<<(const long double val) noexcept
// AXIVION ENABLE STYLE AutosarC++19_03-A3.9.1

template <typename T, typename std::enable_if_t<std::is_integral<T>::value, bool>>
inline LogStream& LogStream::operator<<(const LogHex<T> val) noexcept
inline LogStream& LogStream::operator<<(const LogHex<T>&& val) noexcept
{
m_logger.logString("0x");
m_logger.logHex(static_cast<typename std::make_unsigned<T>::type>(val.m_value));
Expand All @@ -234,29 +267,45 @@ inline LogStream& LogStream::operator<<(const LogHex<T> val) noexcept
}

template <typename T, typename std::enable_if_t<std::is_floating_point<T>::value, bool>>
inline LogStream& LogStream::operator<<(const LogHex<T> val) noexcept
inline LogStream& LogStream::operator<<(const LogHex<T>&& val) noexcept
{
m_logger.logHex(val.m_value);
m_isFlushed = false;
return *this;
}

inline LogStream& LogStream::operator<<(const LogHex<const void* const> val) noexcept
inline LogStream& LogStream::operator<<(const LogHex<const void* const>&& val) noexcept
{
m_logger.logHex(val.m_value);
m_isFlushed = false;
return *this;
}

template <typename T, typename std::enable_if_t<std::is_integral<T>::value, bool>>
inline LogStream& LogStream::operator<<(const LogOct<T> val) noexcept
inline LogStream& LogStream::operator<<(const LogOct<T>&& val) noexcept
{
m_logger.logString("0o");
m_logger.logOct(static_cast<typename std::make_unsigned<T>::type>(val.m_value));
m_isFlushed = false;
return *this;
}

template <typename T, typename std::enable_if_t<std::is_integral<T>::value, bool>>
inline LogStream& LogStream::operator<<(const LogBin<T>&& val) noexcept
{
m_logger.logString("0b");
m_logger.logBin(static_cast<typename std::make_unsigned<T>::type>(val.m_value));
m_isFlushed = false;
return *this;
}

inline LogStream& LogStream::operator<<(const LogRaw&& val) noexcept
{
m_logger.logRaw(val.m_data, val.m_size);
m_isFlushed = false;
return *this;
}

template <typename Callable, typename>
inline LogStream& LogStream::operator<<(const Callable& c) noexcept
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class ConsoleLogger
// AXIVION Next Construct AutosarC++19_03-A3.9.1 : Not used as an integer but a low-level C-style string
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>
Expand All @@ -87,6 +89,11 @@ class ConsoleLogger
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;

private:
// AXIVION Next Construct AutosarC++19_03-A3.9.1 : Not used as an integer but as actual character
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : C-style array is used to acquire size of the array safely. Safe
Expand Down
Loading

0 comments on commit 89ea8f7

Please sign in to comment.