Skip to content

Commit

Permalink
v4.0.0 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
odygrd committed May 17, 2024
1 parent b1586db commit 2edfbf5
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 4 deletions.
1 change: 1 addition & 0 deletions quill/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ set(HEADER_FILES
include/quill/sinks/StreamSink.h

include/quill/std/Array.h
include/quill/std/Pair.h

include/quill/Backend.h
include/quill/BackendTscClock.h
Expand Down
1 change: 1 addition & 0 deletions quill/include/quill/backend/BackendWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "quill/sinks/Sink.h"

#include "quill/std/Array.h"
#include "quill/std/Pair.h"

#include "quill/bundled/fmt/args.h"
#include "quill/bundled/fmt/core.h"
Expand Down
4 changes: 0 additions & 4 deletions quill/include/quill/std/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
#include <string_view>
#include <type_traits>

namespace quill::detail
{
Expand Down
66 changes: 66 additions & 0 deletions quill/include/quill/std/Pair.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright(c) 2020-present, Odysseas Georgoudis & quill contributors.
* Distributed under the MIT License (http://opensource.org/licenses/MIT)
*/

#pragma once

#include "quill/bundled/fmt/args.h"
#include "quill/bundled/fmt/core.h"
#include "quill/core/Attributes.h"
#include "quill/core/Codec.h"

#include <cstddef>
#include <cstdint>
#include <utility>

namespace quill::detail
{
/***/
template <typename Arg1, typename Arg2>
struct ArgSizeCalculator<std::pair<Arg1, Arg2>>
{
QUILL_ATTRIBUTE_HOT static constexpr size_t calculate(std::vector<size_t>& c_style_string_lengths_cache,
std::pair<Arg1, Arg2> const& arg) noexcept
{
return ArgSizeCalculator<Arg1>::calculate(c_style_string_lengths_cache, arg.first) +
ArgSizeCalculator<Arg2>::calculate(c_style_string_lengths_cache, arg.second);
}
};

/***/
template <typename Arg1, typename Arg2>
struct Encoder<std::pair<Arg1, Arg2>>
{
QUILL_ATTRIBUTE_HOT static inline void encode(std::byte*& buffer, std::vector<size_t> const& c_style_string_lengths_cache,
uint32_t& c_style_string_lengths_cache_index,
std::pair<Arg1, Arg2> const& arg) noexcept
{
Encoder<Arg1>::encode(buffer, c_style_string_lengths_cache, c_style_string_lengths_cache_index,
arg.first);
Encoder<Arg2>::encode(buffer, c_style_string_lengths_cache, c_style_string_lengths_cache_index,
arg.second);
}
};

/***/
template <typename Arg1, typename Arg2>
struct Decoder<std::pair<Arg1, Arg2>>
{
QUILL_ATTRIBUTE_HOT static inline std::pair<Arg1, Arg2> decode(
std::byte*& buffer, fmtquill::dynamic_format_arg_store<fmtquill::format_context>* args_store)
{
std::pair<Arg1, Arg2> arg;

arg.first = Decoder<Arg1>::decode(buffer, nullptr);
arg.second = Decoder<Arg2>::decode(buffer, nullptr);

if (args_store)
{
args_store->push_back(arg);
}

return arg;
}
};
} // namespace quill::detail
1 change: 1 addition & 0 deletions quill/test/integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ quill_add_test(TEST_RotatingSinkOverwriteOldest RotatingSinkOverwriteOldestTest.
quill_add_test(TEST_SingleFrontendThread SingleFrontendThreadTest.cpp)
quill_add_test(TEST_SinkFilter SinkFilterTest.cpp)
quill_add_test(TEST_StdArrayLogging StdArrayLoggingTest.cpp)
quill_add_test(TEST_StdPairLogging StdPairLoggingTest.cpp)
quill_add_test(TEST_SinkLogLevelFilter SinkLogLevelFilterTest.cpp)
quill_add_test(TEST_StringLargeLogging StringLargeLoggingTest.cpp)
quill_add_test(TEST_StringLoggingDynamicLogLevel StringLoggingDynamicLogLevelTest.cpp)
Expand Down
86 changes: 86 additions & 0 deletions quill/test/integration_tests/StdPairLoggingTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "doctest/doctest.h"

#include "misc/TestUtilities.h"
#include "quill/Backend.h"
#include "quill/Frontend.h"
#include "quill/LogMacros.h"
#include "quill/sinks/FileSink.h"

#include "quill/std/Pair.h"

#include <array>
#include <cstdio>
#include <string>
#include <string_view>
#include <vector>

using namespace quill;

/***/
TEST_CASE("std_pair_logging")
{
static constexpr char const* filename = "std_pair_logging.log";
static std::string const logger_name = "logger";

// Start the logging backend thread
Backend::start();

Frontend::preallocate();

// Set writing logging to a file
auto file_sink = Frontend::create_or_get_sink<FileSink>(
filename,
[]()
{
FileSinkConfig cfg;
cfg.set_open_mode('w');
return cfg;
}(),
FileEventNotifier{});

Logger* logger = Frontend::create_or_get_logger(logger_name, std::move(file_sink));

{
std::pair<bool, int> b = {true, 312};
LOG_INFO(logger, "v {}", b);

std::pair<std::string, std::string> sa = {"test", "string"};
LOG_INFO(logger, "sa {}", sa);

std::pair<std::string_view, std::string> sva = {"test", "string_view"};
LOG_INFO(logger, "sva {}", sva);

std::pair<char const*, std::string> scva = {"c style", "string test"};
LOG_INFO(logger, "scva {}", scva);

std::pair<std::pair<std::string, std::string_view>, std::pair<const char*, uint32_t>> cp = {
{"pair", "testing"}, {"first", 2}};
LOG_INFO(logger, "cp {}", cp);
}

logger->flush_log();
Frontend::remove_logger(logger);

// Wait until the backend thread stops for test stability
Backend::stop();

// Read file and check
std::vector<std::string> const file_contents = quill::testing::file_contents(filename);

REQUIRE(quill::testing::file_contains(
file_contents, std::string{"LOG_INFO " + logger_name + " v (true, 312)"}));

REQUIRE(quill::testing::file_contains(
file_contents, std::string{"LOG_INFO " + logger_name + " sa (\"test\", \"string\")"}));

REQUIRE(quill::testing::file_contains(
file_contents, std::string{"LOG_INFO " + logger_name + " sva (\"test\", \"string_view\")"}));

REQUIRE(quill::testing::file_contains(
file_contents, std::string{"LOG_INFO " + logger_name + " scva (\"c style\", \"string test\")"}));

REQUIRE(quill::testing::file_contains(
file_contents, std::string{"LOG_INFO " + logger_name + " cp ((\"pair\", \"testing\"), (\"first\", 2))"}));

testing::remove_file(filename);
}

0 comments on commit 2edfbf5

Please sign in to comment.