Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added relay_sink. #3017

Open
wants to merge 1 commit into
base: v1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/spdlog/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ class SPDLOG_API logger {

void log(level::level_enum lvl, string_view_t msg) { log(source_loc{}, lvl, msg); }

void log_raw(const details::log_msg &log_msg) {
bool log_enabled = should_log(log_msg.level);
bool traceback_enabled = tracer_.enabled();
if (!log_enabled && !traceback_enabled) {
return;
}
log_it_(log_msg, log_enabled, traceback_enabled);
}

template <typename... Args>
void trace(format_string_t<Args...> fmt, Args &&...args) {
log(level::trace, fmt, std::forward<Args>(args)...);
Expand Down
35 changes: 35 additions & 0 deletions include/spdlog/sinks/relay_sink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)

#pragma once

//
// Sink to wrap another logger. This is usefull when consolidating multiple application components
// with built-in loggers into one app wide logger.
//

#include <spdlog/sinks/base_sink.h>
#include <spdlog/logger.h>

namespace spdlog {
namespace sinks {

template <typename Mutex>
class relay_sink : public base_sink<Mutex> {
public:
relay_sink(const std::shared_ptr<spdlog::logger>& logger)
: m_logger{logger} {}

protected:
void sink_it_(const spdlog::details::log_msg& msg) override { m_logger->log_raw(msg); };
void flush_() override{};

private:
std::shared_ptr<spdlog::logger> m_logger;
};

using relay_sink_mt = relay_sink<std::mutex>;
using relay_sink_st = relay_sink<spdlog::details::null_mutex>;

} // namespace sinks
} // namespace spdlog
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(SPDLOG_UTESTS_SOURCES
test_eventlog.cpp
test_pattern_formatter.cpp
test_async.cpp
test_relay_sink.cpp
test_registry.cpp
test_macros.cpp
utils.cpp
Expand Down
19 changes: 19 additions & 0 deletions tests/test_relay_sink.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "includes.h"
#include "spdlog/sinks/relay_sink.h"
#include "spdlog/logger.h"
#include "test_sink.h"

TEST_CASE("relay_sink_test1", "[relay_sink]") {
using spdlog::sinks::relay_sink_st;
using spdlog::sinks::test_sink_mt;

auto test_sink = std::make_shared<test_sink_mt>();
auto remote_logger = std::make_shared<spdlog::logger>("remote", test_sink);
auto relay_sink = std::make_shared<relay_sink_st>(remote_logger);

for (int i = 0; i < 10; i++) {
relay_sink->log(spdlog::details::log_msg{"test", spdlog::level::info, "message1"});
}

REQUIRE(test_sink->msg_counter() == 10);
}