Compare commits

..

54 Commits

Author SHA1 Message Date
Gabi Melman
eb3220622e Bump version to 1.9.2 2021-08-12 14:10:50 +03:00
Gabi Melman
8f26e819ad Merge pull request #2036 from madeso/v1.x
The install instructions for "header only" refers to the wrong folder
2021-08-12 13:02:44 +03:00
Gabi Melman
b6b1c2f95d Update .travis.yml 2021-08-10 22:09:30 +03:00
Gabi Melman
9ce9804a88 Update .travis.yml 2021-08-10 22:05:54 +03:00
Gabi Melman
ddaa61ca9a Revert changes 2021-08-10 16:53:22 +03:00
Gabi Melman
4646bd082a Update rotating_file_sink-inl.h 2021-08-10 15:41:03 +03:00
Gabi Melman
53aca9c3d0 C++20 support 2021-08-10 14:17:20 +03:00
Gabi Melman
aa1e794213 Update .travis.yml 2021-08-10 12:37:13 +03:00
Gabi Melman
45e3b678b0 Merge pull request #2037 from dkavolis/v1.x
Fix #2034
2021-08-09 20:07:45 +03:00
Gabi Melman
bd99496423 Merge pull request #2035 from dmerkushov/v1.x
bin_to_hex.h: include spdlog.h
2021-08-09 20:04:18 +03:00
dkavolis
e471ec884e remove conditional is_convertible_* structs for wide chars 2021-08-09 17:33:00 +01:00
Dmitriy Merkushov
b400705a1c bin_to_hex.h: include common.h instead of spdlog.h 2021-08-09 19:27:24 +03:00
dkavolis
cb35191fc1 clang is acting weird with disabled constructors 2021-08-09 09:59:57 +01:00
Gustav
1945a93b33 chore: the link points to the include, make sure the text reflect this 2021-08-09 09:36:11 +02:00
Dmitriy Merkushov
dfd12e6dac bin_to_hex.h: include spdlog.h to support inclusion of bin_to_hex.h in any order with spdlog.h 2021-08-07 01:50:09 +03:00
Gabi Melman
ba29e1d75d Merge pull request #2030 from neheb/v1.x
remove std::distance usage
2021-08-05 09:37:32 +03:00
Rosen Penev
8f6d123586 remove std::distance usage
std::distance internally runs a loop, which may or may not be optimized
away. Just use simple arithmetic.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-04 17:07:18 -07:00
Gabi Melman
d368ed586c Merge pull request #2029 from daverigby/relocatable_export
Ensure exported package is relocatable
2021-08-04 20:02:52 +03:00
Dave Rigby
87095a9f1f Ensure exported package is relocatable
As per CMake's Importing and Exporting Guide[1],
configure_package_config_file() should be used for configuring the
package configuration file, not the regular configure_file() function.

This ensures that a spdlog package built on one system (with a given
directory tree) can be imported from a different system -
e.g. creating a pre-compiled spdlog package for use on different
systems.

