Skip to content

Commit

Permalink
Fix csv writer with custom frontend options (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
odygrd authored Oct 13, 2024
1 parent f339278 commit 30c06f4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
`RTTI` ([#604](https://github.com/odygrd/quill/issues/604))
- Fixed an incorrectly triggered assertion in debug builds when `BackendOptions::log_timestamp_ordering_grace_period` is
set to 0 ([#605](https://github.com/odygrd/quill/issues/605))
- Fixed a compile-time error in `CsvWriter` that occurred when passing a custom `FrontendOptions` type as a template parameter. ([#609](https://github.com/odygrd/quill/issues/609))

## v7.3.0

Expand Down
14 changes: 8 additions & 6 deletions include/quill/CsvWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ QUILL_BEGIN_NAMESPACE
* and I/O operations are handled by the backend worker thread.
*
* @tparam TCsvSchema A user-defined struct specifying the CSV schema at compile-time.
* @tparam TFrontendOptions Custom frontend options if they are used application-wide. If no custom frontend options are used, then use quill::FrontendOptions.
* @tparam TFrontendOptions Custom frontend_t options if they are used application-wide. If no custom frontend_t options are used, then use quill::frontend_tOptions.
*
* The TCsvSchema struct should define the CSV header and format, for example:
*
Expand All @@ -41,6 +41,8 @@ template <typename TCsvSchema, typename TFrontendOptions>
class CsvWriter
{
public:
using frontend_t = FrontendImpl<TFrontendOptions>;

/**
* Constructs a CsvWriter object that writes to a file.
*
Expand All @@ -51,9 +53,9 @@ class CsvWriter
explicit CsvWriter(std::string const& filename, char open_mode = 'w',
FilenameAppendOption filename_append = FilenameAppendOption::None)
{
_logger = Frontend::create_or_get_logger(
_logger = frontend_t::create_or_get_logger(
_logger_name_prefix + filename,
Frontend::create_or_get_sink<FileSink>(filename,
frontend_t::template create_or_get_sink<FileSink>(filename,
[open_mode, filename_append]()
{
FileSinkConfig cfg;
Expand All @@ -74,7 +76,7 @@ class CsvWriter
*/
CsvWriter(std::string const& unique_name, std::shared_ptr<Sink> sink)
{
_logger = Frontend::create_or_get_logger(_logger_name_prefix + unique_name, std::move(sink),
_logger = frontend_t::create_or_get_logger(_logger_name_prefix + unique_name, std::move(sink),
PatternFormatterOptions{"%(message)", "", Timezone::GmtTime});

_logger->template log_statement<false, false>(LogLevel::None, &_header_metadata, TCsvSchema::header);
Expand All @@ -88,7 +90,7 @@ class CsvWriter
*/
CsvWriter(std::string const& unique_name, std::initializer_list<std::shared_ptr<Sink>> sinks)
{
_logger = Frontend::create_or_get_logger(
_logger = frontend_t::create_or_get_logger(
_logger_name_prefix + unique_name, sinks, PatternFormatterOptions{"%(message)", "", Timezone::GmtTime});

_logger->template log_statement<false, false>(LogLevel::None, &_header_metadata, TCsvSchema::header);
Expand All @@ -100,7 +102,7 @@ class CsvWriter
~CsvWriter()
{
_logger->flush_log();
Frontend::remove_logger(_logger);
frontend_t::remove_logger(_logger);
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ quill_add_test(TEST_BoundedDroppingQueueDropMessages BoundedDroppingQueueDropMes
quill_add_test(TEST_ConsoleSinkStderrMultipleFormats ConsoleSinkStderrMultipleFormatsTest.cpp)
quill_add_test(TEST_ConsoleSinkStdoutMultipleFormats ConsoleSinkStdoutMultipleFormatsTest.cpp)
quill_add_test(TEST_CsvWriting CsvWritingTest.cpp)
quill_add_test(TEST_CsvWritingCustomFrontend CsvWritingCustomFrontendTest.cpp)
quill_add_test(TEST_EnumLogging EnumLoggingTest.cpp)
quill_add_test(TEST_FlushMultipleLoggers FlushMultipleLoggers.cpp)
quill_add_test(TEST_FlushWithoutAnyLog FlushWithoutAnyLog.cpp)
Expand Down
54 changes: 54 additions & 0 deletions test/integration_tests/CsvWritingCustomFrontendTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "doctest/doctest.h"

#include "misc/TestUtilities.h"
#include "quill/Backend.h"
#include "quill/CsvWriter.h"
#include "quill/core/FrontendOptions.h"

using namespace quill;

struct OrderCsvSchema
{
static constexpr char const* header = "order_id,symbol,quantity,price,side";
static constexpr char const* format = "{},{},{},{:.2f},{}";
};

// Define custom Frontend Options
struct CustomFrontendOptions
{
static constexpr quill::QueueType queue_type = quill::QueueType::BoundedBlocking;
static constexpr uint32_t initial_queue_capacity = 131'072;
static constexpr uint32_t blocking_queue_retry_interval_ns = 800;
static constexpr bool huge_pages_enabled = false;
};

/***/
TEST_CASE("csv_writing_custom_frontend")
{
static constexpr char const* filename = "orders_custom_frontend.csv";

// Start the backend thread
quill::BackendOptions backend_options;
quill::Backend::start(backend_options);

{
quill::CsvWriter<OrderCsvSchema, CustomFrontendOptions> csv_writter{filename};
csv_writter.append_row(13212123, "AAPL", 100, 210.32321, "BUY");
csv_writter.append_row(132121123, "META", 300, 478.32321, "SELL");
csv_writter.append_row(14212123, "AAPL", 120, 210.42321, "BUY");
}

// 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_EQ(file_contents.size(), 4);

REQUIRE(quill::testing::file_contains(file_contents, "order_id,symbol,quantity,price,side"));
REQUIRE(quill::testing::file_contains(file_contents, "13212123,AAPL,100,210.32,BUY"));
REQUIRE(quill::testing::file_contains(file_contents, "132121123,META,300,478.32,SELL"));
REQUIRE(quill::testing::file_contains(file_contents, "14212123,AAPL,120,210.42,BUY"));

testing::remove_file(filename);
}

0 comments on commit 30c06f4

Please sign in to comment.