-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. | ||
// Distributed under the MIT License (http://opensource.org/licenses/MIT) | ||
|
||
#pragma once | ||
|
||
#include <spdlog/details/null_mutex.h> | ||
#include <spdlog/sinks/base_sink.h> | ||
#include <spdlog/details/synchronous_factory.h> | ||
|
||
#include <mutex> | ||
#include <string> | ||
|
||
namespace spdlog { | ||
|
||
// callbacks type | ||
typedef std::function<void(const details::log_msg &msg)> custom_log_callback; | ||
|
||
namespace sinks { | ||
/* | ||
* Trivial callback sink, gets a callback function and calls it on each log | ||
*/ | ||
template<typename Mutex> | ||
class callback_sink final : public base_sink<Mutex> | ||
{ | ||
public: | ||
explicit callback_sink(const custom_log_callback &callback) | ||
: callback_{callback} | ||
{} | ||
|
||
protected: | ||
void sink_it_(const details::log_msg &msg) override | ||
{ | ||
callback_(msg); | ||
} | ||
void flush_() override{}; | ||
|
||
private: | ||
custom_log_callback callback_; | ||
}; | ||
|
||
using callback_sink_mt = callback_sink<std::mutex>; | ||
using callback_sink_st = callback_sink<details::null_mutex>; | ||
|
||
} // namespace sinks | ||
|
||
// | ||
// factory functions | ||
// | ||
template<typename Factory = spdlog::synchronous_factory> | ||
inline std::shared_ptr<logger> callback_logger_mt(const std::string &logger_name, const custom_log_callback &callback) | ||
{ | ||
return Factory::template create<sinks::callback_sink_mt>(logger_name, callback); | ||
} | ||
|
||
template<typename Factory = spdlog::synchronous_factory> | ||
inline std::shared_ptr<logger> callback_logger_st(const std::string &logger_name, const custom_log_callback &callback) | ||
{ | ||
return Factory::template create<sinks::callback_sink_st>(logger_name, callback); | ||
} | ||
|
||
} // namespace spdlog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE | ||
*/ | ||
#include "includes.h" | ||
#include "test_sink.h" | ||
#include "spdlog/sinks/callback_sink.h" | ||
#include "spdlog/async.h" | ||
#include "spdlog/common.h" | ||
|
||
TEST_CASE("custom_callback_logger", "[custom_callback_logger]]") | ||
{ | ||
std::vector<std::string> lines; | ||
spdlog::pattern_formatter formatter; | ||
auto callback_logger = std::make_shared<spdlog::sinks::callback_sink_st>([&](const spdlog::details::log_msg &msg) { | ||
spdlog::memory_buf_t formatted; | ||
formatter.format(msg, formatted); | ||
auto eol_len = strlen(spdlog::details::os::default_eol); | ||
lines.emplace_back(formatted.begin(), formatted.end() - eol_len); | ||
}); | ||
std::shared_ptr<spdlog::sinks::test_sink_st> test_sink(new spdlog::sinks::test_sink_st); | ||
|
||
spdlog::logger logger("test-callback", {callback_logger, test_sink}); | ||
|
||
logger.info("test message 1"); | ||
logger.info("test message 2"); | ||
logger.info("test message 3"); | ||
|
||
std::vector<std::string> ref_lines = test_sink->lines(); | ||
|
||
REQUIRE(lines[0] == ref_lines[0]); | ||
REQUIRE(lines[1] == ref_lines[1]); | ||
REQUIRE(lines[2] == ref_lines[2]); | ||
spdlog::drop_all(); | ||
} |