clang format

This commit is contained in:
gabime 2023-06-07 13:23:44 +03:00
parent 31cefdce79
commit 1f61f5e019
25 changed files with 291 additions and 306 deletions

View File

@ -181,11 +181,12 @@ 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}>")
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(spdlog PUBLIC Threads::Threads)
spdlog_enable_warnings(spdlog)
set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION ${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR})
set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION
${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR})
set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX d)
if(COMMAND target_precompile_headers AND SPDLOG_ENABLE_PCH)
@ -199,8 +200,9 @@ endif()
add_library(spdlog_header_only INTERFACE)
add_library(spdlog::spdlog_header_only ALIAS spdlog_header_only)
target_include_directories(spdlog_header_only ${SPDLOG_INCLUDES_LEVEL} INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
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)
# ---------------------------------------------------------------------------------------
@ -323,12 +325,12 @@ if(SPDLOG_INSTALL)
# ---------------------------------------------------------------------------------------
# Install pkg-config file
# ---------------------------------------------------------------------------------------
if (IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
set(PKG_CONFIG_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}")
else()
set(PKG_CONFIG_INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
endif()
if (IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
set(PKG_CONFIG_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
else()
set(PKG_CONFIG_LIBDIR "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
@ -342,15 +344,11 @@ if(SPDLOG_INSTALL)
# ---------------------------------------------------------------------------------------
# Install CMake config files
# ---------------------------------------------------------------------------------------
export(
TARGETS spdlog
NAMESPACE spdlog::
FILE "${CMAKE_CURRENT_BINARY_DIR}/${config_targets_file}")
export(TARGETS spdlog NAMESPACE spdlog:: FILE "${CMAKE_CURRENT_BINARY_DIR}/${config_targets_file}")
install(EXPORT spdlog DESTINATION ${export_dest_dir} NAMESPACE spdlog:: FILE ${config_targets_file})
include(CMakePackageConfigHelpers)
configure_package_config_file("${project_config_in}" "${project_config_out}"
INSTALL_DESTINATION ${export_dest_dir})
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

@ -116,9 +116,6 @@ int main(int argc, char *argv[])
tracing_null_logger_st->enable_backtrace(64);
benchmark::RegisterBenchmark("null_sink_st/backtrace", bench_logger, tracing_null_logger_st);
#ifdef __linux
bench_dev_null();
#endif // __linux__

View File

@ -142,7 +142,7 @@ void daily_example()
void callback_example()
{
// Create the logger
auto logger = spdlog::callback_logger_mt("custom_callback_logger", [](const spdlog::details::log_msg &/*msg*/) {
auto logger = spdlog::callback_logger_mt("custom_callback_logger", [](const spdlog::details::log_msg & /*msg*/) {
// do what you need to do with msg
});
}

View File

@ -35,7 +35,7 @@ template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
struct async_factory_impl
{
template<typename Sink, typename... SinkArgs>
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&... args)
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args)
{
auto &registry_inst = details::registry::instance();
@ -61,13 +61,13 @@ using async_factory = async_factory_impl<async_overflow_policy::block>;
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&... sink_args)
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&...sink_args)
{
return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
}
template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&... sink_args)
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&...sink_args)
{
return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
}

View File

@ -24,37 +24,27 @@ SPDLOG_INLINE spdlog::async_logger::async_logger(
{}
// send the log message to the thread pool
SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg)
SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
}
else
{
SPDLOG_TRY
{
if (auto pool_ptr = thread_pool_.lock())
{
pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
}
else
{
throw_spdlog_ex("async log: thread pool doesn't exist anymore");
}
}
SPDLOG_LOGGER_CATCH(msg.source)
throw_spdlog_ex("async log: thread pool doesn't exist anymore");
}
}
SPDLOG_LOGGER_CATCH(msg.source)
}
// send flush request to the thread pool
SPDLOG_INLINE void spdlog::async_logger::flush_()
SPDLOG_INLINE void spdlog::async_logger::flush_(){
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){pool_ptr->post_flush(shared_from_this(), overflow_policy_);
}
else
{
SPDLOG_TRY
{
if (auto pool_ptr = thread_pool_.lock())
{
pool_ptr->post_flush(shared_from_this(), overflow_policy_);
}
else
{
throw_spdlog_ex("async flush: thread pool doesn't exist anymore");
}
}
SPDLOG_LOGGER_CATCH(source_loc())
throw_spdlog_ex("async flush: thread pool doesn't exist anymore");
}
}
SPDLOG_LOGGER_CATCH(source_loc())
}
//

