Skip to content

Commit

Permalink
Add overload to create_or_get_logger for passing variable number of…
Browse files Browse the repository at this point in the history
… sinks at runtime
  • Loading branch information
odygrd committed Sep 27, 2024
1 parent b8a801d commit b68731a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
- Prevented error logs from the `SignalHandler` from being output to CSV files when a `CsvWriter` is in
use. ([#588](https://github.com/odygrd/quill/issues/588))
- Implemented a workaround to resolve false positive warnings from `clang-tidy` on Windows.
- Added a new `create_or_get_logger` overload that accepts a `std::vector<std::shared_ptr<Sink>>`, improving flexibility
by allowing a variable number of sinks to be passed at runtime when creating a logger.

## v7.2.2

Expand Down
29 changes: 25 additions & 4 deletions include/quill/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FrontendImpl
{
auto const volatile spsc_queue_capacity = detail::get_local_thread_context<TFrontendOptions>()
->template get_spsc_queue<TFrontendOptions::queue_type>()
.capacity();
.capacity();

// On windows and c++17, QUILL_MAYBE_UNUSED won't work
(void)spsc_queue_capacity;
Expand Down Expand Up @@ -88,7 +88,8 @@ class FrontendImpl
*/
static logger_t* create_or_get_logger(std::string const& logger_name, std::shared_ptr<Sink> sink,
PatternFormatterOptions const& pattern_formatter_options = PatternFormatterOptions{},
ClockSourceType clock_source = ClockSourceType::Tsc, UserClockSource* user_clock = nullptr)
ClockSourceType clock_source = ClockSourceType::Tsc,
UserClockSource* user_clock = nullptr)
{
std::vector<std::shared_ptr<Sink>> sinks;
sinks.push_back(static_cast<std::shared_ptr<Sink>&&>(sink));
Expand All @@ -98,6 +99,26 @@ class FrontendImpl
pattern_formatter_options, clock_source, user_clock));
}

/**
* @brief Creates a new logger or retrieves an existing one with the specified name and multiple sinks.
*
* @param logger_name The name of the logger.
* @param sinks A vector of shared pointers to sinks to associate with the logger.
* @param pattern_formatter_options Contains the formatting configuration for PatternFormatter
* @param clock_source The clock source for log timestamps.
* @param user_clock A pointer to a custom user clock.
* @return Logger* A pointer to the created or retrieved logger.
*/
static logger_t* create_or_get_logger(
std::string const& logger_name, std::vector<std::shared_ptr<Sink>> sinks,
PatternFormatterOptions const& pattern_formatter_options = PatternFormatterOptions{},
ClockSourceType clock_source = ClockSourceType::Tsc, UserClockSource* user_clock = nullptr)
{
return _cast_to_logger(detail::LoggerManager::instance().create_or_get_logger<logger_t>(
logger_name, static_cast<std::vector<std::shared_ptr<Sink>>&&>(sinks),
pattern_formatter_options, clock_source, user_clock));
}

/**
* @brief Creates a new logger or retrieves an existing one with the specified name and multiple sinks.
*
Expand All @@ -113,8 +134,8 @@ class FrontendImpl
PatternFormatterOptions const& pattern_formatter_options = PatternFormatterOptions{},
ClockSourceType clock_source = ClockSourceType::Tsc, UserClockSource* user_clock = nullptr)
{
return _cast_to_logger(detail::LoggerManager::instance().create_or_get_logger<logger_t>(
logger_name, std::vector<std::shared_ptr<Sink>>{sinks}, pattern_formatter_options, clock_source, user_clock));
return create_or_get_logger(logger_name, std::vector<std::shared_ptr<Sink>>{sinks},
pattern_formatter_options, clock_source, user_clock);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/integration_tests/JsonMultilineMetadataTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ TEST_CASE("json_multi_line_metadata")
"%(message) %(caller_function)",
"%H:%M:%S.%Qns", Timezone::LocalTime, true});

std::vector<std::shared_ptr<Sink>> sinks_vec{json_file_sink, file_sink};
Logger* logger_b = Frontend::create_or_get_logger(
logger_name_b, std::initializer_list<std::shared_ptr<Sink>>{json_file_sink, file_sink},
logger_name_b, std::move(sinks_vec),
PatternFormatterOptions{
"%(time) [%(thread_id)] %(short_source_location:<28) LOG_%(log_level:<9) %(logger:<12) "
"%(message) %(caller_function)",
Expand Down

0 comments on commit b68731a

Please sign in to comment.