mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
Compare commits
54 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
eb3220622e | ||
![]() |
8f26e819ad | ||
![]() |
b6b1c2f95d | ||
![]() |
9ce9804a88 | ||
![]() |
ddaa61ca9a | ||
![]() |
4646bd082a | ||
![]() |
53aca9c3d0 | ||
![]() |
aa1e794213 | ||
![]() |
45e3b678b0 | ||
![]() |
bd99496423 | ||
![]() |
e471ec884e | ||
![]() |
b400705a1c | ||
![]() |
cb35191fc1 | ||
![]() |
1945a93b33 | ||
![]() |
dfd12e6dac | ||
![]() |
ba29e1d75d | ||
![]() |
8f6d123586 | ||
![]() |
d368ed586c | ||
![]() |
87095a9f1f | ||
![]() |
dd6d203488 | ||
![]() |
f463ebf54a | ||
![]() |
3547d7e24f | ||
![]() |
a9c01aba78 | ||
![]() |
f237947bdc | ||
![]() |
890df3d90b | ||
![]() |
14783585b6 | ||
![]() |
243c4beac7 | ||
![]() |
fe9cb54e0d | ||
![]() |
dabec32748 | ||
![]() |
6faa5fc95b | ||
![]() |
dbbec6cdb4 | ||
![]() |
43923cf038 | ||
![]() |
2ccba49b01 | ||
![]() |
362fdc6ceb | ||
![]() |
7bb53541e4 | ||
![]() |
c07b3aeef9 | ||
![]() |
fb47935a7b | ||
![]() |
ec3538c2ee | ||
![]() |
84e15d1ee2 | ||
![]() |
5b4c4f3f77 | ||
![]() |
aecdfc60a0 | ||
![]() |
816ede3a17 | ||
![]() |
353c79ca71 | ||
![]() |
e93115f436 | ||
![]() |
197c9639bb | ||
![]() |
a358a38b84 | ||
![]() |
16d76e2293 | ||
![]() |
536e583cbe | ||
![]() |
9049f9aeb9 | ||
![]() |
bee3e63e1b | ||
![]() |
d8f13cbd5b | ||
![]() |
0f39da5490 | ||
![]() |
28fef35a12 | ||
![]() |
1344d44a5a |
36
.travis.yml
36
.travis.yml
@@ -29,6 +29,14 @@ addons: &gcc9
|
||||
- g++-9
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
|
||||
# gcc 11.0
|
||||
addons: &gcc11
|
||||
apt:
|
||||
packages:
|
||||
- g++-11
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
|
||||
|
||||
# Clang 3.5
|
||||
@@ -50,6 +58,16 @@ addons: &clang10
|
||||
sources:
|
||||
- sourceline: "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main"
|
||||
key_url: "https://apt.llvm.org/llvm-snapshot.gpg.key"
|
||||
|
||||
addons: &clang12
|
||||
apt:
|
||||
packages:
|
||||
- clang-12
|
||||
- lldb-12
|
||||
- lld-12
|
||||
sources:
|
||||
- sourceline: "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-12 main"
|
||||
key_url: "https://apt.llvm.org/llvm-snapshot.gpg.key"
|
||||
|
||||
|
||||
matrix:
|
||||
@@ -64,11 +82,17 @@ matrix:
|
||||
os: linux
|
||||
addons: *gcc7
|
||||
|
||||
# Test gcc-9: C++17, Build=Release
|
||||
# Test gcc-9: C++17, Build=Release
|
||||
- env: GCC_VERSION=9 BUILD_TYPE=Release CPP=17
|
||||
os: linux
|
||||
addons: *gcc9
|
||||
|
||||
# Test gcc-11.0: C++20, Build=Debug
|
||||
- env: GCC_VERSION=11 BUILD_TYPE=Debug CPP=20 ASAN=Off
|
||||
os: linux
|
||||
dist: bionic
|
||||
addons: *gcc11
|
||||
|
||||
# Test clang-3.5: C++11, Build=Release
|
||||
- env: CLANG_VERSION=3.5 BUILD_TYPE=Release CPP=11
|
||||
os: linux
|
||||
@@ -89,9 +113,15 @@ matrix:
|
||||
os: linux
|
||||
dist: bionic
|
||||
addons: *clang10
|
||||
|
||||
|
||||
|
||||
|
||||
# Test clang-12.0: C++17, Build=Debug
|
||||
- env: CLANG_VERSION=12 BUILD_TYPE=Debug CPP=17 ASAN=Off
|
||||
os: linux
|
||||
dist: bionic
|
||||
addons: *clang12
|
||||
|
||||
|
||||
before_script:
|
||||
- if [ -n "$GCC_VERSION" ]; then export CXX="g++-${GCC_VERSION}" CC="gcc-${GCC_VERSION}"; fi
|
||||
- if [ -n "$CLANG_VERSION" ]; then export CXX="clang++-${CLANG_VERSION}" CC="clang-${CLANG_VERSION}"; fi
|
||||
|
@@ -307,7 +307,8 @@ if(SPDLOG_INSTALL)
|
||||
install(EXPORT spdlog DESTINATION ${export_dest_dir} NAMESPACE spdlog:: FILE ${config_targets_file})
|
||||
|
||||
include(CMakePackageConfigHelpers)
|
||||
configure_file("${project_config_in}" "${project_config_out}" @ONLY)
|
||||
configure_package_config_file("${project_config_in}" "${project_config_out}"
|
||||
INSTALL_DESTINATION ${export_dest_dir})
|
||||
|
||||
write_basic_package_version_file("${version_config_file}" COMPATIBILITY SameMajorVersion)
|
||||
install(FILES "${project_config_out}" "${version_config_file}" DESTINATION "${export_dest_dir}")
|
||||
|
@@ -4,7 +4,7 @@ Very fast, header-only/compiled, C++ logging library. [ to your build tree and use a C++11 compiler.
|
||||
Copy the include [folder](https://github.com/gabime/spdlog/tree/v1.x/include/spdlog) to your build tree and use a C++11 compiler.
|
||||
|
||||
#### Static lib version (recommended - much faster compile times)
|
||||
```console
|
||||
@@ -144,7 +144,7 @@ void daily_example()
|
||||
#### Backtrace support
|
||||
```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 nededed (e.g. when error happens).
|
||||
// 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.
|
||||
|
||||
spdlog::enable_backtrace(32); // Store the latest 32 messages in a buffer. Older messages will be dropped.
|
||||
|
@@ -1,6 +1,8 @@
|
||||
# Copyright(c) 2019 spdlog authors
|
||||
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
@PACKAGE_INIT@
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
set(SPDLOG_FMT_EXTERNAL @SPDLOG_FMT_EXTERNAL@)
|
||||
@@ -13,3 +15,5 @@ endif()
|
||||
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/${config_targets_file}")
|
||||
|
||||
check_required_components(spdlog)
|
@@ -22,7 +22,8 @@ void syslog_example();
|
||||
void custom_flags_example();
|
||||
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "spdlog/cfg/env.h" // support for loading levels from the environment variable
|
||||
#include "spdlog/cfg/env.h" // support for loading levels from the environment variable
|
||||
#include "spdlog/fmt/ostr.h" // support for user defined types
|
||||
|
||||
int main(int, char *[])
|
||||
{
|
||||
@@ -221,7 +222,6 @@ void multi_sink_example()
|
||||
}
|
||||
|
||||
// User defined types logging by implementing operator<<
|
||||
#include "spdlog/fmt/ostr.h" // must be included
|
||||
struct my_type
|
||||
{
|
||||
int i;
|
||||
|
@@ -34,7 +34,7 @@ SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG
|
||||
{
|
||||
auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name);
|
||||
if (it != std::end(level_string_views))
|
||||
return static_cast<level::level_enum>(std::distance(std::begin(level_string_views), it));
|
||||
return static_cast<level::level_enum>(it - std::begin(level_string_views));
|
||||
|
||||
// check also for "warn" and "err" before giving up..
|
||||
if (name == "warn")
|
||||
|
@@ -116,19 +116,27 @@ using wstring_view_t = fmt::basic_string_view<wchar_t>;
|
||||
using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
|
||||
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
|
||||
|
||||
template<class T>
|
||||
using remove_cvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
|
||||
|
||||
// clang doesn't like SFINAE disabled constructor in std::is_convertible<> so have to repeat the condition from basic_format_string here,
|
||||
// in addition, fmt::basic_runtime<Char> is only convertible to basic_format_string<Char> but not basic_string_view<Char>
|
||||
template<class T, class Char = char>
|
||||
struct is_convertible_to_basic_format_string
|
||||
: std::integral_constant<bool,
|
||||
std::is_convertible<T, fmt::basic_string_view<Char>>::value || std::is_same<remove_cvref_t<T>, fmt::basic_runtime<Char>>::value>
|
||||
{};
|
||||
|
||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||
# ifndef _WIN32
|
||||
# error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
|
||||
# else
|
||||
template<typename T>
|
||||
struct is_convertible_to_wstring_view : std::is_convertible<T, wstring_view_t>
|
||||
{};
|
||||
# endif // _WIN32
|
||||
#else
|
||||
template<typename>
|
||||
struct is_convertible_to_wstring_view : std::false_type
|
||||
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||
|
||||
template<class T>
|
||||
struct is_convertible_to_any_format_string : std::integral_constant<bool, is_convertible_to_basic_format_string<T, char>::value ||
|
||||
is_convertible_to_basic_format_string<T, wchar_t>::value>
|
||||
{};
|
||||
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||
|
||||
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
|
||||
using level_t = details::null_atomic_int;
|
||||
@@ -162,13 +170,13 @@ enum level_enum
|
||||
n_levels
|
||||
};
|
||||
|
||||
#define SPDLOG_LEVEL_NAME_TRACE string_view_t("trace", 5)
|
||||
#define SPDLOG_LEVEL_NAME_DEBUG string_view_t("debug", 5)
|
||||
#define SPDLOG_LEVEL_NAME_INFO string_view_t("info", 4)
|
||||
#define SPDLOG_LEVEL_NAME_WARNING string_view_t("warning", 7)
|
||||
#define SPDLOG_LEVEL_NAME_ERROR string_view_t("error", 5)
|
||||
#define SPDLOG_LEVEL_NAME_CRITICAL string_view_t("critical", 8)
|
||||
#define SPDLOG_LEVEL_NAME_OFF string_view_t("off", 3)
|
||||
#define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5)
|
||||
#define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5)
|
||||
#define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4)
|
||||
#define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7)
|
||||
#define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5)
|
||||
#define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8)
|
||||
#define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3)
|
||||
|
||||
#if !defined(SPDLOG_LEVEL_NAMES)
|
||||
# define SPDLOG_LEVEL_NAMES \
|
||||
|
@@ -55,7 +55,7 @@ inline void pad2(int n, memory_buf_t &dest)
|
||||
}
|
||||
else // unlikely, but just in case, let fmt deal with it
|
||||
{
|
||||
fmt::format_to(std::back_inserter(dest), "{:02}", n);
|
||||
fmt::format_to(std::back_inserter(dest), SPDLOG_FMT_RUNTIME("{:02}"), n);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -27,7 +27,6 @@ enum class async_msg_type
|
||||
terminate
|
||||
};
|
||||
|
||||
#include <spdlog/details/log_msg_buffer.h>
|
||||
// Async msg to move to/from the queue
|
||||
// Movable only. should never be copied
|
||||
struct async_msg : log_msg_buffer
|
||||
|
@@ -6,6 +6,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cctype>
|
||||
#include <spdlog/common.h>
|
||||
|
||||
//
|
||||
// Support for logging binary data as hex
|
||||
@@ -89,7 +90,7 @@ struct formatter<spdlog::details::dump_info<T>>
|
||||
|
||||
// parse the format string flags
|
||||
template<typename ParseContext>
|
||||
auto parse(ParseContext &ctx) -> decltype(ctx.begin())
|
||||
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin())
|
||||
{
|
||||
auto it = ctx.begin();
|
||||
while (it != ctx.end() && *it != '}')
|
||||
|
@@ -77,14 +77,14 @@ public:
|
||||
logger &operator=(logger other) SPDLOG_NOEXCEPT;
|
||||
void swap(spdlog::logger &other) SPDLOG_NOEXCEPT;
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void log(source_loc loc, level::level_enum lvl, const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
void log(source_loc loc, level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log_(loc, lvl, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void log(level::level_enum lvl, const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
void log(level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
@@ -102,6 +102,13 @@ public:
|
||||
log(loc, lvl, string_view_t{msg});
|
||||
}
|
||||
|
||||
// T cannot be statically converted to format string (including string_view)
|
||||
template<class T, typename std::enable_if<!is_convertible_to_any_format_string<const T &>::value, int>::type = 0>
|
||||
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
||||
{
|
||||
log(loc, lvl, "{}", msg);
|
||||
}
|
||||
|
||||
void log(log_clock::time_point log_time, source_loc loc, level::level_enum lvl, string_view_t msg)
|
||||
{
|
||||
bool log_enabled = should_log(lvl);
|
||||
@@ -133,51 +140,92 @@ public:
|
||||
log(source_loc{}, lvl, msg);
|
||||
}
|
||||
|
||||
// T cannot be statically converted to string_view or wstring_view
|
||||
template<class T, typename std::enable_if<!std::is_convertible<const T &, spdlog::string_view_t>::value &&
|
||||
!is_convertible_to_wstring_view<const T &>::value,
|
||||
int>::type = 0>
|
||||
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
||||
{
|
||||
log(loc, lvl, "{}", msg);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void trace(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
void trace(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::trace, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void debug(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
void debug(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::debug, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void info(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
void info(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::info, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void warn(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
void warn(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::warn, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void error(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
void error(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::err, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
void critical(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
void critical(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::critical, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||
template<typename... Args>
|
||||
void log(level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void log(source_loc loc, level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log_(loc, lvl, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void trace(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::trace, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void debug(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::debug, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void info(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::info, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void warn(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::warn, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void error(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::err, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void critical(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
log(level::critical, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename T>
|
||||
void trace(const T &msg)
|
||||
{
|
||||
@@ -269,9 +317,8 @@ protected:
|
||||
details::backtracer tracer_;
|
||||
|
||||
// common implementation for after templated public api has been resolved
|
||||
template<typename FormatString, typename... Args, typename Char = fmt::char_t<FormatString>,
|
||||
typename std::enable_if<!std::is_same<Char, wchar_t>::value, Char>::type * = nullptr>
|
||||
void log_(source_loc loc, level::level_enum lvl, const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
void log_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&...args)
|
||||
{
|
||||
bool log_enabled = should_log(lvl);
|
||||
bool traceback_enabled = tracer_.enabled();
|
||||
@@ -282,7 +329,7 @@ protected:
|
||||
SPDLOG_TRY
|
||||
{
|
||||
memory_buf_t buf;
|
||||
fmt::detail::vformat_to(buf, fmt::to_string_view(fmt), fmt::make_format_args(args...));
|
||||
fmt::detail::vformat_to(buf, fmt, fmt::make_format_args(args...));
|
||||
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
|
||||
log_it_(log_msg, log_enabled, traceback_enabled);
|
||||
}
|
||||
@@ -291,7 +338,7 @@ protected:
|
||||
|
||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||
template<typename... Args>
|
||||
void log_(source_loc loc, level::level_enum lvl, const wstring_view_t &fmt, Args &&...args)
|
||||
void log_(source_loc loc, level::level_enum lvl, wstring_view_t fmt, Args &&...args)
|
||||
{
|
||||
bool log_enabled = should_log(lvl);
|
||||
bool traceback_enabled = tracer_.enabled();
|
||||
@@ -303,7 +350,7 @@ protected:
|
||||
{
|
||||
// format to wmemory_buffer and convert to utf8
|
||||
fmt::wmemory_buffer wbuf;
|
||||
fmt::format_to(std::back_inserter(wbuf), fmt, std::forward<Args>(args)...);
|
||||
fmt::detail::vformat_to(wbuf, fmt, fmt::make_format_args<fmt::wformat_context>(args...));
|
||||
memory_buf_t buf;
|
||||
details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf);
|
||||
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
|
||||
|
@@ -33,7 +33,7 @@ public:
|
||||
{
|
||||
try
|
||||
{
|
||||
client_ = std::make_unique<mongocxx::client>(mongocxx::uri{uri});
|
||||
client_ = spdlog::details::make_unique<mongocxx::client>(mongocxx::uri{uri});
|
||||
db_name_ = db_name;
|
||||
coll_name_ = collection_name;
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@
|
||||
#pragma once
|
||||
|
||||
//
|
||||
// Custom sink for QPlainTextEdit or QTextEdit and its children(QTextBrowser...
|
||||
// Custom sink for QPlainTextEdit or QTextEdit and its childs(QTextBrowser...
|
||||
// etc) Building and using requires Qt library.
|
||||
//
|
||||
|
||||
@@ -13,188 +13,81 @@
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QTextEdit>
|
||||
|
||||
namespace _spdlog_p {
|
||||
namespace _sinks_p {
|
||||
//
|
||||
// Private class for QTextEdit and its derivatives
|
||||
//
|
||||
class qtextedit_sink_p : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
qtextedit_sink_p(QTextEdit *textedit = nullptr)
|
||||
{
|
||||
if (textedit != nullptr)
|
||||
{
|
||||
textedit_ = textedit;
|
||||
connect(this, &qtextedit_sink_p::append_text, textedit_, &QTextEdit::append);
|
||||
}
|
||||
}
|
||||
|
||||
~qtextedit_sink_p() {}
|
||||
|
||||
void append(const spdlog::string_view_t &str)
|
||||
{
|
||||
emit append_text(QString::fromUtf8(str.data(), static_cast<int>(str.size() - 2)));
|
||||
}
|
||||
|
||||
signals:
|
||||
void append_text(const QString &);
|
||||
|
||||
private:
|
||||
QTextEdit *textedit_ = nullptr;
|
||||
};
|
||||
#include <QPlainTextEdit>
|
||||
|
||||
//
|
||||
// Private class for QPlainTextEdit
|
||||
//
|
||||
class qplaintextedit_sink_p : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
qplaintextedit_sink_p(QPlainTextEdit *textedit = nullptr)
|
||||
{
|
||||
if (textedit != nullptr)
|
||||
{
|
||||
textedit_ = textedit;
|
||||
connect(this, &qplaintextedit_sink_p::append_text, textedit_, &QPlainTextEdit::appendPlainText);
|
||||
}
|
||||
}
|
||||
|
||||
~qplaintextedit_sink_p() {}
|
||||
|
||||
void append(const spdlog::string_view_t &str)
|
||||
{
|
||||
emit append_text(QString::fromUtf8(str.data(), static_cast<int>(str.size() - 2)));
|
||||
}
|
||||
|
||||
signals:
|
||||
void append_text(const QString &);
|
||||
|
||||
private:
|
||||
QPlainTextEdit *textedit_ = nullptr;
|
||||
};
|
||||
} // namespace _sinks_p
|
||||
} // namespace _spdlog_p
|
||||
|
||||
//
|
||||
// qtextedit_sink class
|
||||
// qt_sink class
|
||||
//
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
template<typename Mutex>
|
||||
class qtextedit_sink : public base_sink<Mutex>
|
||||
{
|
||||
template <typename Mutex> class qt_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
qtextedit_sink(QTextEdit *textedit = nullptr)
|
||||
{
|
||||
if (textedit != nullptr)
|
||||
{
|
||||
textedit_p = std::make_shared<_spdlog_p::_sinks_p::qtextedit_sink_p>(textedit);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw spdlog_ex("Error opening QTextEdit");
|
||||
}
|
||||
}
|
||||
qt_sink(QObject *qt_object = nullptr, const std::string &meta_method = "") {
|
||||
qt_object_ = qt_object;
|
||||
meta_method_ = meta_method;
|
||||
}
|
||||
|
||||
~qtextedit_sink()
|
||||
{
|
||||
flush_();
|
||||
}
|
||||
~qt_sink() { flush_(); }
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
memory_buf_t formatted;
|
||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
string_view_t str_v = string_view_t(formatted.data(), formatted.size());
|
||||
textedit_p->append(str_v);
|
||||
}
|
||||
void sink_it_(const details::log_msg &msg) override {
|
||||
memory_buf_t formatted;
|
||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
string_view_t str = string_view_t(formatted.data(), formatted.size());
|
||||
QMetaObject::invokeMethod(qt_object_, meta_method_.c_str(), Qt::AutoConnection,
|
||||
Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size())).trimmed()));
|
||||
}
|
||||
|
||||
void flush_() override {}
|
||||
void flush_() override {}
|
||||
|
||||
private:
|
||||
std::shared_ptr<_spdlog_p::_sinks_p::qtextedit_sink_p> textedit_p = nullptr;
|
||||
};
|
||||
|
||||
//
|
||||
// qplaintextedit_sink class
|
||||
//
|
||||
template<typename Mutex>
|
||||
class qplaintextedit_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
qplaintextedit_sink(QPlainTextEdit *textedit = nullptr)
|
||||
{
|
||||
if (textedit != nullptr)
|
||||
{
|
||||
textedit_p = std::make_shared<_spdlog_p::_sinks_p::qplaintextedit_sink_p>(textedit);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw spdlog_ex("Error opening QPlainTextEdit");
|
||||
}
|
||||
}
|
||||
|
||||
~qplaintextedit_sink()
|
||||
{
|
||||
flush_();
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
memory_buf_t formatted;
|
||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
string_view_t str_v = string_view_t(formatted.data(), formatted.size());
|
||||
textedit_p->append(str_v);
|
||||
}
|
||||
|
||||
void flush_() override {}
|
||||
|
||||
private:
|
||||
std::shared_ptr<_spdlog_p::_sinks_p::qplaintextedit_sink_p> textedit_p = nullptr;
|
||||
QObject *qt_object_ = nullptr;
|
||||
std::string meta_method_;
|
||||
};
|
||||
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include <mutex>
|
||||
using qtextedit_sink_mt = qtextedit_sink<std::mutex>;
|
||||
using qtextedit_sink_st = qtextedit_sink<spdlog::details::null_mutex>;
|
||||
|
||||
using qplaintextedit_sink_mt = qplaintextedit_sink<std::mutex>;
|
||||
using qplaintextedit_sink_st = qplaintextedit_sink<spdlog::details::null_mutex>;
|
||||
|
||||
using qt_sink_mt = qt_sink<std::mutex>;
|
||||
using qt_sink_st = qt_sink<spdlog::details::null_mutex>;
|
||||
} // namespace sinks
|
||||
|
||||
//
|
||||
// Factory functions
|
||||
//
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> qtextedit_logger_mt(const std::string &logger_name, QTextEdit *qtextedit = nullptr)
|
||||
{
|
||||
return Factory::template create<sinks::qtextedit_sink_mt>(logger_name, qtextedit);
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_logger_mt(const std::string &logger_name, QTextEdit* qt_object, const std::string &meta_method = "append") {
|
||||
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
||||
}
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> qtextedit_logger_st(const std::string &logger_name, QTextEdit *qtextedit = nullptr)
|
||||
{
|
||||
return Factory::template create<sinks::qtextedit_sink_st>(logger_name, qtextedit);
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_logger_st(const std::string &logger_name, QTextEdit* qt_object, const std::string &meta_method = "append") {
|
||||
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
||||
}
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> qplaintextedit_logger_mt(const std::string &logger_name, QPlainTextEdit *qplaintextedit = nullptr)
|
||||
{
|
||||
return Factory::template create<sinks::qplaintextedit_sink_mt>(logger_name, qplaintextedit);
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_logger_mt(const std::string &logger_name, QPlainTextEdit* qt_object , const std::string &meta_method = "appendPlainText") {
|
||||
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
||||
}
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> qplaintextedit_logger_st(const std::string &logger_name, QPlainTextEdit *qplaintextedit = nullptr)
|
||||
{
|
||||
return Factory::template create<sinks::qplaintextedit_sink_st>(logger_name, qplaintextedit);
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_logger_st(const std::string &logger_name, QPlainTextEdit* qt_object, const std::string &meta_method = "appendPlainText") {
|
||||
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_logger_mt(const std::string &logger_name, QObject* qt_object, const std::string &meta_method) {
|
||||
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_logger_st(const std::string &logger_name, QObject* qt_object, const std::string &meta_method) {
|
||||
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@@ -127,50 +127,50 @@ SPDLOG_API spdlog::logger *default_logger_raw();
|
||||
|
||||
SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger);
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
inline void log(source_loc source, level::level_enum lvl, const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
inline void log(source_loc source, level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
inline void log(level::level_enum lvl, const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
inline void log(level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
inline void trace(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
inline void trace(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
inline void debug(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
inline void debug(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
inline void info(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
inline void info(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->info(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
inline void warn(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
inline void warn(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
inline void error(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
inline void error(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->error(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename FormatString, typename... Args>
|
||||
inline void critical(const FormatString &fmt, Args &&...args)
|
||||
template<typename... Args>
|
||||
inline void critical(fmt::format_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
@@ -187,6 +187,56 @@ inline void log(level::level_enum lvl, const T &msg)
|
||||
default_logger_raw()->log(lvl, msg);
|
||||
}
|
||||
|
||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||
template<typename... Args>
|
||||
inline void log(source_loc source, level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void log(level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void trace(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void debug(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void info(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->info(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void warn(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void error(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->error(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void critical(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||
{
|
||||
default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename T>
|
||||
inline void trace(const T &msg)
|
||||
{
|
||||
|
@@ -5,6 +5,6 @@
|
||||
|
||||
#define SPDLOG_VER_MAJOR 1
|
||||
#define SPDLOG_VER_MINOR 9
|
||||
#define SPDLOG_VER_PATCH 0
|
||||
#define SPDLOG_VER_PATCH 2
|
||||
|
||||
#define SPDLOG_VERSION (SPDLOG_VER_MAJOR * 10000 + SPDLOG_VER_MINOR * 100 + SPDLOG_VER_PATCH)
|
||||
|
@@ -29,7 +29,7 @@ TEST_CASE("default_error_handler", "[errors]]")
|
||||
|
||||
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
|
||||
logger->set_pattern("%v");
|
||||
logger->info("Test message {} {}", 1);
|
||||
logger->info(fmt::runtime("Test message {} {}"), 1);
|
||||
logger->info("Test message {}", 2);
|
||||
logger->flush();
|
||||
|
||||
@@ -49,7 +49,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
logger->info("Good message #1");
|
||||
|
||||
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
|
||||
REQUIRE_THROWS_AS(logger->info(fmt::runtime("Bad format msg {} {}"), "xxx"), custom_ex);
|
||||
logger->info("Good message #2");
|
||||
require_message_count(SIMPLE_LOG, 2);
|
||||
}
|
||||
@@ -88,7 +88,7 @@ TEST_CASE("async_error_handler", "[errors]]")
|
||||
ofs << err_msg;
|
||||
});
|
||||
logger->info("Good message #1");
|
||||
logger->info("Bad format msg {} {}", "xxx");
|
||||
logger->info(fmt::runtime("Bad format msg {} {}"), "xxx");
|
||||
logger->info("Good message #2");
|
||||
spdlog::drop("logger"); // force logger to drain the queue and shutdown
|
||||
}
|
||||
|
@@ -95,4 +95,4 @@ TEST_CASE("wchar_api", "[stdout]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user