View File

@ -107,7 +107,8 @@
# define SPDLOG_TRY try
# define SPDLOG_THROW(ex) throw(ex)
# define SPDLOG_CATCH_STD \
catch (const std::exception &) {}
catch (const std::exception &) \
{}
#endif
namespace spdlog {
@ -173,12 +174,12 @@ using format_string_t = fmt::format_string<Args...>;
template<class T>
using remove_cvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
template <typename Char>
#if FMT_VERSION >= 90101
template<typename Char>
# if FMT_VERSION >= 90101
using fmt_runtime_string = fmt::runtime_format_string<Char>;
#else
# else
using fmt_runtime_string = fmt::basic_runtime<Char>;
#endif
# endif
// 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>
@ -391,7 +392,7 @@ template<bool B, class T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&... args)
std::unique_ptr<T> make_unique(Args &&...args)
{
static_assert(!std::is_array<T>::value, "arrays not supported");
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));

View File

@ -92,7 +92,7 @@ SPDLOG_INLINE void file_helper::flush()
SPDLOG_INLINE void file_helper::sync()
{
if(!os::fsync(fd_))
if (!os::fsync(fd_))
{
throw_spdlog_ex("Failed to fsync file " + os::filename_to_str(filename_), errno);
}

View File

@ -292,7 +292,8 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
return offset;
#else
# if defined(sun) || defined(__sun) || defined(_AIX) || (defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || (!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
{
@ -362,15 +363,18 @@ SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT
// There is no pthread_threadid_np prior to 10.6, and it is not supported on any PPC,
// including 10.6.8 Rosetta. __POWERPC__ is Apple-specific define encompassing ppc and ppc64.
# if (MAC_OS_X_VERSION_MAX_ALLOWED < 1060) || defined(__POWERPC__)
tid = pthread_mach_thread_np(pthread_self());
tid = pthread_mach_thread_np(pthread_self());
# elif MAC_OS_X_VERSION_MIN_REQUIRED < 1060
if (&pthread_threadid_np) {
pthread_threadid_np(nullptr, &tid);
} else {
tid = pthread_mach_thread_np(pthread_self());
}
# else
if (&pthread_threadid_np)
{
pthread_threadid_np(nullptr, &tid);
}
else
{
tid = pthread_mach_thread_np(pthread_self());
}
# else
pthread_threadid_np(nullptr, &tid);
# endif
return static_cast<size_t>(tid);
#else // Default to standard C++11 (other Unix)
@ -525,7 +529,7 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target)
{
target.resize(result_size);
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, target.data(), result_size);
if (result_size > 0)
if (result_size > 0)
{
assert(result_size == target.size());
return;

View File

@ -111,7 +111,7 @@ SPDLOG_API std::string getenv(const char *field);
// Do fsync by FILE objectpointer.
// Return true on success.
SPDLOG_API bool fsync(FILE * fp);
SPDLOG_API bool fsync(FILE *fp);
} // namespace os
} // namespace details

View File

@ -13,7 +13,7 @@ class logger;
struct synchronous_factory
{
template<typename Sink, typename... SinkArgs>
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... args)
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...args)
{
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink));

View File

