Merge branch 'v1.x' into p_attributes

pull/2595/head
Bailey Chittle 3 years ago
commit 7b92a6c15f

@ -176,7 +176,6 @@ if(SPDLOG_SYSTEM_INCLUDES)
set(SPDLOG_INCLUDES_LEVEL "SYSTEM")
endif()
target_compile_definitions(spdlog PUBLIC SPDLOG_COMPILED_LIB)
target_include_directories(spdlog ${SPDLOG_INCLUDES_LEVEL} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

@ -232,6 +232,27 @@ void multi_sink_example()
}
```
---
#### User defined callbacks about log events
```c++
// create logger with a lambda function callback, the callback will be called
// each time something is logged to the logger
void callback_example()
{
auto callback_sink = std::make_shared<spdlog::sinks::callback_sink_mt>([](const spdlog::details::log_msg &msg) {
// for example you can be notified by sending an email to yourself
});
callback_sink->set_level(spdlog::level::err);
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
spdlog::logger logger("custom_callback_logger", {console_sink, callback_sink});
logger.info("some info log");
logger.error("critical issue"); // will notify you
}
```
---
#### Asynchronous logging
```c++

@ -12,7 +12,7 @@ endif()
# Example of using pre-compiled library
# ---------------------------------------------------------------------------------------
add_executable(example example.cpp)
target_link_libraries(example PRIVATE spdlog::spdlog)
target_link_libraries(example PRIVATE spdlog::spdlog $<$<BOOL:${MINGW}>:ws2_32>)
# ---------------------------------------------------------------------------------------
# Example of using header-only library

@ -12,6 +12,7 @@ void stdout_logger_example();
void basic_example();
void rotating_example();
void daily_example();
void callback_example();
void async_example();
void binary_example();
void vector_example();
@ -74,6 +75,7 @@ int main(int, char *[])
basic_example();
rotating_example();
daily_example();
callback_example();
async_example();
binary_example();
vector_example();
@ -139,6 +141,15 @@ void daily_example()
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
}
#include "spdlog/sinks/callback_sink.h"
void callback_example()
{
// Create the logger
auto logger = spdlog::callback_logger_mt("custom_callback_logger", [](const spdlog::details::log_msg &/*msg*/) {
// do what you need to do with msg
});
}
#include "spdlog/cfg/env.h"
void load_levels_example()
{

@ -15,9 +15,11 @@
#include <stdio.h>
#include <string>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Mswsock.lib")
#pragma comment(lib, "AdvApi32.lib")
#if defined(_MSC_VER)
# pragma comment(lib, "Ws2_32.lib")
# pragma comment(lib, "Mswsock.lib")
# pragma comment(lib, "AdvApi32.lib")
#endif
namespace spdlog {
namespace details {
@ -25,7 +27,7 @@ class udp_client
{
static constexpr int TX_BUFFER_SIZE = 1024 * 10;
SOCKET socket_ = INVALID_SOCKET;
sockaddr_in addr_ = {0};
sockaddr_in addr_ = {};
static void init_winsock_()
{

@ -196,7 +196,7 @@ struct formatter<spdlog::details::dump_info<T>, char>
continue;
}
if (put_delimiters)
if (put_delimiters && i != the_range.get_begin())
{
*inserter++ = delimiter;
}

@ -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

@ -31,6 +31,7 @@ set(SPDLOG_UTESTS_SOURCES
test_stdout_api.cpp
test_backtrace.cpp
test_create_dir.cpp
test_custom_callbacks.cpp
test_cfg.cpp
test_time_point.cpp
test_stopwatch.cpp

@ -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();
}
Loading…
Cancel
Save