CSV Writing¶
The library provides functionality for asynchronously writing CSV files. Formatting and I/O operations are managed by the backend thread, allowing for efficient and minimal-overhead CSV file writing on the hot path. This feature can be used alongside regular logging.
The CsvWriter
class is a utility designed to facilitate asynchronous CSV file writing.
CSV Writing To File¶
1#include "quill/Backend.h"
2#include "quill/CsvWriter.h"
3#include "quill/LogMacros.h"
4#include "quill/core/FrontendOptions.h"
5#include "quill/sinks/ConsoleSink.h"
6
7/**
8 * This example shows how to define a CSV schema, instantiate a CsvWriter object, and append rows to
9 * the CSV file. The logging operations are handled asynchronously by the backend worker thread.
10 */
11
12struct OrderCsvSchema
13{
14 static constexpr char const* header = "order_id,symbol,quantity,price,side";
15 static constexpr char const* format = "{},{},{},{:.2f},{}";
16};
17
18int main()
19{
20 quill::BackendOptions backend_options;
21 quill::Backend::start(backend_options);
22
23 quill::Logger* logger = quill::Frontend::create_or_get_logger(
24 "root", quill::Frontend::create_or_get_sink<quill::ConsoleSink>("sink_id_1"));
25
26 LOG_INFO(logger, "CSV writing example");
27
28 quill::CsvWriter<OrderCsvSchema, quill::FrontendOptions> csv_writer{"orders.csv"};
29 csv_writer.append_row(13212123, "AAPL", 100, 210.32321, "BUY");
30 csv_writer.append_row(132121123, "META", 300, 478.32321, "SELL");
31 csv_writer.append_row(13212123, "AAPL", 120, 210.42321, "BUY");
32}
Csv output (orders.csv):
order_id,symbol,quantity,price,side
13212123,AAPL,100,210.32,BUY
132121123,META,300,478.32,SELL
13212123,AAPL,120,210.42,BUY
CSV Writing To Existing Sink¶
It is possible to pass an existing Sink, or a custom user-created Sink, to the CSV file for output. The following example shows how to use the console sink
1#include "quill/Backend.h"
2#include "quill/CsvWriter.h"
3#include "quill/LogMacros.h"
4#include "quill/core/FrontendOptions.h"
5#include "quill/sinks/ConsoleSink.h"
6
7struct OrderCsvSchema
8{
9 static constexpr char const* header = "order_id,symbol,quantity,price,side";
10 static constexpr char const* format = "{},{},{},{:.2f},{}";
11};
12
13int main()
14{
15 quill::Backend::start();
16
17 quill::Logger* logger = quill::Frontend::create_or_get_logger(
18 "root", quill::Frontend::create_or_get_sink<quill::ConsoleSink>("sink_id_1"));
19
20 LOG_INFO(logger, "CSV writing example");
21
22 // Pass the existing ConsoleSink to the CsvWritter, this will output the csv to console
23 quill::CsvWriter<OrderCsvSchema, quill::FrontendOptions> csv_writer{
24 "orders.csv", quill::Frontend::get_sink("sink_id_1")};
25
26 csv_writer.append_row(13212123, "AAPL", 100, 210.32321, "BUY");
27 csv_writer.append_row(132121123, "META", 300, 478.32321, "SELL");
28 csv_writer.append_row(13212123, "AAPL", 120, 210.42321, "BUY");
29}