[1]: https://cmake.org/cmake/help/git-stage/guide/importing-exporting/index.html#id8
2021-08-04 15:34:27 +01:00
Gabi Melman
dd6d203488 Merge pull request #2026 from hbwang15/feature/include_twice_fix
fix include file twice in the same file
2021-08-03 11:35:51 +03:00
wanghengbing
f463ebf54a fix include file twice in the same file 2021-08-03 11:36:12 +08:00
Gabi Melman
3547d7e24f Merge pull request #2025 from jabartek/mongo_make_unique
Removal of C++14-specific std::make_unique from mongo_sink.h
2021-08-02 15:24:02 +03:00
Bartlomiej Janowski
a9c01aba78 Changed mongo_sink.h so that it does not use C++14-specific std::make_unique 2021-08-02 13:38:59 +02:00
Gabi Melman
f237947bdc Merge pull request #2024 from p-ranav/patch-1
Fixed typo in README
2021-07-30 17:33:10 +03:00
Pranav
890df3d90b Fixed typo 2021-07-29 21:26:53 -05:00
Gabi Melman
14783585b6 Fix #2022 2021-07-29 10:09:52 +03:00
Gabi Melman
243c4beac7 Merge pull request #2018 from mguludag/v1.x
Added common class for all qt objects
2021-07-28 22:51:15 +03:00
Muhammed Galib Uludag
fe9cb54e0d Added factory function overloads for QTextEdit, QPlainTextEdit and QObject
Added factory funtion overloads for QTextEdit, QPlainTextEdit and QObject objects
cleaned qt_sink ctor
2021-07-28 22:35:09 +03:00
Muhammed Galib Uludag
dabec32748 Added common class for all qt objects
Removed separate class for qt_sinks and also send logs to any custom qt (QObject) classes (QML, QFile, custom Widget etc.)
2021-07-28 16:23:43 +03:00
Gabi Melman
6faa5fc95b Update to version 1.9.1 2021-07-28 15:30:08 +03:00
Gabi Melman
dbbec6cdb4 Merge pull request #2016 from mguludag/v1.x
Simplified Qt sinks
2021-07-28 02:08:10 +03:00
Muhammed Galib Uludag
43923cf038 Merge branch 'v1.x' into v1.x 2021-07-28 00:48:48 +03:00
Muhammed Galib Uludag
2ccba49b01 removed nullptr checks and renamed member vars 2021-07-28 00:06:12 +03:00
Muhammed Galib Uludag
362fdc6ceb trim newline chars instead of remove 2 chars 2021-07-27 23:42:00 +03:00
Gabi Melman
7bb53541e4 Merge pull request #2015 from MadMax411/change-qt-sink-delete-newline-chars
Trim the newline-chars instead of removing of 2 chars
2021-07-27 23:15:59 +03:00
Muhammed Galib Uludag
c07b3aeef9 Simplified Qt sinks
Removed private class that derived from QObject
2021-07-27 23:05:24 +03:00
Muhammed Galib Uludag
fb47935a7b Delete qt_sinks .h 2021-07-27 23:04:26 +03:00
Muhammed Galib Uludag
ec3538c2ee Simplified Qt sinks
Removed private class that derived from QObject
2021-07-27 22:55:43 +03:00
Markus Neugebauer
84e15d1ee2 Trim the newline-chars instead of removing of 2 chars 2021-07-27 21:45:34 +02:00
Gabi Melman
5b4c4f3f77 Merge pull request #2011 from sjanel/bugfix/fixclangcompilation
Fix compilation error in clang 13 in C++20 mode - ambiguous call to log function
2021-07-27 12:26:34 +03:00
Stephane Janel
aecdfc60a0 Fix comment and clang-format 2021-07-27 09:19:02 +02:00
Gabi Melman
816ede3a17 Revert e93115f436 because won't compile under mscv 2017 2021-07-27 02:43:54 +03:00
Gabi Melman
353c79ca71 Update .travis.yml 2021-07-27 01:49:36 +03:00
Gabi Melman
e93115f436 Fixed compile under c++20 and clang 12 2021-07-27 01:44:52 +03:00
Gabi Melman
197c9639bb Fixed compile under c++20 and clang 12 2021-07-27 01:37:15 +03:00
Gabi Melman
a358a38b84 Update .travis.yml 2021-07-27 01:31:34 +03:00
Gabi Melman
16d76e2293 Update .travis.yml 2021-07-27 01:29:36 +03:00
Gabi Melman
536e583cbe Merge pull request #2013 from gabime/pr-2011
Fix ambiguous error in clang13 and c++20
2021-07-27 01:12:48 +03:00
gabime
9049f9aeb9 Fix ambiguous error in clang13 and c++20 2021-07-27 00:26:32 +03:00
Gabi Melman
bee3e63e1b Merge pull request #2008 from dkavolis/v1.x
Enable format string compile time validation
2021-07-22 19:54:14 +03:00
dkavolis
d8f13cbd5b replace FormatString template argument with fmt::basic_format_string 2021-07-22 16:23:56 +01:00
gabime
0f39da5490 Updated example 2021-07-22 12:44:41 +03:00
Gabi Melman
28fef35a12 Merge pull request #2010 from dkavolis/fmt_string
Check args on formatting
2021-07-22 02:32:28 +03:00
dkavolis
1344d44a5a check args on formatting 2021-07-21 23:54:11 +01:00
17 changed files with 269 additions and 236 deletions

View File

@@ -30,6 +30,14 @@ addons: &gcc9
sources:
- ubuntu-toolchain-r-test
# gcc 11.0
addons: &gcc11
apt:
packages:
- g++-11
sources:
- ubuntu-toolchain-r-test
# Clang 3.5
addons: &clang35
@@ -51,6 +59,16 @@ addons: &clang10
- 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:
include:
@@ -69,6 +87,12 @@ matrix:
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
@@ -91,6 +115,12 @@ matrix:
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

View File

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

View File

@@ -4,7 +4,7 @@ Very fast, header-only/compiled, C++ logging library. [![Build Status](https://t
## Install
#### Header only version
Copy the source [folder](https://github.com/gabime/spdlog/tree/v1.x/include/spdlog) 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.

View File

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

View File

@@ -23,6 +23,7 @@ void custom_flags_example();
#include "spdlog/spdlog.h"
#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;

View File

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

View File

@@ -116,20 +116,28 @@ 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>
{};
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
using level_t = details::null_atomic_int;
#else
@@ -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 \

View File

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

View File

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

View File

@@ -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 != '}')

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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