Skip to content

Commit

Permalink
Merged in f/EL20-368_build_elklog_with_cpp20 (pull request #9)
Browse files Browse the repository at this point in the history
Update spdlog for C++20 support

Approved-by: Gustav Andersson
Approved-by: Ilias Bergstrom
Approved-by: Marco Del Fiasco
  • Loading branch information
stez-mind committed Mar 6, 2024
2 parents 386d032 + c4f3209 commit e48a465
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ add_subdirectory(third-party)
add_library(elklog STATIC src/elklog.cpp)
target_include_directories(elklog PUBLIC include ${TWINE_PATH})
set_target_properties(elklog spdlog PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_compile_features(elklog PUBLIC cxx_std_17)
target_compile_features(elklog PUBLIC cxx_std_20)
target_compile_definitions(elklog PUBLIC -DELKLOG_FILE_SIZE=${ELKLOG_FILE_SIZE}
-DELKLOG_RT_MESSAGE_SIZE=${ELKLOG_RT_MESSAGE_SIZE}
-DELKLOG_RT_QUEUE_SIZE=${ELKLOG_RT_QUEUE_SIZE})
Expand Down
40 changes: 30 additions & 10 deletions include/elklog/elk_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#ifndef ELKLOG_DISABLE_LOGGING
#include "spdlog/spdlog.h"
#include "spdlog/fmt/bundled/format.h"

#include "elk-warning-suppressor/warning_suppressor.hpp"

Expand All @@ -51,13 +52,32 @@ ELK_POP_WARNING

#include "rtlogger.h"

#if __cplusplus >= 202002L
// newer versions of fmt library for C++20 requires a custom formatter
// if we want to log simple enum types like error codes, see:
// https://stackoverflow.com/a/69496952

template <typename EnumType>
requires std::is_enum_v<EnumType>
struct fmt::formatter<EnumType> : fmt::formatter<std::underlying_type_t<EnumType>>
{
// Forwards the formatting by casting the enum to it's underlying type
auto format(const EnumType& enumValue, format_context& ctx) const
{
return fmt::formatter<std::underlying_type_t<EnumType>>::format(
static_cast<std::underlying_type_t<EnumType>>(enumValue), ctx);
}
};
#endif // __cplusplus >= 202002L

namespace elklog {

constexpr int RTLOG_MESSAGE_SIZE = ELKLOG_RT_MESSAGE_SIZE;
constexpr int RTLOG_QUEUE_SIZE = ELKLOG_RT_QUEUE_SIZE;
constexpr int MAX_LOG_FILE_SIZE = ELKLOG_FILE_SIZE; // In bytes
constexpr auto RT_CONSUMER_POLL_PERIOD = std::chrono::milliseconds(50);


class ElkLogger
{
public:
Expand Down Expand Up @@ -113,19 +133,19 @@ class ElkLogger
int max_files = 1)
{
_log_file_path = log_file_path;

Status status = set_log_level(_min_log_level);

if (status != Status::OK)
{
return status;
}

if (flush_interval.count() > 0)
{
spdlog::flush_every(std::chrono::seconds(flush_interval));
}

spdlog::flush_on(spdlog::level::err);

// Check for already registered logger
Expand Down Expand Up @@ -205,27 +225,27 @@ class ElkLogger
Status set_log_level(const std::string& min_log_level)
{
_rt_logger->set_log_level(min_log_level);

std::map<std::string, spdlog::level::level_enum> level_map;
level_map["debug"] = spdlog::level::debug;
level_map["info"] = spdlog::level::info;
level_map["warning"] = spdlog::level::warn;
level_map["error"] = spdlog::level::err;
level_map["critical"] = spdlog::level::critical;

_min_log_level = min_log_level;

std::string log_level_lowercase = _min_log_level;
std::transform(_min_log_level.begin(), _min_log_level.end(), log_level_lowercase.begin(), ::tolower);

if (level_map.count(log_level_lowercase) <= 0)
{
return Status::INVALID_LOG_LEVEL;
}

auto log_level = level_map[log_level_lowercase];
spdlog::set_level(log_level);

return Status::OK;
}

Expand Down Expand Up @@ -418,7 +438,7 @@ class ElkLogger
{
return Status::OK;
}

Status set_log_level([[maybe_unused]] const std::string& min_log_level)
{
return Status::OK;
Expand Down
4 changes: 2 additions & 2 deletions include/elklog/rtlogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ class RtLogger
_consumer_thread.join();
}
}

void set_log_level(const std::string& min_log_level)
{
std::map<std::string, RtLogLevel> level_map;
level_map["debug"] = RtLogLevel::DBG;
level_map["info"] = RtLogLevel::INFO;
level_map["warning"] = RtLogLevel::WARNING;
level_map["error"] = RtLogLevel::ERROR;

std::string log_level_lowercase = min_log_level;
std::transform(min_log_level.begin(), min_log_level.end(), log_level_lowercase.begin(), ::tolower);

Expand Down
8 changes: 8 additions & 0 deletions test/unittests/elklog_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ TEST_F(InitLogTest, TestCreation)
ASSERT_EQ(Status::INVALID_LOG_LEVEL, status);
}

TEST_F(InitLogTest, TestCustomTypes)
{
auto status = _module_under_test->initialize("./log.txt", "logger");
ASSERT_EQ(Status::OK, status);

_module_under_test->info("Logging a custom type: {}", status);
}

TEST_F(InitLogTest, TestAddingSinkWithoutInit)
{
auto test_sink = std::make_shared<TestingSink>();
Expand Down
2 changes: 1 addition & 1 deletion third-party/spdlog
Submodule spdlog updated 163 files

0 comments on commit e48a465

Please sign in to comment.