@ -16,9 +16,9 @@
#include <string>
#if defined(_MSC_VER)
# pragma comment(lib, "Ws2_32.lib")
# pragma comment(lib, "Mswsock.lib")
# pragma comment(lib, "AdvApi32.lib")
# pragma comment(lib, "Ws2_32.lib")
# pragma comment(lib, "Mswsock.lib")
# pragma comment(lib, "AdvApi32.lib")
#endif
namespace spdlog {

View File

@ -85,13 +85,13 @@ public:
void swap(spdlog::logger &other) SPDLOG_NOEXCEPT;
template<typename... Args>
void log(source_loc loc, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
void log(source_loc loc, level::level_enum lvl, format_string_t<Args...> fmt, Args &&...args)
{
log_(loc, lvl, details::to_string_view(fmt), std::forward<Args>(args)...);
}
template<typename... Args>
void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&...args)
{
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}
@ -141,50 +141,50 @@ public:
}
template<typename... Args>
void trace(format_string_t<Args...> fmt, Args &&... args)
void trace(format_string_t<Args...> fmt, Args &&...args)
{
log(level::trace, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void debug(format_string_t<Args...> fmt, Args &&... args)
void debug(format_string_t<Args...> fmt, Args &&...args)
{
log(level::debug, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void info(format_string_t<Args...> fmt, Args &&... args)
void info(format_string_t<Args...> fmt, Args &&...args)
{
log(level::info, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void warn(format_string_t<Args...> fmt, Args &&... args)
void warn(format_string_t<Args...> fmt, Args &&...args)
{
log(level::warn, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void error(format_string_t<Args...> fmt, Args &&... args)
void error(format_string_t<Args...> fmt, Args &&...args)
{
log(level::err, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void critical(format_string_t<Args...> fmt, Args &&... args)
void critical(format_string_t<Args...> fmt, Args &&...args)
{
log(level::critical, fmt, std::forward<Args>(args)...);
}
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args>
void log(source_loc loc, level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
void log(source_loc loc, level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&...args)
{
log_(loc, lvl, details::to_string_view(fmt), std::forward<Args>(args)...);
}
template<typename... Args>
void log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
void log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&...args)
{
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}
@ -225,37 +225,37 @@ public:
}
template<typename... Args>
void trace(wformat_string_t<Args...> fmt, Args &&... args)
void trace(wformat_string_t<Args...> fmt, Args &&...args)
{
log(level::trace, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void debug(wformat_string_t<Args...> fmt, Args &&... args)
void debug(wformat_string_t<Args...> fmt, Args &&...args)
{
log(level::debug, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void info(wformat_string_t<Args...> fmt, Args &&... args)
void info(wformat_string_t<Args...> fmt, Args &&...args)
{
log(level::info, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void warn(wformat_string_t<Args...> fmt, Args &&... args)
void warn(wformat_string_t<Args...> fmt, Args &&...args)
{
log(level::warn, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void error(wformat_string_t<Args...> fmt, Args &&... args)
void error(wformat_string_t<Args...> fmt, Args &&...args)
{
log(level::err, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void critical(wformat_string_t<Args...> fmt, Args &&... args)
void critical(wformat_string_t<Args...> fmt, Args &&...args)
{
log(level::critical, fmt, std::forward<Args>(args)...);
}
@ -357,7 +357,7 @@ protected:
// common implementation for after templated public api has been resolved
template<typename... Args>
void log_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&... 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();
@ -382,7 +382,7 @@ protected:
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args>
void log_(source_loc loc, level::level_enum lvl, 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();
@ -394,8 +394,7 @@ protected:
{
// format to wmemory_buffer and convert to utf8
wmemory_buf_t wbuf;
fmt_lib::vformat_to(
std::back_inserter(wbuf), fmt, fmt_lib::make_format_args<fmt_lib::wformat_context>(args...));
fmt_lib::vformat_to(std::back_inserter(wbuf), fmt, fmt_lib::make_format_args<fmt_lib::wformat_context>(args...));
memory_buf_t buf;
details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf);

View File

@ -92,7 +92,7 @@ public:
void format(const details::log_msg &msg, memory_buf_t &dest) override;
template<typename T, typename... Args>
pattern_formatter &add_flag(char flag, Args &&... args)
pattern_formatter &add_flag(char flag, Args &&...args)
{
custom_handlers_[flag] = details::make_unique<T>(std::forward<Args>(args)...);
return *this;

View File

@ -52,7 +52,7 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg
// before color range
print_range_(formatted, 0, msg.color_range_start);
// in color range
print_ccode_(colors_[static_cast<size_t>(msg.level)]);
print_ccode_(colors_.at(static_cast<size_t>(msg.level)));
print_range_(formatted, msg.color_range_start, msg.color_range_end);
print_ccode_(reset);
// after color range

View File

@ -50,12 +50,12 @@ struct daily_filename_format_calculator
static filename_t calc_filename(const filename_t &file_path, const tm &now_tm)
{
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
std::wstringstream stream;
std::wstringstream stream;
#else
std::stringstream stream;
std::stringstream stream;
#endif
stream << std::put_time(&now_tm, file_path.c_str());
return stream.str();
stream << std::put_time(&now_tm, file_path.c_str());
return stream.str();
}
};

View File

@ -28,12 +28,12 @@ struct kafka_sink_config
{
std::string server_addr;
std::string produce_topic;
int32_t flush_timeout_ms = 1000;
int32_t flush_timeout_ms = 1000;
kafka_sink_config(std::string addr, std::string topic, int flush_timeout_ms = 1000)
: server_addr{std::move(addr)}
,produce_topic{std::move(topic)}
,flush_timeout_ms(flush_timeout_ms)
: server_addr{std::move(addr)}
, produce_topic{std::move(topic)}
, flush_timeout_ms(flush_timeout_ms)
{}
};
@ -42,8 +42,8 @@ class kafka_sink : public base_sink<Mutex>
{
public:
kafka_sink(kafka_sink_config config)
: config_{std::move(config)}
{
: config_{std::move(config)}
{
try
{
std::string errstr;
@ -75,7 +75,7 @@ public:
{
throw_spdlog_ex(fmt_lib::format("error create kafka instance: {}", e.what()));
}
}
}
~kafka_sink()
{
@ -85,7 +85,7 @@ public:
protected:
void sink_it_(const details::log_msg &msg) override
{
producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY, (void *)msg.payload.data(), msg.payload.size(), NULL, NULL);
producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY, (void *)msg.payload.data(), msg.payload.size(), NULL, NULL);
}
void flush_() override
@ -104,7 +104,7 @@ private:
using kafka_sink_mt = kafka_sink<std::mutex>;
using kafka_sink_st = kafka_sink<spdlog::details::null_mutex>;
} // namespace sinks
} // namespace sinks
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> kafka_logger_mt(const std::string &logger_name, spdlog::sinks::kafka_sink_config config)
@ -121,13 +121,13 @@ inline std::shared_ptr<logger> kafka_logger_st(const std::string &logger_name, s
template<typename Factory = spdlog::async_factory>
inline std::shared_ptr<spdlog::logger> kafka_logger_async_mt(std::string logger_name, spdlog::sinks::kafka_sink_config config)
{
return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
}
template<typename Factory = spdlog::async_factory>
inline std::shared_ptr<spdlog::logger> kafka_logger_async_st(std::string logger_name, spdlog::sinks::kafka_sink_config config)
{
return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
}
} // namespace spdlog
} // namespace spdlog

View File

@ -3,7 +3,6 @@
#pragma once
#if defined(_WIN32)
# include <spdlog/details/null_mutex.h>
@ -16,11 +15,11 @@
# include <string>
// Avoid including windows.h (https://stackoverflow.com/a/30741042)
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
# if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringW(const wchar_t *lpOutputString);
#else
# else
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString);
#endif
# endif
extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
namespace spdlog {
@ -46,13 +45,13 @@ protected:
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
formatted.push_back('\0'); // add a null terminator for OutputDebugString
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
# if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
wmemory_buf_t wformatted;
details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), wformatted);
OutputDebugStringW(wformatted.data());
#else
# else
OutputDebugStringA(formatted.data());
#endif
# endif
}
void flush_() override {}

View File

@ -11,7 +11,7 @@
// auto edit = new QTextEdit();
// setCentralWidget(edit);
// auto logger = spdlog::qt_color_logger_mt("my_logger", my_edit_widget);
//
// Warning: those sinks won't be notified if the target widget is destroyed.
// If the widget's lifetime can be shorter than the logger's one, you should provide some permanent QObject,
// and then use a standard signal/slot.
@ -72,7 +72,6 @@ private:
QMetaMethod meta_method_;
};
// QT color sink to QTextEdit.
// Color location is determined by the sink log pattern like in the rest of spdlog sinks.
// Colors can be modified if needed using sink->set_color(level, qtTextCharFormat).
@ -81,104 +80,104 @@ template<typename Mutex>
class qt_color_sink : public base_sink<Mutex>
{
public:
qt_color_sink(QTextEdit *qt_text_edit) : qt_text_edit_(qt_text_edit)
qt_color_sink(QTextEdit *qt_text_edit)
: qt_text_edit_(qt_text_edit)
{
if (!qt_text_edit_)
{
if(!qt_text_edit_)
{
throw_spdlog_ex("qt_color_text_sink: text_edit is null");
}
default_color_ = qt_text_edit_->currentCharFormat();
// set colors
QTextCharFormat format;
// trace
format.setForeground(Qt::gray);
colors_.at(level::trace) = format;
// debug
format.setForeground(Qt::cyan);
colors_.at(level::debug) = format;
// info
format.setForeground(Qt::green);
colors_.at(level::info) = format;
// warn
format.setForeground(Qt::yellow);
colors_.at(level::warn) = format;
// err
format.setForeground(Qt::red);
colors_.at(level::err) = format;
// critical
format.setForeground(Qt::white);
format.setBackground(Qt::red);
colors_.at(level::critical) = format;
throw_spdlog_ex("qt_color_text_sink: text_edit is null");
}
default_color_ = qt_text_edit_->currentCharFormat();
// set colors
QTextCharFormat format;
// trace
format.setForeground(Qt::gray);
colors_.at(level::trace) = format;
// debug
format.setForeground(Qt::cyan);
colors_.at(level::debug) = format;
// info
format.setForeground(Qt::green);
colors_.at(level::info) = format;
// warn
format.setForeground(Qt::yellow);
colors_.at(level::warn) = format;
// err
format.setForeground(Qt::red);
colors_.at(level::err) = format;
// critical
format.setForeground(Qt::white);
format.setBackground(Qt::red);
colors_.at(level::critical) = format;
}
~qt_color_sink()
{
flush_();
}
~qt_color_sink()
{
flush_();
}
void set_default_color(QTextCharFormat format)
{
//std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
default_color_ = format;
}
void set_default_color(QTextCharFormat format)
{
// std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
default_color_ = format;
}
void set_level_color(level::level_enum color_level, QTextCharFormat format)
{
//std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
colors_.at(static_cast<size_t>(color_level)) = format;
}
void set_level_color(level::level_enum color_level, QTextCharFormat format)
{
// std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
colors_.at(static_cast<size_t>(color_level)) = format;
}
QTextCharFormat& get_level_color(level::level_enum color_level)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
return colors_.at(static_cast<size_t>(color_level));
}
QTextCharFormat &get_level_color(level::level_enum color_level)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
return colors_.at(static_cast<size_t>(color_level));
}
QTextCharFormat& get_default_color()
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
return default_color_;
}
QTextCharFormat &get_default_color()
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
return default_color_;
}
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 = string_view_t(formatted.data(), formatted.size());
// apply the color to the color range in the formatted message.
auto payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
if (msg.color_range_end > msg.color_range_start)
{
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
string_view_t str = string_view_t(formatted.data(), formatted.size());
QTextCursor cursor(qt_text_edit_->document());
cursor.movePosition(QTextCursor::End);
// apply the color to the color range in the formatted message.
auto payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
if(msg.color_range_end > msg.color_range_start)
{
QTextCursor cursor(qt_text_edit_->document());
cursor.movePosition(QTextCursor::End);
// insert the text before the color range
cursor.setCharFormat(default_color_);
cursor.insertText(payload.left(msg.color_range_start));
// insert the text before the color range
cursor.setCharFormat(default_color_);
cursor.insertText(payload.left(msg.color_range_start));
// insert the colorized text
auto color = colors_.at(static_cast<size_t>(msg.level));
cursor.setCharFormat(color);
cursor.insertText(payload.mid(msg.color_range_start, msg.color_range_end - msg.color_range_start));
// insert the colorized text
auto color = colors_.at(static_cast<size_t>(msg.level));
cursor.setCharFormat(color);
cursor.insertText(payload.mid(msg.color_range_start, msg.color_range_end - msg.color_range_start));
// insert the text after the color range with default format
cursor.setCharFormat(default_color_);
cursor.insertText(payload.mid(msg.color_range_end));
}
else // no color range
{
qt_text_edit_->append(payload.trimmed());
}
// insert the text after the color range with default format
cursor.setCharFormat(default_color_);
cursor.insertText(payload.mid(msg.color_range_end));
}
else // no color range
{
qt_text_edit_->append(payload.trimmed());
}
}
void flush_() override {}
QTextEdit *qt_text_edit_;
void flush_() override {}
QTextEdit *qt_text_edit_;
QTextCharFormat default_color_;
std::array<QTextCharFormat, level::n_levels> colors_;
};
std::array<QTextCharFormat, level::n_levels> colors_;
};
#include "spdlog/details/null_mutex.h"
#include <mutex>
@ -194,13 +193,15 @@ using qt_color_sink_st = qt_color_sink<spdlog::details::null_mutex>;
// create logger using QTextEdit object
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(QString)")
inline std::shared_ptr<logger> qt_logger_mt(
const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
{
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, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
inline std::shared_ptr<logger> qt_logger_st(
const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
{
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
}

View File

@ -72,7 +72,7 @@ SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &m
memory_buf_t formatted;
formatter_->format(msg, formatted);
::fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
#endif // WIN32
#endif // WIN32
::fflush(file_); // flush every line to terminal
}

View File

@ -241,12 +241,12 @@ protected:
details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf);
LPCWSTR lp_wstr = buf.data();
succeeded = static_cast<bool>(::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr));
succeeded = static_cast<bool>(::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg),
event_id_, current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr));
#else
LPCSTR lp_str = formatted.data();
succeeded = static_cast<bool>(::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr));
succeeded = static_cast<bool>(::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg),
event_id_, current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr));
#endif
if (!succeeded)

View File

@ -31,7 +31,7 @@ using default_factory = synchronous_factory;
// Example:
// spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... sink_args)
inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...sink_args)
{
return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
}
@ -141,49 +141,49 @@ SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logge
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)
inline void log(source_loc source, level::level_enum lvl, format_string_t<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, format_string_t<Args...> fmt, Args &&... args)
inline void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void trace(format_string_t<Args...> fmt, Args &&... args)
inline void trace(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void debug(format_string_t<Args...> fmt, Args &&... args)
inline void debug(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void info(format_string_t<Args...> fmt, Args &&... args)
inline void info(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->info(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void warn(format_string_t<Args...> fmt, Args &&... args)
inline void warn(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void error(format_string_t<Args...> fmt, Args &&... args)
inline void error(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->error(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void critical(format_string_t<Args...> fmt, Args &&... args)
inline void critical(format_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
}
@ -202,49 +202,49 @@ inline void log(level::level_enum lvl, const T &msg)
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args)
inline void log(source_loc source, level::level_enum lvl, wformat_string_t<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, wformat_string_t<Args...> fmt, Args &&... args)
inline void log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void trace(wformat_string_t<Args...> fmt, Args &&... args)
inline void trace(wformat_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void debug(wformat_string_t<Args...> fmt, Args &&... args)
inline void debug(wformat_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void info(wformat_string_t<Args...> fmt, Args &&... args)
inline void info(wformat_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->info(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void warn(wformat_string_t<Args...> fmt, Args &&... args)
inline void warn(wformat_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void error(wformat_string_t<Args...> fmt, Args &&... args)
inline void error(wformat_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->error(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
inline void critical(wformat_string_t<Args...> fmt, Args &&... args)
inline void critical(wformat_string_t<Args...> fmt, Args &&...args)
{
default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
}

View File

@ -14,16 +14,12 @@ if(PkgConfig_FOUND)
endif()
find_package(Catch2 3 QUIET)
if (Catch2_FOUND)
if(Catch2_FOUND)
message(STATUS "Packaged version of Catch will be used.")
else()
message(STATUS "Bundled version of Catch will be downloaded and used.")
include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.3.2
)
FetchContent_Declare(Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v3.3.2)
FetchContent_MakeAvailable(Catch2)
endif()

View File

@ -1,6 +1,6 @@
/*
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
*/
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
*/
#include "includes.h"
#include <iostream>
@ -11,114 +11,114 @@
class failing_sink : public spdlog::sinks::base_sink<std::mutex>
{
protected:
void sink_it_(const spdlog::details::log_msg &) final
{
throw std::runtime_error("some error happened during log");
}
void sink_it_(const spdlog::details::log_msg &) final
{
throw std::runtime_error("some error happened during log");
}
void flush_() final
{
throw std::runtime_error("some error happened during flush");
}
void flush_() final
{
throw std::runtime_error("some error happened during flush");
}
};
struct custom_ex {};
struct custom_ex
{};
#if !defined(SPDLOG_USE_STD_FORMAT) // std formt doesn't fully support tuntime strings
TEST_CASE("default_error_handler", "[errors]")
{
prepare_logdir();
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
prepare_logdir();
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
logger->set_pattern("%v");
logger->info(SPDLOG_FMT_RUNTIME("Test message {} {}"), 1);
logger->info("Test message {}", 2);
logger->flush();
using spdlog::details::os::default_eol;
REQUIRE(file_contents(SIMPLE_LOG) == spdlog::fmt_lib::format("Test message 2{}", default_eol));
REQUIRE(count_lines(SIMPLE_LOG) == 1);
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("test-error", filename, true);
logger->set_pattern("%v");
logger->info(SPDLOG_FMT_RUNTIME("Test message {} {}"), 1);
logger->info("Test message {}", 2);
logger->flush();
using spdlog::details::os::default_eol;
REQUIRE(file_contents(SIMPLE_LOG) == spdlog::fmt_lib::format("Test message 2{}", default_eol));
REQUIRE(count_lines(SIMPLE_LOG) == 1);
}
TEST_CASE("custom_error_handler", "[errors]")
{
prepare_logdir();
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
logger->flush_on(spdlog::level::info);
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
logger->info("Good message #1");
prepare_logdir();
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
logger->flush_on(spdlog::level::info);
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
logger->info("Good message #1");
REQUIRE_THROWS_AS(logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx"), custom_ex);
logger->info("Good message #2");
require_message_count(SIMPLE_LOG, 2);
REQUIRE_THROWS_AS(logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx"), custom_ex);
logger->info("Good message #2");
require_message_count(SIMPLE_LOG, 2);
}
#endif
TEST_CASE("default_error_handler2", "[errors]")
{
spdlog::drop_all();
auto logger = spdlog::create<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
spdlog::drop_all();
auto logger = spdlog::create<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
}
TEST_CASE("flush_error_handler", "[errors]")
{
spdlog::drop_all();
auto logger = spdlog::create<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
spdlog::drop_all();
auto logger = spdlog::create<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
}
#if !defined(SPDLOG_USE_STD_FORMAT)
TEST_CASE("async_error_handler", "[errors]")
{
prepare_logdir();
std::string err_msg("log failed with some msg");
prepare_logdir();
std::string err_msg("log failed with some msg");
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_ASYNC_LOG);
{
spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
logger->set_error_handler([=](const std::string &) {
std::ofstream ofs("test_logs/custom_err.txt");
if (!ofs)
{
throw std::runtime_error("Failed open test_logs/custom_err.txt");
}
ofs << err_msg;
});
logger->info("Good message #1");
logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx");
logger->info("Good message #2");
spdlog::drop("logger"); // force logger to drain the queue and shutdown
}
spdlog::init_thread_pool(128, 1);
require_message_count(SIMPLE_ASYNC_LOG, 2);
REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg);
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_ASYNC_LOG);
{
spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
logger->set_error_handler([=](const std::string &) {
std::ofstream ofs("test_logs/custom_err.txt");
if (!ofs)
{
throw std::runtime_error("Failed open test_logs/custom_err.txt");
}
ofs << err_msg;
});
logger->info("Good message #1");
logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx");
logger->info("Good message #2");
spdlog::drop("logger"); // force logger to drain the queue and shutdown
}
spdlog::init_thread_pool(128, 1);
require_message_count(SIMPLE_ASYNC_LOG, 2);
REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg);
}
#endif
// Make sure async error handler is executed
TEST_CASE("async_error_handler2", "[errors]")
{
prepare_logdir();
std::string err_msg("This is async handler error message");
{
spdlog::details::os::create_dir(SPDLOG_FILENAME_T("test_logs"));
spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) {
std::ofstream ofs("test_logs/custom_err2.txt");
if (!ofs)
throw std::runtime_error("Failed open test_logs/custom_err2.txt");
ofs << err_msg;
});
logger->info("Hello failure");
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
}
prepare_logdir();
std::string err_msg("This is async handler error message");
{
spdlog::details::os::create_dir(SPDLOG_FILENAME_T("test_logs"));
spdlog::init_thread_pool(128, 1);
auto logger = spdlog::create_async<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string &) {
std::ofstream ofs("test_logs/custom_err2.txt");
if (!ofs)
throw std::runtime_error("Failed open test_logs/custom_err2.txt");
ofs << err_msg;
});
logger->info("Hello failure");
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
}
spdlog::init_thread_pool(128, 1);
REQUIRE(file_contents("test_logs/custom_err2.txt") == err_msg);
spdlog::init_thread_pool(128, 1);
REQUIRE(file_contents("test_logs/custom_err2.txt") == err_msg);
}

View File

@ -6,7 +6,7 @@ using spdlog::details::to_string_view;
// log to str and return it
template<typename... Args>
static std::string log_to_str(const std::string &msg, const Args &... args)
static std::string log_to_str(const std::string &msg, const Args &...args)
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);