Merge branch 'gabime:v1.x' into v1.x

pull/2653/head
NaDDu 3 years ago committed by GitHub
commit be1aed9143
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -82,6 +82,7 @@ option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF)
option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF)
# install options
option(SPDLOG_SYSTEM_INCLUDES "Include as system headers (skip for clang-tidy)." OFF)
option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_USE_STD_FORMAT "Use std::format instead of fmt library." OFF)
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF)
@ -170,8 +171,13 @@ endif()
add_library(spdlog::spdlog ALIAS spdlog)
set(SPDLOG_INCLUDES_LEVEL "")
if(SPDLOG_SYSTEM_INCLUDES)
set(SPDLOG_INCLUDES_LEVEL "SYSTEM")
endif()
target_compile_definitions(spdlog PUBLIC SPDLOG_COMPILED_LIB)
target_include_directories(spdlog PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
target_include_directories(spdlog ${SPDLOG_INCLUDES_LEVEL} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(spdlog PUBLIC Threads::Threads)
spdlog_enable_warnings(spdlog)
@ -190,7 +196,7 @@ endif()
add_library(spdlog_header_only INTERFACE)
add_library(spdlog::spdlog_header_only ALIAS spdlog_header_only)
target_include_directories(spdlog_header_only INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
target_include_directories(spdlog_header_only ${SPDLOG_INCLUDES_LEVEL} INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(spdlog_header_only INTERFACE Threads::Threads)

@ -146,17 +146,16 @@ void daily_example()
```c++
// Debug messages can be stored in a ring buffer instead of being logged immediately.
// This is useful in order to display debug logs only when really needed (e.g. when error happens).
// When needed, call dump_backtrace() to see them.
// When needed, call dump_backtrace() to dump them to your log.
spdlog::enable_backtrace(32); // Store the latest 32 messages in a buffer. Older messages will be dropped.
spdlog::enable_backtrace(32); // Store the latest 32 messages in a buffer.
// or my_logger->enable_backtrace(32)..
for(int i = 0; i < 100; i++)
{
spdlog::debug("Backtrace message {}", i); // not logged yet..
}
// e.g. if some error happened:
// e.g. if some has error happened:
spdlog::dump_backtrace(); // log them now! show the last 32 messages
// or my_logger->dump_backtrace(32)..
```
@ -233,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++
@ -376,7 +396,7 @@ $ ./example
#### Log file open/close event handlers
```c++
// You can get callbacks from spdlog before/after log file has been opened or closed.
// This is useful for cleanup procedures or for adding someting the start/end of the log files.
// This is useful for cleanup procedures or for adding something the start/end of the log files.
void file_events_example()
{
// pass the spdlog::file_event_handlers to file sinks for open/close log file notifications

@ -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();
@ -72,6 +73,7 @@ int main(int, char *[])
basic_example();
rotating_example();
daily_example();
callback_example();
async_example();
binary_example();
vector_example();
@ -136,6 +138,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()
{

@ -54,6 +54,12 @@ SPDLOG_INLINE void backtracer::push_back(const log_msg &msg)
messages_.push_back(log_msg_buffer{msg});
}
SPDLOG_INLINE bool backtracer::empty() const
{
std::lock_guard<std::mutex> lock{mutex_};
return messages_.empty();
}
// pop all items in the q and apply the given fun on each of them.
SPDLOG_INLINE void backtracer::foreach_pop(std::function<void(const details::log_msg &)> fun)
{

@ -32,6 +32,7 @@ public:
void disable();
bool enabled() const;
void push_back(const log_msg &msg);
bool empty() const;
// pop all items in the q and apply the given fun on each of them.
void foreach_pop(std::function<void(const details::log_msg &)> fun);

@ -90,6 +90,14 @@ SPDLOG_INLINE void file_helper::flush()
}
}
SPDLOG_INLINE void file_helper::sync()
{
if(!os::fsync(fd_))
{
throw_spdlog_ex("Failed to fsync file " + os::filename_to_str(filename_), errno);
}
}
SPDLOG_INLINE void file_helper::close()
{
if (fd_ != nullptr)

@ -26,6 +26,7 @@ public:
void open(const filename_t &fname, bool truncate = false);
void reopen(bool truncate);
void flush();
void sync();
void close();
void write(const memory_buf_t &buf);
size_t size() const;

@ -49,7 +49,7 @@ public:
push_cv_.notify_one();
}
// try to dequeue item. if no item found. wait up to timeout and try again
// dequeue with a timeout.
// Return true, if succeeded dequeue item, false otherwise
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration)
{
@ -66,6 +66,18 @@ public:
return true;
}
// blocking dequeue without a timeout.
void dequeue(T &popped_item)
{
{
std::unique_lock<std::mutex> lock(queue_mutex_);
push_cv_.wait(lock, [this] { return !this->q_.empty(); });
popped_item = std::move(q_.front());
q_.pop_front();
}
pop_cv_.notify_one();
}
#else
// apparently mingw deadlocks if the mutex is released before cv.notify_one(),
// so release the mutex at the very end each function.
@ -87,7 +99,7 @@ public:
push_cv_.notify_one();
}
// try to dequeue item. if no item found. wait up to timeout and try again
// dequeue with a timeout.
// Return true, if succeeded dequeue item, false otherwise
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration)
{
@ -102,6 +114,16 @@ public:
return true;
}
// blocking dequeue without a timeout.
void dequeue(T &popped_item)
{
std::unique_lock<std::mutex> lock(queue_mutex_);
push_cv_.wait(lock, [this] { return !this->q_.empty(); });
popped_item = std::move(q_.front());
q_.pop_front();
pop_cv_.notify_one();
}
#endif
size_t overrun_counter()

@ -23,9 +23,10 @@
#ifdef _WIN32
# include <io.h> // _get_osfhandle and _isatty support
# include <process.h> // _get_pid support
# include <io.h> // for _get_osfhandle, _isatty, _fileno
# include <process.h> // for _get_pid
# include <spdlog/details/windows_include.h>
# include <fileapi.h> // for FlushFileBuffers
# ifdef __MINGW32__
# include <share.h>
@ -236,8 +237,8 @@ SPDLOG_INLINE size_t filesize(FILE *f)
# else
int fd = ::fileno(f);
# endif
// 64 bits(but not in osx or cygwin, where fstat64 is deprecated)
# if (defined(__linux__) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64))
// 64 bits(but not in osx, linux/musl or cygwin, where fstat64 is deprecated)
# if ((defined(__linux__) && defined(__GLIBC__)) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64))
struct stat64 st;
if (::fstat64(fd, &st) == 0)
{
@ -286,7 +287,7 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
return offset;
#else
# if defined(sun) || defined(__sun) || defined(_AIX) || (!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
# if defined(sun) || defined(__sun) || defined(_AIX) || (defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || (!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
struct helper
{
@ -601,6 +602,17 @@ std::string SPDLOG_INLINE getenv(const char *field)
#endif
}
// Do fsync by FILE handlerpointer
// Return true on success
SPDLOG_INLINE bool fsync(FILE *fp)
{
#ifdef _WIN32
return FlushFileBuffers(reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fp)))) != 0;
#else
return ::fsync(fileno(fp)) == 0;
#endif
}
} // namespace os
} // namespace details
} // namespace spdlog

@ -109,6 +109,10 @@ SPDLOG_API bool create_dir(const filename_t &path);
// return empty string if field not found
SPDLOG_API std::string getenv(const char *field);
// Do fsync by FILE objectpointer.
// Return true on success.
SPDLOG_API bool fsync(FILE * fp);
} // namespace os
} // namespace details
} // namespace spdlog

@ -287,6 +287,14 @@ SPDLOG_INLINE registry &registry::instance()
return s_instance;
}
SPDLOG_INLINE void registry::apply_logger_env_levels(std::shared_ptr<logger> new_logger)
{
std::lock_guard<std::mutex> lock(logger_map_mutex_);
auto it = log_levels_.find(new_logger->name());
auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
new_logger->set_level(new_level);
}
SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name)
{
if (loggers_.find(logger_name) != loggers_.end())

@ -91,6 +91,8 @@ public:
static registry &instance();
void apply_logger_env_levels(std::shared_ptr<logger> new_logger);
private:
registry();
~registry();

@ -16,6 +16,7 @@
#include <unistd.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <string>
@ -110,7 +111,7 @@ public:
#endif
#if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
# error "tcp_sink would raise SIGPIPE since niether SO_NOSIGPIPE nor MSG_NOSIGNAL are available"
# error "tcp_sink would raise SIGPIPE since neither SO_NOSIGPIPE nor MSG_NOSIGNAL are available"
#endif
}

@ -108,11 +108,7 @@ void SPDLOG_INLINE thread_pool::worker_loop_()
bool SPDLOG_INLINE thread_pool::process_next_msg_()
{
async_msg incoming_async_msg;
bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
if (!dequeued)
{
return true;
}
q_.dequeue(incoming_async_msg);
switch (incoming_async_msg.msg_type)
{

@ -15,9 +15,11 @@
#include <stdio.h>
#include <string>
#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;
}

@ -210,7 +210,7 @@ SPDLOG_INLINE void logger::flush_()
SPDLOG_INLINE void logger::dump_backtrace_()
{
using details::log_msg;
if (tracer_.enabled())
if (tracer_.enabled() && !tracer_.empty())
{
sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); });

@ -74,7 +74,7 @@ protected:
private:
// There might be liblog versions used, that do not support __android_log_buf_write. So we only compile and link against
// __android_log_buf_write, if user explicitely provides a non-default log buffer. Otherwise, when using the default log buffer, always
// __android_log_buf_write, if user explicitly provides a non-default log buffer. Otherwise, when using the default log buffer, always
// log via __android_log_write.
template<int ID = BufferID>
typename std::enable_if<ID == static_cast<int>(log_id::LOG_ID_MAIN), int>::type android_log(int prio, const char *tag, const char *text)

@ -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,16 +31,16 @@ public:
dist_sink(const dist_sink &) = delete;
dist_sink &operator=(const dist_sink &) = delete;
void add_sink(std::shared_ptr<sink> sink)
void add_sink(std::shared_ptr<sink> sub_sink)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
sinks_.push_back(sink);
sinks_.push_back(sub_sink);
}
void remove_sink(std::shared_ptr<sink> sink)
void remove_sink(std::shared_ptr<sink> sub_sink)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sub_sink), sinks_.end());
}
void set_sinks(std::vector<std::shared_ptr<sink>> sinks)

@ -4,6 +4,7 @@
#pragma once
#include <spdlog/sinks/base_sink.h>
#include <spdlog/details/os.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/details/synchronous_factory.h>
@ -75,11 +76,17 @@ protected:
{
// Note: function call inside '()' to avoid macro expansion
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d", syslog_level(msg.level),
#ifndef SPDLOG_NO_THREAD_ID
"TID=%zu", os::thread_id(),
#endif
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), nullptr);
}
else
{
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d", syslog_level(msg.level),
#ifndef SPDLOG_NO_THREAD_ID
"TID=%zu", os::thread_id(),
#endif
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), "CODE_FILE=%s",
msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr);
}

@ -210,7 +210,7 @@ private:
HANDLE hEventLog_{NULL};
internal::sid_t current_user_sid_;
std::string source_;
WORD event_id_;
DWORD event_id_;
HANDLE event_log_handle()
{
@ -258,7 +258,7 @@ protected:
void flush_() override {}
public:
win_eventlog_sink(std::string const &source, WORD event_id = 1000 /* according to mscoree.dll */)
win_eventlog_sink(std::string const &source, DWORD event_id = 1000 /* according to mscoree.dll */)
: source_(source)
, event_id_(event_id)
{

@ -117,4 +117,9 @@ SPDLOG_INLINE void set_default_logger(std::shared_ptr<spdlog::logger> default_lo
details::registry::instance().set_default_logger(std::move(default_logger));
}
SPDLOG_INLINE void apply_logger_env_levels(std::shared_ptr<logger> logger)
{
details::registry::instance().apply_logger_env_levels(std::move(logger));
}
} // namespace spdlog

@ -131,6 +131,15 @@ SPDLOG_API spdlog::logger *default_logger_raw();
SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger);
// Initialize logger level based on environment configs.
//
// Useful for applying SPDLOG_LEVEL to manually created loggers.
//
// Example:
// auto mylogger = std::make_shared<spdlog::logger>("mylogger", ...);
// spdlog::apply_logger_env_levels(mylogger);
SPDLOG_API void apply_logger_env_levels(std::shared_ptr<logger> logger);
template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
{

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

@ -32,4 +32,5 @@
#include "spdlog/sinks/ostream_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/msvc_sink.h"
#include "spdlog/pattern_formatter.h"

@ -31,6 +31,19 @@ TEST_CASE("bactrace1", "[bactrace]")
REQUIRE(test_sink->lines()[7] == "****************** Backtrace End ********************");
}
TEST_CASE("bactrace-empty", "[bactrace]")
{
using spdlog::sinks::test_sink_st;
auto test_sink = std::make_shared<test_sink_st>();
size_t backtrace_size = 5;
spdlog::logger logger("test-backtrace", test_sink);
logger.set_pattern("%v");
logger.enable_backtrace(backtrace_size);
logger.dump_backtrace();
REQUIRE(test_sink->lines().size() == 0);
}
TEST_CASE("bactrace-async", "[bactrace]")
{
using spdlog::sinks::test_sink_mt;

@ -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();
}

@ -43,6 +43,26 @@ TEST_CASE("dequeue-empty-wait", "[mpmc_blocking_q]")
REQUIRE(delta_ms <= wait_ms + tolerance_wait);
}
TEST_CASE("dequeue-full-nowait", "[mpmc_blocking_q]")
{
spdlog::details::mpmc_blocking_queue<int> q(1);
q.enqueue(42);
int item = 0;
q.dequeue_for(item, milliseconds::zero());
REQUIRE(item == 42);
}
TEST_CASE("dequeue-full-wait", "[mpmc_blocking_q]")
{
spdlog::details::mpmc_blocking_queue<int> q(1);
q.enqueue(42);
int item = 0;
q.dequeue(item);
REQUIRE(item == 42);
}
TEST_CASE("enqueue_nowait", "[mpmc_blocking_q]")
{
@ -95,12 +115,12 @@ TEST_CASE("full_queue", "[mpmc_blocking_q]")
for (int i = 1; i < static_cast<int>(q_size); i++)
{
int item = -1;
q.dequeue_for(item, milliseconds(0));
q.dequeue(item);
REQUIRE(item == i);
}
// last item pushed has overridden the oldest.
int item = -1;
q.dequeue_for(item, milliseconds(0));
q.dequeue(item);
REQUIRE(item == 123456);
}

Loading…
Cancel
Save