From cf2bf488a22373b46eee36513335ff0e44355abc Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 4 Oct 2019 21:49:20 +0300 Subject: [PATCH 01/96] CmakeLists.txt: Fixed package config generation --- CMakeLists.txt | 17 +++++++++++------ cmake/spdlog.pc.in | 7 +++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7efe5049..0c302bcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,8 +87,6 @@ set(SPDLOG_SRCS src/file_sinks.cpp src/async.cpp) -set(SPDLOG_CFLAGS "${PROJECT_NAME}") - if (SPDLOG_BUILD_SHARED) if(WIN32) message(FATAL_ERROR "spdlog shared lib is not yet supported under windows") @@ -129,14 +127,13 @@ if(SPDLOG_FMT_EXTERNAL) if (NOT TARGET fmt::fmt) find_package(fmt REQUIRED) endif () - - set(SPDLOG_CFLAGS "${SPDLOG_CFLAGS} -DSPDLOG_FMT_EXTERNAL") - target_compile_definitions(spdlog PUBLIC SPDLOG_FMT_EXTERNAL) target_link_libraries(spdlog PUBLIC fmt::fmt) target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_FMT_EXTERNAL) target_link_libraries(spdlog_header_only INTERFACE fmt::fmt) + + set(PKG_CONFIG_REQUIRES fmt) # add dependecy to pkg-config endif() if(SPDLOG_WCHAR_SUPPORT) @@ -204,11 +201,17 @@ if (SPDLOG_INSTALL) endif() #--------------------------------------------------------------------------------------- - # Package and version files + # Install pkg-config file #--------------------------------------------------------------------------------------- + get_target_property(PKG_CONFIG_DEFINES spdlog INTERFACE_COMPILE_DEFINITIONS) + string(REPLACE ";" " -D" PKG_CONFIG_DEFINES "${PKG_CONFIG_DEFINES}") + string(PREPEND PKG_CONFIG_DEFINES "-D") configure_file("cmake/${PROJECT_NAME}.pc.in" "${pkg_config}" @ONLY) install(FILES "${pkg_config}" DESTINATION "${pkgconfig_install_dir}") + #--------------------------------------------------------------------------------------- + # Install CMake config files + #--------------------------------------------------------------------------------------- install(EXPORT spdlog DESTINATION ${export_dest_dir} NAMESPACE spdlog:: @@ -216,6 +219,7 @@ if (SPDLOG_INSTALL) include(CMakePackageConfigHelpers) configure_file("${project_config_in}" "${project_config_out}" @ONLY) + write_basic_package_version_file("${version_config_file}" COMPATIBILITY SameMajorVersion) install(FILES "${project_config_out}" @@ -227,3 +231,4 @@ if (SPDLOG_INSTALL) include(cmake/spdlogCPack.cmake) endif () + diff --git a/cmake/spdlog.pc.in b/cmake/spdlog.pc.in index 86f5905f..121478fd 100644 --- a/cmake/spdlog.pc.in +++ b/cmake/spdlog.pc.in @@ -7,5 +7,8 @@ Name: lib@PROJECT_NAME@ Description: Fast C++ logging library. URL: https://github.com/gabime/@PROJECT_NAME@ Version: @SPDLOG_VERSION@ -CFlags: -I${includedir}/@SPDLOG_CFLAGS@ -Libs: -L${libdir}/@PROJECT_NAME@ -l@PROJECT_NAME@ +CFlags: -I${includedir} @PKG_CONFIG_DEFINES@ +Libs: -L${libdir} -lspdlog +Libs.private: -lpthread +Requires: @PKG_CONFIG_REQUIRES@ + From fb70eca0a31f65953551eb646039205a4d94172c Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 4 Oct 2019 21:56:17 +0300 Subject: [PATCH 02/96] CmakeLists.txt: add -pthread to pkg-config --- cmake/spdlog.pc.in | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmake/spdlog.pc.in b/cmake/spdlog.pc.in index 121478fd..861707c3 100644 --- a/cmake/spdlog.pc.in +++ b/cmake/spdlog.pc.in @@ -8,7 +8,6 @@ Description: Fast C++ logging library. URL: https://github.com/gabime/@PROJECT_NAME@ Version: @SPDLOG_VERSION@ CFlags: -I${includedir} @PKG_CONFIG_DEFINES@ -Libs: -L${libdir} -lspdlog -Libs.private: -lpthread +Libs: -L${libdir} -lspdlog -pthread Requires: @PKG_CONFIG_REQUIRES@ From cfe7cac1c47db174ab23f4294a98da1d9a26ffe1 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 6 Oct 2019 00:30:40 +0300 Subject: [PATCH 03/96] version 1.4.2 --- include/spdlog/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/version.h b/include/spdlog/version.h index b1b114ab..c374bd47 100644 --- a/include/spdlog/version.h +++ b/include/spdlog/version.h @@ -5,6 +5,6 @@ #define SPDLOG_VER_MAJOR 1 #define SPDLOG_VER_MINOR 4 -#define SPDLOG_VER_PATCH 1 +#define SPDLOG_VER_PATCH 2 #define SPDLOG_VERSION (SPDLOG_VER_MAJOR * 10000 + SPDLOG_VER_MINOR * 100 + SPDLOG_VER_PATCH) From 70357ceff2fc342ec87c56124976e3dabf7b3bd9 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 6 Oct 2019 00:31:38 +0300 Subject: [PATCH 04/96] clang-format --- include/spdlog/sinks/wincolor_sink-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 72225aac..aee0bb95 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -140,7 +140,7 @@ void SPDLOG_INLINE wincolor_sink::print_range_(const memory_buf_t template void SPDLOG_INLINE wincolor_sink::write_to_file_(const memory_buf_t &formatted) { - if(out_handle_ == nullptr) // no console and no file redirect + if (out_handle_ == nullptr) // no console and no file redirect { return; } From 1549ff12f1aa61ffc4d9a8727c519034724392a0 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 6 Oct 2019 13:13:26 +0300 Subject: [PATCH 05/96] Replace STRING(PREPEND ..) in CMakeLists.txt to support older CMake versions --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c302bcd..12320fbf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -205,7 +205,7 @@ if (SPDLOG_INSTALL) #--------------------------------------------------------------------------------------- get_target_property(PKG_CONFIG_DEFINES spdlog INTERFACE_COMPILE_DEFINITIONS) string(REPLACE ";" " -D" PKG_CONFIG_DEFINES "${PKG_CONFIG_DEFINES}") - string(PREPEND PKG_CONFIG_DEFINES "-D") + string(CONCAT PKG_CONFIG_DEFINES "-D" "${PKG_CONFIG_DEFINES}") configure_file("cmake/${PROJECT_NAME}.pc.in" "${pkg_config}" @ONLY) install(FILES "${pkg_config}" DESTINATION "${pkgconfig_install_dir}") From 9369fe8c27260ca7929559a0f4e11980a8b3ade6 Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 9 Oct 2019 21:41:02 +0300 Subject: [PATCH 06/96] Fix #1262 --- include/spdlog/logger-inl.h | 5 +++++ include/spdlog/logger.h | 4 ++++ include/spdlog/spdlog.h | 5 ++++- tests/test_macros.cpp | 10 ++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 05c57e8e..f7605de5 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -69,6 +69,11 @@ SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const return msg_level >= level_.load(std::memory_order_relaxed); } +SPDLOG_INLINE bool logger::should_backtrace() const +{ + return tracer_.enabled(); +} + SPDLOG_INLINE void logger::set_level(level::level_enum log_level) { level_.store(log_level); diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 0e4eaa00..56b982fe 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -319,8 +319,12 @@ public: #endif // _WIN32 #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT + // return true logging is enabled for the given level. bool should_log(level::level_enum msg_level) const; + // return true if backtrace logging is enabled. + bool should_backtrace() const; + void set_level(level::level_enum log_level); level::level_enum level() const; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 3638ba12..a0d3c912 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -285,7 +285,10 @@ inline void critical(wstring_view_t fmt, const Args &... args) // SPDLOG_LEVEL_OFF // -#define SPDLOG_LOGGER_CALL(logger, level, ...) logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) +#define SPDLOG_LOGGER_CALL(logger, level, ...) do {\ + if(logger->should_log(level) || logger->should_backtrace()) \ + logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__);\ + } while(0) #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE #define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index 22a5ccbf..6a57a166 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -39,3 +39,13 @@ TEST_CASE("disable param evaluation", "[macros]") { SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated")); } + +// ensure that even if right macro level is on- don't eavluate if the logger's level is not high enough +TEST_CASE("disable param evaluation2", "[macros]") +{ + auto logger = std::make_shared("test-macro"); + logger->set_level(spdlog::level::off); + int x = 0; + SPDLOG_LOGGER_DEBUG(logger, "Test message {}", ++x); + REQUIRE(x == 0); +} From 856b4f4654ae41ca0a273bdbefc42eca5c7b18ec Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Wed, 9 Oct 2019 23:22:45 +0300 Subject: [PATCH 07/96] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4067e842..0f45b9a0 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ $ cmake .. && make -j * Gentoo: `emerge dev-libs/spdlog` * Arch Linux: `yaourt -S spdlog-git` * vcpkg: `vcpkg install spdlog` +* conan: `spdlog/[>=1.4.1]@bincrafters/stable` + ## Features * Very fast (see [benchmarks](#benchmarks) below). From aa65dd89053bea0b7e0ffc8d9be4f152d5c01f44 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Fri, 11 Oct 2019 19:48:33 +0300 Subject: [PATCH 08/96] version 1.4.3 rc --- include/spdlog/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/version.h b/include/spdlog/version.h index c374bd47..8b4fa4e5 100644 --- a/include/spdlog/version.h +++ b/include/spdlog/version.h @@ -5,6 +5,6 @@ #define SPDLOG_VER_MAJOR 1 #define SPDLOG_VER_MINOR 4 -#define SPDLOG_VER_PATCH 2 +#define SPDLOG_VER_PATCH 3 #define SPDLOG_VERSION (SPDLOG_VER_MAJOR * 10000 + SPDLOG_VER_MINOR * 100 + SPDLOG_VER_PATCH) From a03f9eb1561e738b9111e731d5f183c5b6093d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kundr=C3=A1t?= Date: Fri, 18 Oct 2019 10:14:44 +0200 Subject: [PATCH 09/96] Fix build failure on clang 7 with libc++ Unlike the GNU C++ STL, there's no implicit include for in this one, apparently. --- include/spdlog/sinks/systemd_sink.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index c45a9f66..bad6442d 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -7,6 +7,7 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/details/synchronous_factory.h" +#include #include namespace spdlog { From fcc809f4f161f41390492bacbf36662266ab8fe2 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 18 Oct 2019 16:10:30 +0300 Subject: [PATCH 10/96] Refactored logger --- include/spdlog/details/backtracer-inl.h | 4 -- include/spdlog/details/backtracer.h | 1 - include/spdlog/logger-inl.h | 14 ++++++- include/spdlog/logger.h | 54 +++++++------------------ 4 files changed, 28 insertions(+), 45 deletions(-) diff --git a/include/spdlog/details/backtracer-inl.h b/include/spdlog/details/backtracer-inl.h index daf1ab33..8b0eb2bf 100644 --- a/include/spdlog/details/backtracer-inl.h +++ b/include/spdlog/details/backtracer-inl.h @@ -48,10 +48,6 @@ SPDLOG_INLINE bool backtracer::enabled() const return enabled_.load(std::memory_order_relaxed); } -SPDLOG_INLINE backtracer::operator bool() const -{ - return enabled(); -} SPDLOG_INLINE void backtracer::push_back(const log_msg &msg) { diff --git a/include/spdlog/details/backtracer.h b/include/spdlog/details/backtracer.h index cf813fe6..713d00e9 100644 --- a/include/spdlog/details/backtracer.h +++ b/include/spdlog/details/backtracer.h @@ -31,7 +31,6 @@ public: void enable(size_t size); void disable(); bool enabled() const; - explicit operator bool() const; void push_back(const log_msg &msg); // pop all items in the q and apply the given fun on each of them. diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index f7605de5..ebb3ec92 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -172,6 +172,18 @@ SPDLOG_INLINE std::shared_ptr logger::clone(std::string logger_name) } // protected methods +SPDLOG_INLINE void logger::log_it_(const details::log_msg &log_msg) +{ + if (should_log(log_msg.level)) + { + sink_it_(log_msg); + } + if (tracer_.enabled()) + { + tracer_.push_back(log_msg); + } +} + SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg) { for (auto &sink : sinks_) @@ -207,7 +219,7 @@ SPDLOG_INLINE void logger::flush_() SPDLOG_INLINE void logger::dump_backtrace_() { using details::log_msg; - if (tracer_) + if (tracer_.enabled()) { sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"}); tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); }); diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 56b982fe..45ba28ec 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -73,11 +73,11 @@ public: void swap(spdlog::logger &other) SPDLOG_NOEXCEPT; + template void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) { - auto level_enabled = should_log(lvl); - if (!level_enabled && !tracer_) + if (!should_log(lvl) && !tracer_.enabled()) { return; } @@ -86,14 +86,7 @@ public: memory_buf_t buf; fmt::format_to(buf, fmt, args...); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - if (level_enabled) - { - sink_it_(log_msg); - } - if (tracer_) - { - tracer_.push_back(log_msg); - } + log_it_(log_msg); } SPDLOG_LOGGER_CATCH() } @@ -150,24 +143,14 @@ public: template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - auto level_enabled = should_log(lvl); - if (!level_enabled && !tracer_) + if (!should_log(lvl) && !tracer_.enabled()) { return; } - SPDLOG_TRY - { - details::log_msg log_msg(loc, name_, lvl, msg); - if (level_enabled) - { - sink_it_(log_msg); - } - if (tracer_) - { - tracer_.push_back(log_msg); - } - } - SPDLOG_LOGGER_CATCH() + + details::log_msg log_msg(loc, name_, lvl, msg); + log_it_(log_msg); + } void log(level::level_enum lvl, string_view_t msg) @@ -228,8 +211,7 @@ public: template void log(source_loc loc, level::level_enum lvl, wstring_view_t fmt, const Args &... args) { - auto level_enabled = should_log(lvl); - if (!level_enabled && !tracer_) + if (!should_log(lvl) && !tracer_.enabled()) { return; } @@ -242,15 +224,7 @@ public: 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())); - - if (level_enabled) - { - sink_it_(log_msg); - } - if (tracer_) - { - tracer_.push_back(log_msg); - } + log_it_(log_msg); } SPDLOG_LOGGER_CATCH() } @@ -301,7 +275,7 @@ public: template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - if (!should_log(lvl)) + if (!should_log(lvl) && !tracer_.enabled()) { return; } @@ -310,9 +284,8 @@ public: { memory_buf_t buf; details::os::wstr_to_utf8buf(msg, buf); - details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - sink_it_(log_msg); + log_it_(log_msg); } SPDLOG_LOGGER_CATCH() } @@ -367,6 +340,9 @@ protected: err_handler custom_err_handler_{nullptr}; details::backtracer tracer_; + // log the given message (if the given log level is high enough), + // and save backtrace (if backtrace is enabled). + void log_it_(const details::log_msg &log_msg); virtual void sink_it_(const details::log_msg &msg); virtual void flush_(); void dump_backtrace_(); From 9a0a0c2d8c43625847b9d9427b749b267f9be0a8 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 18 Oct 2019 16:14:08 +0300 Subject: [PATCH 11/96] clang-format --- include/spdlog/details/backtracer-inl.h | 1 - include/spdlog/logger.h | 2 -- include/spdlog/spdlog.h | 10 ++++++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/include/spdlog/details/backtracer-inl.h b/include/spdlog/details/backtracer-inl.h index 8b0eb2bf..8aa34b97 100644 --- a/include/spdlog/details/backtracer-inl.h +++ b/include/spdlog/details/backtracer-inl.h @@ -48,7 +48,6 @@ SPDLOG_INLINE bool backtracer::enabled() const return enabled_.load(std::memory_order_relaxed); } - SPDLOG_INLINE void backtracer::push_back(const log_msg &msg) { std::lock_guard lock{mutex_}; diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 45ba28ec..04fd9b15 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -73,7 +73,6 @@ public: void swap(spdlog::logger &other) SPDLOG_NOEXCEPT; - template void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) { @@ -150,7 +149,6 @@ public: details::log_msg log_msg(loc, name_, lvl, msg); log_it_(log_msg); - } void log(level::level_enum lvl, string_view_t msg) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index a0d3c912..eefaf25c 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -285,10 +285,12 @@ inline void critical(wstring_view_t fmt, const Args &... args) // SPDLOG_LEVEL_OFF // -#define SPDLOG_LOGGER_CALL(logger, level, ...) do {\ - if(logger->should_log(level) || logger->should_backtrace()) \ - logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__);\ - } while(0) +#define SPDLOG_LOGGER_CALL(logger, level, ...) \ + do \ + { \ + if (logger->should_log(level) || logger->should_backtrace()) \ + logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); \ + } while (0) #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE #define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) From 4aad51a35249e07f8a171e5dc8e72dd5706dae78 Mon Sep 17 00:00:00 2001 From: Florian Pigorsch Date: Sat, 19 Oct 2019 09:42:28 +0200 Subject: [PATCH 12/96] Fix some spelling errors. casese -> cases (1) chache -> cache (1) cirucal -> circular (1) dependecy -> dependency (1) detrmine -> determine (2) eavluate -> evaluate (1) exertnal -> external (1) ony -> only (1) registation -> registration (3) registring -> registering (2) regsistration -> registration (3) seperate -> separate (2) wit -> with (1) withe -> with (1) --- CMakeLists.txt | 4 ++-- include/spdlog/async.h | 2 +- include/spdlog/details/circular_q.h | 2 +- include/spdlog/details/file_helper-inl.h | 2 +- include/spdlog/details/os.h | 2 +- include/spdlog/details/pattern_formatter-inl.h | 2 +- include/spdlog/details/registry-inl.h | 4 ++-- include/spdlog/details/registry.h | 2 +- include/spdlog/logger-inl.h | 2 +- include/spdlog/logger.h | 2 +- include/spdlog/sinks/daily_file_sink.h | 2 +- include/spdlog/spdlog-inl.h | 4 ++-- include/spdlog/spdlog.h | 2 +- meson.build | 2 +- tests/test_macros.cpp | 2 +- tests/test_registry.cpp | 4 ++-- 16 files changed, 20 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 12320fbf..fa9e1fab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,7 +121,7 @@ target_link_libraries(spdlog_header_only INTERFACE Threads::Threads) #--------------------------------------------------------------------------------------- -# Use fmt package if using exertnal fmt +# Use fmt package if using external fmt #--------------------------------------------------------------------------------------- if(SPDLOG_FMT_EXTERNAL) if (NOT TARGET fmt::fmt) @@ -133,7 +133,7 @@ if(SPDLOG_FMT_EXTERNAL) target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_FMT_EXTERNAL) target_link_libraries(spdlog_header_only INTERFACE fmt::fmt) - set(PKG_CONFIG_REQUIRES fmt) # add dependecy to pkg-config + set(PKG_CONFIG_REQUIRES fmt) # add dependency to pkg-config endif() if(SPDLOG_WCHAR_SUPPORT) diff --git a/include/spdlog/async.h b/include/spdlog/async.h index 833be4fb..dc99492d 100644 --- a/include/spdlog/async.h +++ b/include/spdlog/async.h @@ -6,7 +6,7 @@ // // Async logging using global thread pool // All loggers created here share same global thread pool. -// Each log message is pushed to a queue along withe a shared pointer to the +// Each log message is pushed to a queue along with a shared pointer to the // logger. // If a logger deleted while having pending messages in the queue, it's actual // destruction will defer diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index fee3a3bf..4a87940d 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -1,7 +1,7 @@ // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) -// cirucal q view of std::vector. +// circular q view of std::vector. #pragma once #include diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index d8c9cab4..440cb290 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -119,7 +119,7 @@ SPDLOG_INLINE std::tuple file_helper::split_by_extension return std::make_tuple(fname, filename_t()); } - // treat casese like "/etc/rc.d/somelogfile or "/abc/.hiddenfile" + // treat cases like "/etc/rc.d/somelogfile or "/abc/.hiddenfile" auto folder_index = fname.rfind(details::os::folder_sep); if (folder_index != filename_t::npos && folder_index >= ext_index - 1) { diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 6d9dae13..a4a57b7f 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -81,7 +81,7 @@ int pid() SPDLOG_NOEXCEPT; // Source: https://github.com/agauniyal/rang/ bool is_color_terminal() SPDLOG_NOEXCEPT; -// Detrmine if the terminal attached +// Determine if the terminal attached // Source: https://github.com/agauniyal/rang/ bool in_terminal(FILE *file) SPDLOG_NOEXCEPT; diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index 1b95eb6d..33a57a13 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -596,7 +596,7 @@ public: #ifdef _WIN32 int total_minutes = get_cached_offset(msg, tm_time); #else - // No need to chache under gcc, + // No need to cache under gcc, // it is very fast (already stored in tm.tm_gmtoff) (void)(msg); int total_minutes = os::utc_minutes_offset(tm_time); diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index 075a0c7e..784ec237 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -254,10 +254,10 @@ SPDLOG_INLINE std::recursive_mutex ®istry::tp_mutex() return tp_mutex_; } -SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_regsistration) +SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registration) { std::lock_guard lock(logger_map_mutex_); - automatic_registration_ = automatic_regsistration; + automatic_registration_ = automatic_registration; } SPDLOG_INLINE registry ®istry::instance() diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 35a117d9..abb95c32 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -77,7 +77,7 @@ public: std::recursive_mutex &tp_mutex(); - void set_automatic_registration(bool automatic_regsistration); + void set_automatic_registration(bool automatic_registration); static registry &instance(); diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index ebb3ec92..4908ede0 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -90,7 +90,7 @@ SPDLOG_INLINE const std::string &logger::name() const } // set formatting for the sinks in this logger. -// each sink will get a seperate instance of the formatter object. +// each sink will get a separate instance of the formatter object. SPDLOG_INLINE void logger::set_formatter(std::unique_ptr f) { for (auto it = sinks_.begin(); it != sinks_.end(); ++it) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 04fd9b15..6f6f341d 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -303,7 +303,7 @@ public: const std::string &name() const; // set formatting for the sinks in this logger. - // each sink will get a seperate instance of the formatter object. + // each sink will get a separate instance of the formatter object. void set_formatter(std::unique_ptr f); void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local); diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 8514f615..83b63e1a 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -95,7 +95,7 @@ protected: base_sink::formatter_->format(msg, formatted); file_helper_.write(formatted); - // Do the cleaning ony at the end because it might throw on failure. + // Do the cleaning only at the end because it might throw on failure. if (should_rotate && max_files_ > 0) { delete_old_(); diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h index bbfb6e24..34e8d9d0 100644 --- a/include/spdlog/spdlog-inl.h +++ b/include/spdlog/spdlog-inl.h @@ -92,9 +92,9 @@ SPDLOG_INLINE void shutdown() details::registry::instance().shutdown(); } -SPDLOG_INLINE void set_automatic_registration(bool automatic_registation) +SPDLOG_INLINE void set_automatic_registration(bool automatic_registration) { - details::registry::instance().set_automatic_registration(automatic_registation); + details::registry::instance().set_automatic_registration(automatic_registration); } SPDLOG_INLINE std::shared_ptr default_logger() diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index eefaf25c..392a91c5 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -100,7 +100,7 @@ void drop_all(); void shutdown(); // Automatic registration of loggers when using spdlog::create() or spdlog::create_async -void set_automatic_registration(bool automatic_registation); +void set_automatic_registration(bool automatic_registration); // API for using default logger (stdout_color_mt), // e.g: spdlog::info("Message {}", 1); diff --git a/meson.build b/meson.build index 5d3ac101..0c3c2550 100644 --- a/meson.build +++ b/meson.build @@ -21,7 +21,7 @@ dep_list += dependency('threads') # Check for FMT if get_option('external_fmt') if not meson.version().version_compare('>=0.49.0') - warning('Finding fmt can fail wit meson versions before 0.49.0') + warning('Finding fmt can fail with meson versions before 0.49.0') endif dep_list += dependency('fmt') compile_args += '-DSPDLOG_FMT_EXTERNAL' diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index 6a57a166..26b03cdf 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -40,7 +40,7 @@ TEST_CASE("disable param evaluation", "[macros]") SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated")); } -// ensure that even if right macro level is on- don't eavluate if the logger's level is not high enough +// ensure that even if right macro level is on- don't evaluate if the logger's level is not high enough TEST_CASE("disable param evaluation2", "[macros]") { auto logger = std::make_shared("test-macro"); diff --git a/tests/test_registry.cpp b/tests/test_registry.cpp index 9759a505..c1a8a308 100644 --- a/tests/test_registry.cpp +++ b/tests/test_registry.cpp @@ -9,7 +9,7 @@ TEST_CASE("register_drop", "[registry]") spdlog::drop_all(); spdlog::create(tested_logger_name); REQUIRE(spdlog::get(tested_logger_name) != nullptr); - // Throw if registring existing name + // Throw if registering existing name REQUIRE_THROWS_AS(spdlog::create(tested_logger_name), spdlog::spdlog_ex); } @@ -19,7 +19,7 @@ TEST_CASE("explicit register", "[registry]") auto logger = std::make_shared(tested_logger_name, std::make_shared()); spdlog::register_logger(logger); REQUIRE(spdlog::get(tested_logger_name) != nullptr); - // Throw if registring existing name + // Throw if registering existing name REQUIRE_THROWS_AS(spdlog::create(tested_logger_name), spdlog::spdlog_ex); } #endif From fbb3f41dff9ad6499de48cadd756e99f18b4392c Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 20 Oct 2019 10:22:44 +0300 Subject: [PATCH 13/96] Fix #1273 --- CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fa9e1fab..abceb307 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,11 +82,15 @@ message(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) set(SPDLOG_SRCS src/spdlog.cpp src/stdout_sinks.cpp - src/fmt.cpp src/color_sinks.cpp src/file_sinks.cpp src/async.cpp) + +if(NOT SPDLOG_FMT_EXTERNAL) + list(APPEND SPDLOG_SRCS src/fmt.cpp) +endif() + if (SPDLOG_BUILD_SHARED) if(WIN32) message(FATAL_ERROR "spdlog shared lib is not yet supported under windows") From 4858d7e454e66d347024a074749535b91ccbeed9 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 20 Oct 2019 10:27:17 +0300 Subject: [PATCH 14/96] Fix #1274 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index abceb307..5946b2a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,7 +189,7 @@ if (SPDLOG_INSTALL) set(project_config_out "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfig.cmake") set(config_targets_file "spdlogConfigTargets.cmake") set(version_config_file "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfigVersion.cmake") - set(export_dest_dir "${CMAKE_INSTALL_LIBDIR}/spdlog/cmake") + set(export_dest_dir "${CMAKE_INSTALL_LIBDIR}/cmake/spdlog") set(pkgconfig_install_dir "${CMAKE_INSTALL_LIBDIR}/pkgconfig") set(pkg_config "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc") From 9b7812a0f22f4743ee1618c7d879c8f30004253d Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 20 Oct 2019 17:40:56 +0300 Subject: [PATCH 15/96] auto create log dir --- bench/CMakeLists.txt | 2 - bench/latency.cpp | 22 -------- bench/meson.build | 1 - example/CMakeLists.txt | 2 - example/meson.build | 1 - include/spdlog/details/file_helper-inl.h | 8 +++ include/spdlog/details/os-inl.h | 69 +++++++++++++++++++++++- include/spdlog/details/os.h | 11 ++++ tests/CMakeLists.txt | 4 +- tests/meson.build | 3 -- tests/test_async.cpp | 4 +- tests/test_create_dir.cpp | 27 ++++++++++ tests/test_daily_logger.cpp | 8 +-- tests/test_errors.cpp | 19 +++---- tests/test_file_helper.cpp | 2 +- tests/test_file_logging.cpp | 8 +-- tests/test_macros.cpp | 2 +- tests/utils.cpp | 12 ++--- 18 files changed, 141 insertions(+), 64 deletions(-) create mode 100644 tests/test_create_dir.cpp diff --git a/bench/CMakeLists.txt b/bench/CMakeLists.txt index d087cf66..0ea88423 100644 --- a/bench/CMakeLists.txt +++ b/bench/CMakeLists.txt @@ -24,5 +24,3 @@ target_link_libraries(latency PRIVATE benchmark::benchmark spdlog::spdlog) add_executable(formatter-bench formatter-bench.cpp) target_link_libraries(formatter-bench PRIVATE benchmark::benchmark spdlog::spdlog) - -file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") diff --git a/bench/latency.cpp b/bench/latency.cpp index 730226ca..cd8717d9 100644 --- a/bench/latency.cpp +++ b/bench/latency.cpp @@ -16,26 +16,6 @@ #include "spdlog/sinks/null_sink.h" #include "spdlog/sinks/rotating_file_sink.h" -void prepare_logdir() -{ - spdlog::info("Preparing latency_logs directory.."); -#ifdef _WIN32 - system("if not exist logs mkdir latency_logs"); - system("del /F /Q logs\\*"); -#else - auto rv = system("mkdir -p latency_logs"); - if (rv != 0) - { - throw std::runtime_error("Failed to mkdir -p latency_logs"); - } - rv = system("rm -f latency_logs/*"); - if (rv != 0) - { - throw std::runtime_error("Failed to rm -f latency_logs/*"); - } -#endif -} - void bench_c_string(benchmark::State &state, std::shared_ptr logger) { const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus " @@ -83,8 +63,6 @@ int main(int argc, char *argv[]) size_t rotating_files = 5; int n_threads = benchmark::CPUInfo::Get().num_cpus; - prepare_logdir(); - // disabled loggers auto disabled_logger = std::make_shared("bench", std::make_shared()); disabled_logger->set_level(spdlog::level::off); diff --git a/bench/meson.build b/bench/meson.build index c2604271..c877b6ac 100644 --- a/bench/meson.build +++ b/bench/meson.build @@ -12,4 +12,3 @@ foreach i : bench_matrix benchmark('bench_' + i[0], bench_exe, args: i[2]) endforeach -run_command(find_program('mkdir'), meson.current_build_dir() + '/logs') diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index b5fc4060..458ca952 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -25,5 +25,3 @@ if(SPDLOG_BUILD_EXAMPLE_HO) target_link_libraries(example_header_only PRIVATE spdlog::spdlog_header_only) endif() -# Create logs directory -file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") diff --git a/example/meson.build b/example/meson.build index 7e29abb2..c37c4c3c 100644 --- a/example/meson.build +++ b/example/meson.build @@ -1,5 +1,4 @@ executable('example', 'example.cpp', dependencies: spdlog_dep) executable('example_header_only', 'example.cpp', dependencies: spdlog_headeronly_dep) -run_command(find_program('mkdir'), meson.current_build_dir() + '/logs') diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 440cb290..cd6b5480 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -29,9 +29,16 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) { close(); auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); + auto folder_name = os::dir_name(fname); _filename = fname; + for (int tries = 0; tries < open_tries; ++tries) { + if (!folder_name.empty()) + { + os::create_dir(folder_name); + } + if (!os::fopen_s(&fd_, fname, mode)) { return; @@ -129,5 +136,6 @@ SPDLOG_INLINE std::tuple file_helper::split_by_extension // finally - return a valid base and extension tuple return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index)); } + } // namespace details } // namespace spdlog diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index f436b0d1..98914eb4 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -196,7 +197,7 @@ SPDLOG_INLINE bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT #else auto attribs = GetFileAttributesA(filename.c_str()); #endif - return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY)); + return attribs != INVALID_FILE_ATTRIBUTES; #else // common linux/unix all have the stat system call struct stat buffer; return (::stat(filename.c_str(), &buffer) == 0); @@ -460,6 +461,72 @@ SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) } #endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) +// return true on success +SPDLOG_INLINE bool mkdir_(const filename_t &path) +{ +#ifdef _WIN32 +#ifdef SPDLOG_WCHAR_FILENAMES + return ::_wmkdir(path.c_str()) == 0; +#else + return ::_mkdir(path.c_str()) == 0; +#endif +#else + return ::mkdir(path.c_str(), mode_t(0755)) == 0; +#endif +} + +// create the given directory - and all directories leading to it +// return true on success +SPDLOG_INLINE bool create_dir(filename_t path) +{ + if (file_exists(path)) + { + return true; + } + using char_type = filename_t::value_type; + std::basic_istringstream istream(path); + filename_t token; + filename_t cur_dir; + char_type sep = '/'; + +#ifdef _WIN32 + // support forward slash in windows + std::replace(path.begin(), path.end(), char_type('\\'), sep); +#endif + while (std::getline(istream, token, sep)) + { + if (!token.empty()) + { + cur_dir += token; + if (!file_exists(cur_dir) && !mkdir_(cur_dir)) + { + return false; + } + } + cur_dir += sep; + } + + return true; +} + +// Return directory name from given path or empty string +// "abc/file" => "abc" +// "abc/" => "abc" +// "abc" => "" +// "abc///" => "abc" +SPDLOG_INLINE filename_t dir_name(filename_t path) +{ + using char_type = filename_t::value_type; + char_type sep = '/'; + +#ifdef _WIN32 + // support forward slash in windows + std::replace(path.begin(), path.end(), char_type('\\'), sep); +#endif + auto pos = path.find_last_of(sep); + return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; +} + } // namespace os } // namespace details } // namespace spdlog diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index a4a57b7f..0d6abd2f 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -89,6 +89,17 @@ bool in_terminal(FILE *file) SPDLOG_NOEXCEPT; void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target); #endif +// Return directory name from given path or empty string +// "abc/file" => "abc" +// "abc/" => "abc" +// "abc" => "" +// "abc///" => "abc" +filename_t dir_name(filename_t path); + +// Create a dir from the given path. +// Return true if succeeded or if this dir already exists. +bool create_dir(filename_t path); + } // namespace os } // namespace details } // namespace spdlog diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1cf8f805..fd90dd56 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -25,7 +25,8 @@ set(SPDLOG_UTESTS_SOURCES test_fmt_helper.cpp test_stdout_api.cpp test_dup_filter.cpp - test_backtrace.cpp) + test_backtrace.cpp + test_create_dir.cpp) if(NOT SPDLOG_NO_EXCEPTIONS) list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp) @@ -35,7 +36,6 @@ if(systemd_FOUND) list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp) endif() -file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") enable_testing() # The compiled library tests diff --git a/tests/meson.build b/tests/meson.build index 73d7b8f8..98b40797 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -35,7 +35,6 @@ if systemd_dep.found() global_test_deps += systemd_dep endif -run_command('mkdir', 'logs') # -------------------------------------- # --- Build the test executables --- # -------------------------------------- @@ -49,5 +48,3 @@ foreach i : test_matrix test_exe = executable(i[0], test_sources, dependencies: global_test_deps + [i[1]]) test('test_' + i[0], test_exe) endforeach - -run_command(find_program('mkdir'), meson.current_build_dir() + '/logs') \ No newline at end of file diff --git a/tests/test_async.cpp b/tests/test_async.cpp index 6f86cf6c..166ac21e 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -157,7 +157,7 @@ TEST_CASE("to_file", "[async]") prepare_logdir(); size_t messages = 1024; size_t tp_threads = 1; - std::string filename = "logs/async_test.log"; + std::string filename = "test_logs/async_test.log"; { auto file_sink = std::make_shared(filename, true); auto tp = std::make_shared(messages, tp_threads); @@ -179,7 +179,7 @@ TEST_CASE("to_file multi-workers", "[async]") prepare_logdir(); size_t messages = 1024 * 10; size_t tp_threads = 10; - std::string filename = "logs/async_test.log"; + std::string filename = "test_logs/async_test.log"; { auto file_sink = std::make_shared(filename, true); auto tp = std::make_shared(messages, tp_threads); diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp new file mode 100644 index 00000000..5e1a2659 --- /dev/null +++ b/tests/test_create_dir.cpp @@ -0,0 +1,27 @@ +/* + * This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE + */ +#include "includes.h" + +using spdlog::details::os::create_dir; +using spdlog::details::os::file_exists; + +void test_create_dir(const char *path, const char *normalized_path) +{ + auto rv = create_dir(path); + REQUIRE(rv == true); + REQUIRE(file_exists(normalized_path)); +} + +#include "spdlog/sinks/stdout_color_sinks.h" + +TEST_CASE("create_dir", "[create_dir]") +{ + prepare_logdir(); + test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); + test_create_dir("test_logs/dir1///dir2", "test_logs/dir1/dir2"); + test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"); + test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4"); + test_create_dir("./test_logs/dir1/dir2/dir99/../dir23", "test_logs/dir1/dir2/dir23"); + spdlog::drop("test-create-dir"); +} diff --git a/tests/test_daily_logger.cpp b/tests/test_daily_logger.cpp index c89ef171..cf2002a3 100644 --- a/tests/test_daily_logger.cpp +++ b/tests/test_daily_logger.cpp @@ -10,7 +10,7 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") prepare_logdir(); // calculate filename (time based) - std::string basename = "logs/daily_dateonly"; + std::string basename = "test_logs/daily_dateonly"; std::tm tm = spdlog::details::os::localtime(); spdlog::memory_buf_t w; fmt::format_to(w, "{}_{:04d}-{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); @@ -44,7 +44,7 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]") prepare_logdir(); // calculate filename (time based) - std::string basename = "logs/daily_dateonly"; + std::string basename = "test_logs/daily_dateonly"; std::tm tm = spdlog::details::os::localtime(); spdlog::memory_buf_t w; fmt::format_to(w, "{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); @@ -119,7 +119,7 @@ static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_ prepare_logdir(); - std::string basename = "logs/daily_rotate.txt"; + std::string basename = "test_logs/daily_rotate.txt"; daily_file_sink_st sink{basename, 2, 30, true, max_days}; // simulate messages with 24 intervals @@ -130,7 +130,7 @@ static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_ sink.log(create_msg(offset)); } - REQUIRE(count_files("logs") == static_cast(expected_n_files)); + REQUIRE(count_files("test_logs") == static_cast(expected_n_files)); } TEST_CASE("daily_logger rotate", "[daily_file_sink]") diff --git a/tests/test_errors.cpp b/tests/test_errors.cpp index 65185d3c..4fc40594 100644 --- a/tests/test_errors.cpp +++ b/tests/test_errors.cpp @@ -26,7 +26,7 @@ protected: TEST_CASE("default_error_handler", "[errors]]") { prepare_logdir(); - std::string filename = "logs/simple_log.txt"; + std::string filename = "test_logs/simple_log.txt"; auto logger = spdlog::create("test-error", filename, true); logger->set_pattern("%v"); @@ -43,7 +43,7 @@ struct custom_ex TEST_CASE("custom_error_handler", "[errors]]") { prepare_logdir(); - std::string filename = "logs/simple_log.txt"; + std::string filename = "test_logs/simple_log.txt"; auto logger = spdlog::create("logger", filename, true); logger->flush_on(spdlog::level::info); logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); @@ -75,15 +75,15 @@ TEST_CASE("async_error_handler", "[errors]]") prepare_logdir(); std::string err_msg("log failed with some msg"); - std::string filename = "logs/simple_async_log.txt"; + std::string filename = "test_logs/simple_async_log.txt"; { spdlog::init_thread_pool(128, 1); auto logger = spdlog::create_async("logger", filename, true); logger->set_error_handler([=](const std::string &) { - std::ofstream ofs("logs/custom_err.txt"); + std::ofstream ofs("test_logs/custom_err.txt"); if (!ofs) { - throw std::runtime_error("Failed open logs/custom_err.txt"); + throw std::runtime_error("Failed open test_logs/custom_err.txt"); } ofs << err_msg; }); @@ -94,7 +94,7 @@ TEST_CASE("async_error_handler", "[errors]]") } spdlog::init_thread_pool(128, 1); REQUIRE(count_lines(filename) == 2); - REQUIRE(file_contents("logs/custom_err.txt") == err_msg); + REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg); } // Make sure async error handler is executed @@ -103,12 +103,13 @@ TEST_CASE("async_error_handler2", "[errors]]") prepare_logdir(); std::string err_msg("This is async handler error message"); { + spdlog::details::os::create_dir("test_logs"); spdlog::init_thread_pool(128, 1); auto logger = spdlog::create_async("failed_logger"); logger->set_error_handler([=](const std::string &) { - std::ofstream ofs("logs/custom_err2.txt"); + std::ofstream ofs("test_logs/custom_err2.txt"); if (!ofs) - throw std::runtime_error("Failed open logs/custom_err2.txt"); + throw std::runtime_error("Failed open test_logs/custom_err2.txt"); ofs << err_msg; }); logger->info("Hello failure"); @@ -116,5 +117,5 @@ TEST_CASE("async_error_handler2", "[errors]]") } spdlog::init_thread_pool(128, 1); - REQUIRE(file_contents("logs/custom_err2.txt") == err_msg); + REQUIRE(file_contents("test_logs/custom_err2.txt") == err_msg); } diff --git a/tests/test_file_helper.cpp b/tests/test_file_helper.cpp index 74e3744e..85f62ae2 100644 --- a/tests/test_file_helper.cpp +++ b/tests/test_file_helper.cpp @@ -6,7 +6,7 @@ using spdlog::details::file_helper; using spdlog::details::log_msg; -static const std::string target_filename = "logs/file_helper_test.txt"; +static const std::string target_filename = "test_logs/file_helper_test.txt"; static void write_with_helper(file_helper &helper, size_t howmany) { diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index 7563cf65..a8f9b2bb 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -6,7 +6,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") { prepare_logdir(); - std::string filename = "logs/simple_log"; + std::string filename = "test_logs/simple_log"; auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); @@ -22,7 +22,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") TEST_CASE("flush_on", "[flush_on]]") { prepare_logdir(); - std::string filename = "logs/simple_log"; + std::string filename = "test_logs/simple_log"; auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); @@ -42,7 +42,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]") { prepare_logdir(); size_t max_size = 1024 * 10; - std::string basename = "logs/rotating_log"; + std::string basename = "test_logs/rotating_log"; auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0); for (int i = 0; i < 10; ++i) @@ -59,7 +59,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]") { prepare_logdir(); size_t max_size = 1024 * 10; - std::string basename = "logs/rotating_log"; + std::string basename = "test_logs/rotating_log"; { // make an initial logger to create the first output file diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index 26b03cdf..75f53ab2 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -12,7 +12,7 @@ TEST_CASE("debug and trace w/o format string", "[macros]]") { prepare_logdir(); - std::string filename = "logs/simple_log"; + std::string filename = "test_logs/simple_log"; auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); diff --git a/tests/utils.cpp b/tests/utils.cpp index 6c7f3662..426acb30 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -9,18 +9,12 @@ void prepare_logdir() { spdlog::drop_all(); #ifdef _WIN32 - system("if not exist logs mkdir logs"); - system("del /F /Q logs\\*"); + system("rmdir /S /Q test_logs") #else - auto rv = system("mkdir -p logs"); + auto rv = system("rm -rf test_logs"); if (rv != 0) { - throw std::runtime_error("Failed to mkdir -p logs"); - } - rv = system("rm -f logs/*"); - if (rv != 0) - { - throw std::runtime_error("Failed to rm -f logs/*"); + throw std::runtime_error("Failed to rm -rf test_logs"); } #endif } From 9a68bd8cc8d721cc1c3a988c62229679df7b88be Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 20 Oct 2019 17:48:13 +0300 Subject: [PATCH 16/96] Fixed missing include --- include/spdlog/details/os-inl.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 98914eb4..a111844c 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -43,6 +43,8 @@ #include #endif +#include // for _mkdir/_wmkdir + #else // unix #include From 5f3521b3d4a38b01b2d7856d55ef0939df3c695e Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 20 Oct 2019 17:55:13 +0300 Subject: [PATCH 17/96] Update utils.cpp --- tests/utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utils.cpp b/tests/utils.cpp index 426acb30..255a4fec 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -9,7 +9,7 @@ void prepare_logdir() { spdlog::drop_all(); #ifdef _WIN32 - system("rmdir /S /Q test_logs") + system("rmdir /S /Q test_logs"); #else auto rv = system("rm -rf test_logs"); if (rv != 0) From d9f726f2a5d43147b81642d3878e70c37d237268 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 20 Oct 2019 18:25:09 +0300 Subject: [PATCH 18/96] Add global namespace qualifiers to global function calls in os-inl.h --- include/spdlog/details/os-inl.h | 40 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index a111844c..c539f5bf 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -94,17 +94,17 @@ SPDLOG_INLINE std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT #ifdef _WIN32 std::tm tm; - localtime_s(&tm, &time_tt); + ::localtime_s(&tm, &time_tt); #else std::tm tm; - localtime_r(&time_tt, &tm); + ::localtime_r(&time_tt, &tm); #endif return tm; } SPDLOG_INLINE std::tm localtime() SPDLOG_NOEXCEPT { - std::time_t now_t = time(nullptr); + std::time_t now_t = ::time(nullptr); return localtime(now_t); } @@ -123,7 +123,7 @@ SPDLOG_INLINE std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT { - std::time_t now_t = time(nullptr); + std::time_t now_t = ::time(nullptr); return gmtime(now_t); } @@ -132,13 +132,13 @@ SPDLOG_INLINE void prevent_child_fd(FILE *f) #ifdef _WIN32 #if !defined(__cplusplus_winrt) - auto file_handle = reinterpret_cast(_get_osfhandle(_fileno(f))); + auto file_handle = reinterpret_cast(_get_osfhandle(::_fileno(f))); if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) SPDLOG_THROW(spdlog_ex("SetHandleInformation failed", errno)); #endif #else - auto fd = fileno(f); - if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) + auto fd = ::fileno(f); + if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { SPDLOG_THROW(spdlog_ex("fcntl with FD_CLOEXEC failed", errno)); } @@ -150,12 +150,12 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename { #ifdef _WIN32 #ifdef SPDLOG_WCHAR_FILENAMES - *fp = _wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); + *fp = ::_wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); #else - *fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); + *fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); #endif #else // unix - *fp = fopen((filename.c_str()), mode.c_str()); + *fp = ::fopen((filename.c_str()), mode.c_str()); #endif #ifdef SPDLOG_PREVENT_CHILD_FD @@ -170,7 +170,7 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT { #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) - return _wremove(filename.c_str()); + return ::_wremove(filename.c_str()); #else return std::remove(filename.c_str()); #endif @@ -184,7 +184,7 @@ SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT { #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) - return _wrename(filename1.c_str(), filename2.c_str()); + return ::_wrename(filename1.c_str(), filename2.c_str()); #else return std::rename(filename1.c_str(), filename2.c_str()); #endif @@ -195,9 +195,9 @@ SPDLOG_INLINE bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT { #ifdef _WIN32 #ifdef SPDLOG_WCHAR_FILENAMES - auto attribs = GetFileAttributesW(filename.c_str()); + auto attribs = ::GetFileAttributesW(filename.c_str()); #else - auto attribs = GetFileAttributesA(filename.c_str()); + auto attribs = ::GetFileAttributesA(filename.c_str()); #endif return attribs != INVALID_FILE_ATTRIBUTES; #else // common linux/unix all have the stat system call @@ -214,16 +214,16 @@ SPDLOG_INLINE size_t filesize(FILE *f) SPDLOG_THROW(spdlog_ex("Failed getting file size. fd is null")); } #if defined(_WIN32) && !defined(__CYGWIN__) - int fd = _fileno(f); + int fd = ::_fileno(f); #if _WIN64 // 64 bits - __int64 ret = _filelengthi64(fd); + __int64 ret = ::_filelengthi64(fd); if (ret >= 0) { return static_cast(ret); } #else // windows 32 bits - long ret = _filelength(fd); + long ret = ::_filelength(fd); if (ret >= 0) { return static_cast(ret); @@ -231,7 +231,7 @@ SPDLOG_INLINE size_t filesize(FILE *f) #endif #else // unix - int fd = fileno(f); + int fd = ::fileno(f); // 64 bits(but not in osx or cygwin, where fstat64 is deprecated) #if (defined(__linux__) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64)) struct stat64 st; @@ -258,10 +258,10 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) #ifdef _WIN32 #if _WIN32_WINNT < _WIN32_WINNT_WS08 TIME_ZONE_INFORMATION tzinfo; - auto rv = GetTimeZoneInformation(&tzinfo); + auto rv = ::GetTimeZoneInformation(&tzinfo); #else DYNAMIC_TIME_ZONE_INFORMATION tzinfo; - auto rv = GetDynamicTimeZoneInformation(&tzinfo); + auto rv = ::filnoGetDynamicTimeZoneInformation(&tzinfo); #endif if (rv == TIME_ZONE_ID_INVALID) SPDLOG_THROW(spdlog::spdlog_ex("Failed getting timezone info. ", errno)); From 8a638a95a076ee2fb244acf3d0b323509495e146 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 20 Oct 2019 18:31:04 +0300 Subject: [PATCH 19/96] Update os-inl.h --- include/spdlog/details/os-inl.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index c539f5bf..5d2db635 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -113,10 +113,10 @@ SPDLOG_INLINE std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT #ifdef _WIN32 std::tm tm; - gmtime_s(&tm, &time_tt); + ::gmtime_s(&tm, &time_tt); #else std::tm tm; - gmtime_r(&time_tt, &tm); + ::gmtime_r(&time_tt, &tm); #endif return tm; } @@ -261,7 +261,7 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) auto rv = ::GetTimeZoneInformation(&tzinfo); #else DYNAMIC_TIME_ZONE_INFORMATION tzinfo; - auto rv = ::filnoGetDynamicTimeZoneInformation(&tzinfo); + auto rv = ::GetDynamicTimeZoneInformation(&tzinfo); #endif if (rv == TIME_ZONE_ID_INVALID) SPDLOG_THROW(spdlog::spdlog_ex("Failed getting timezone info. ", errno)); @@ -327,15 +327,15 @@ SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT #if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21) #define SYS_gettid __NR_gettid #endif - return static_cast(syscall(SYS_gettid)); + return static_cast(::syscall(SYS_gettid)); #elif defined(_AIX) || defined(__DragonFly__) || defined(__FreeBSD__) - return static_cast(pthread_getthreadid_np()); + return static_cast(::pthread_getthreadid_np()); #elif defined(__NetBSD__) - return static_cast(_lwp_self()); + return static_cast(::_lwp_self()); #elif defined(__OpenBSD__) - return static_cast(getthrid()); + return static_cast(::getthrid()); #elif defined(__sun) - return static_cast(thr_self()); + return static_cast(::thr_self()); #elif __APPLE__ uint64_t tid; pthread_threadid_np(nullptr, &tid); @@ -420,9 +420,9 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT { #ifdef _WIN32 - return _isatty(_fileno(file)) != 0; + return ::_isatty(_fileno(file)) != 0; #else - return isatty(fileno(file)) != 0; + return ::isatty(fileno(file)) != 0; #endif } From 1271081865171443bb311df6a3fa6522c5352cd6 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 20 Oct 2019 19:08:47 +0300 Subject: [PATCH 20/96] Update os-inl.h --- include/spdlog/details/os-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 5d2db635..b237adeb 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -515,7 +515,7 @@ SPDLOG_INLINE bool create_dir(filename_t path) // "abc/file" => "abc" // "abc/" => "abc" // "abc" => "" -// "abc///" => "abc" +// "abc///" => "abc//" SPDLOG_INLINE filename_t dir_name(filename_t path) { using char_type = filename_t::value_type; From d3c6974e991cdcc645c19a1d4cc2db67be250ba9 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 20 Oct 2019 19:09:37 +0300 Subject: [PATCH 21/96] Update os.h --- include/spdlog/details/os.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 0d6abd2f..352b063f 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -93,7 +93,7 @@ void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target); // "abc/file" => "abc" // "abc/" => "abc" // "abc" => "" -// "abc///" => "abc" +// "abc///" => "abc//" filename_t dir_name(filename_t path); // Create a dir from the given path. From 31ed1339325be8572cd74f08d379e4421332360b Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 21 Oct 2019 13:42:25 +0300 Subject: [PATCH 22/96] Added dir_name tests --- tests/test_create_dir.cpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 5e1a2659..3849e2fe 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -23,5 +23,36 @@ TEST_CASE("create_dir", "[create_dir]") test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"); test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4"); test_create_dir("./test_logs/dir1/dir2/dir99/../dir23", "test_logs/dir1/dir2/dir23"); - spdlog::drop("test-create-dir"); +} + +TEST_CASE("dir_name", "[create_dir]") +{ + using spdlog::details::os::dir_name; + REQUIRE(dir_name("").empty()); + REQUIRE(dir_name("dir").empty()); + REQUIRE(dir_name("dir/") == "dir"); + REQUIRE(dir_name("dir///") == "dir//"); + REQUIRE(dir_name("dir/file") == "dir"); + REQUIRE(dir_name("dir/file.txt") == "dir"); + REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt"); + REQUIRE(dir_name("/dir/file.txt") == "/dir"); + REQUIRE(dir_name("//dir/file.txt") == "//dir"); + REQUIRE(dir_name("//dir/file.txt") == "//dir"); + REQUIRE(dir_name("../file.txt") == ".."); + REQUIRE(dir_name("./file.txt") == "."); +#ifdef WIN32 + REQUIRE(dir_name(R"(dir\)") == "dir"); + REQUIRE(dir_name(R"(dir\\\)") == "dir//"); + REQUIRE(dir_name(R"(dir\file)") == "dir"); + REQUIRE(dir_name(R"(dir\file.txt)") == "dir"); + REQUIRE(dir_name(R"(dir\file.txt\)") == "dir/file.txt"); + REQUIRE(dir_name(R"(\dir\file.txt)") == "/dir"); + REQUIRE(dir_name(R"(\\dir\file.txt)") == "//dir"); + REQUIRE(dir_name(R"(\\dir\file.txt)") == "//dir"); + REQUIRE(dir_name(R"(..\file.txt)") == ".."); + REQUIRE(dir_name(R"(.\file.txt)") == "."); + REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == "c://a/b/c/d"); + //REQUIRE(dir_name(R"(c:\\a)") == "c://"); +#endif + } From cee705ccd399d3dd4ae285f13c21eac064dcad97 Mon Sep 17 00:00:00 2001 From: Andrei-Florin BENCSIK Date: Thu, 24 Oct 2019 22:57:47 +0300 Subject: [PATCH 23/96] Minor cmake code duplication improvement Create a macro to add tests for both static/header only. The only differneces between these two are the libraries they link with and the target names. Created the simple macro: _spdlog_prepare_test( ) which does the work. Signed-off-by: Andrei-Florin BENCSIK --- tests/CMakeLists.txt | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1cf8f805..8079722d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -38,31 +38,25 @@ endif() file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") enable_testing() -# The compiled library tests -if(SPDLOG_BUILD_TESTS) - add_executable(spdlog-utests ${SPDLOG_UTESTS_SOURCES}) - spdlog_enable_warnings(spdlog-utests) - target_link_libraries(spdlog-utests PRIVATE spdlog) - +macro(_spdlog_prepare_test test_target spdlog_lib) + add_executable(${test_target} ${SPDLOG_UTESTS_SOURCES}) + spdlog_enable_warnings(${test_target}) + target_link_libraries(${test_target} PRIVATE ${spdlog_lib}) if(systemd_FOUND) - target_link_libraries(spdlog-utests PRIVATE ${systemd_LIBRARIES}) + target_link_libraries(${test_target} PRIVATE ${systemd_LIBRARIES}) endif() if(SPDLOG_SANITIZE_ADDRESS) - spdlog_enable_sanitizer(spdlog-utests) + spdlog_enable_sanitizer(${test_target}) endif() - add_test(NAME spdlog-utests COMMAND spdlog-utests) + add_test(NAME ${test_target} COMMAND ${test_target}) +endmacro() + +# The compiled library tests +if(SPDLOG_BUILD_TESTS) + _spdlog_prepare_test(spdlog-utests spdlog::spdlog) endif() # The header-only library version tests if(SPDLOG_BUILD_TESTS_HO) - add_executable(spdlog-utests-ho ${SPDLOG_UTESTS_SOURCES}) - spdlog_enable_warnings(spdlog-utests-ho) - target_link_libraries(spdlog-utests-ho PRIVATE spdlog::spdlog_header_only) - if(systemd_FOUND) - target_link_libraries(spdlog-utests-ho PRIVATE ${systemd_LIBRARIES}) - endif() - if(SPDLOG_SANITIZE_ADDRESS) - spdlog_set_address_sanitizer(spdlog-utests-ho) - endif() - add_test(NAME spdlog-utests-ho COMMAND spdlog-utests-ho) + _spdlog_prepare_test(spdlog-utests-ho spdlog::spdlog_header_only) endif() From 2cc620ef33f7299e0c0ff05c629df7ccf0d35ce8 Mon Sep 17 00:00:00 2001 From: Andrei-Florin BENCSIK Date: Fri, 25 Oct 2019 10:19:15 +0300 Subject: [PATCH 24/96] Function instead of macro Be more consistent with the existing code and with the naming _function -> function --- tests/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8079722d..55175bc1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -38,7 +38,7 @@ endif() file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") enable_testing() -macro(_spdlog_prepare_test test_target spdlog_lib) +function(spdlog_prepare_test test_target spdlog_lib) add_executable(${test_target} ${SPDLOG_UTESTS_SOURCES}) spdlog_enable_warnings(${test_target}) target_link_libraries(${test_target} PRIVATE ${spdlog_lib}) @@ -49,14 +49,14 @@ macro(_spdlog_prepare_test test_target spdlog_lib) spdlog_enable_sanitizer(${test_target}) endif() add_test(NAME ${test_target} COMMAND ${test_target}) -endmacro() +endfunction() # The compiled library tests if(SPDLOG_BUILD_TESTS) - _spdlog_prepare_test(spdlog-utests spdlog::spdlog) + spdlog_prepare_test(spdlog-utests spdlog::spdlog) endif() # The header-only library version tests if(SPDLOG_BUILD_TESTS_HO) - _spdlog_prepare_test(spdlog-utests-ho spdlog::spdlog_header_only) + spdlog_prepare_test(spdlog-utests-ho spdlog::spdlog_header_only) endif() From e9d42e059f4381ce9402191293378adc5bdf2d2c Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 12:52:39 +0300 Subject: [PATCH 25/96] // support forward slash in windows --- include/spdlog/details/os-inl.h | 18 +++++++----------- include/spdlog/details/os.h | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index b237adeb..590e35a1 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -488,14 +488,13 @@ SPDLOG_INLINE bool create_dir(filename_t path) using char_type = filename_t::value_type; std::basic_istringstream istream(path); filename_t token; - filename_t cur_dir; - char_type sep = '/'; + filename_t cur_dir; #ifdef _WIN32 // support forward slash in windows - std::replace(path.begin(), path.end(), char_type('\\'), sep); + std::replace(path.begin(), path.end(), '/', folder_sep); #endif - while (std::getline(istream, token, sep)) + while (std::getline(istream, token, folder_sep)) { if (!token.empty()) { @@ -505,7 +504,7 @@ SPDLOG_INLINE bool create_dir(filename_t path) return false; } } - cur_dir += sep; + cur_dir += folder_sep; } return true; @@ -517,15 +516,12 @@ SPDLOG_INLINE bool create_dir(filename_t path) // "abc" => "" // "abc///" => "abc//" SPDLOG_INLINE filename_t dir_name(filename_t path) -{ - using char_type = filename_t::value_type; - char_type sep = '/'; - +{ #ifdef _WIN32 // support forward slash in windows - std::replace(path.begin(), path.end(), char_type('\\'), sep); + std::replace(path.begin(), path.end(), '/', folder_sep); #endif - auto pos = path.find_last_of(sep); + auto pos = path.find_last_of(folder_sep); return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; } diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 352b063f..2ab828ba 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -33,7 +33,7 @@ SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL; // folder separator #ifdef _WIN32 -const char folder_sep = '\\'; +static const char folder_sep = '\\'; #else SPDLOG_CONSTEXPR static const char folder_sep = '/'; #endif From 066087b383d281a64c9e3fbf5294e480bf4ab41a Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 14:14:50 +0300 Subject: [PATCH 26/96] Update create_dir --- include/spdlog/details/os-inl.h | 43 +++++++++++++------------ tests/test_create_dir.cpp | 56 ++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 44 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 590e35a1..1764eeeb 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -485,27 +485,30 @@ SPDLOG_INLINE bool create_dir(filename_t path) { return true; } - using char_type = filename_t::value_type; - std::basic_istringstream istream(path); - filename_t token; - filename_t cur_dir; - + #ifdef _WIN32 // support forward slash in windows std::replace(path.begin(), path.end(), '/', folder_sep); -#endif - while (std::getline(istream, token, folder_sep)) - { - if (!token.empty()) - { - cur_dir += token; - if (!file_exists(cur_dir) && !mkdir_(cur_dir)) - { - return false; - } - } - cur_dir += folder_sep; - } +#endif + + size_t search_offset = 0; + do + { + auto token_pos = path.find(folder_sep, search_offset); + // treat the entire path as a folder if no folder separator not found + if (token_pos == filename_t::npos) + { + token_pos = path.size(); + } + + auto subdir = path.substr(0, token_pos); + + if (!subdir.empty() && !file_exists(subdir) && !mkdir_(subdir)) + { + return false; // return error if failed creating dir + } + search_offset = token_pos + 1; + } while (search_offset < path.size()); return true; } @@ -517,10 +520,6 @@ SPDLOG_INLINE bool create_dir(filename_t path) // "abc///" => "abc//" SPDLOG_INLINE filename_t dir_name(filename_t path) { -#ifdef _WIN32 - // support forward slash in windows - std::replace(path.begin(), path.end(), '/', folder_sep); -#endif auto pos = path.find_last_of(folder_sep); return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; } diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 3849e2fe..1c20e5c1 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -8,6 +8,7 @@ using spdlog::details::os::file_exists; void test_create_dir(const char *path, const char *normalized_path) { + printf("Test Create dir %s\n", path); auto rv = create_dir(path); REQUIRE(rv == true); REQUIRE(file_exists(normalized_path)); @@ -18,11 +19,23 @@ void test_create_dir(const char *path, const char *normalized_path) TEST_CASE("create_dir", "[create_dir]") { prepare_logdir(); - test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); - test_create_dir("test_logs/dir1///dir2", "test_logs/dir1/dir2"); - test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"); - test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4"); - test_create_dir("./test_logs/dir1/dir2/dir99/../dir23", "test_logs/dir1/dir2/dir23"); +#ifdef WIN32 + test_create_dir("test_logs/dir1/dir1", "test_logs\\dir1\\dir1"); + test_create_dir("test_logs/dir1/dir1", "test_logs\\dir1\\dir1"); //test existing + test_create_dir("test_logs/dir1///dir2//", "test_logs\\dir1\\dir2"); + test_create_dir("./test_logs/dir1/dir3", "test_logs\\dir1\\dir3"); + test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs\\dir1\\dir4"); + // test backslash + test_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222"); + test_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\"); + test_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23"); +#else + test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); + test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); // test existing + test_create_dir("test_logs/dir1///dir2", "test_logs/dir1/dir2"); + test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"); + test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4"); +#endif } TEST_CASE("dir_name", "[create_dir]") @@ -30,29 +43,28 @@ TEST_CASE("dir_name", "[create_dir]") using spdlog::details::os::dir_name; REQUIRE(dir_name("").empty()); REQUIRE(dir_name("dir").empty()); - REQUIRE(dir_name("dir/") == "dir"); - REQUIRE(dir_name("dir///") == "dir//"); - REQUIRE(dir_name("dir/file") == "dir"); - REQUIRE(dir_name("dir/file.txt") == "dir"); - REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt"); - REQUIRE(dir_name("/dir/file.txt") == "/dir"); - REQUIRE(dir_name("//dir/file.txt") == "//dir"); - REQUIRE(dir_name("//dir/file.txt") == "//dir"); - REQUIRE(dir_name("../file.txt") == ".."); - REQUIRE(dir_name("./file.txt") == "."); + #ifdef WIN32 REQUIRE(dir_name(R"(dir\)") == "dir"); - REQUIRE(dir_name(R"(dir\\\)") == "dir//"); + REQUIRE(dir_name(R"(dir\\\)") == R"(dir\\)"); REQUIRE(dir_name(R"(dir\file)") == "dir"); REQUIRE(dir_name(R"(dir\file.txt)") == "dir"); - REQUIRE(dir_name(R"(dir\file.txt\)") == "dir/file.txt"); - REQUIRE(dir_name(R"(\dir\file.txt)") == "/dir"); - REQUIRE(dir_name(R"(\\dir\file.txt)") == "//dir"); - REQUIRE(dir_name(R"(\\dir\file.txt)") == "//dir"); + REQUIRE(dir_name(R"(dir\file.txt\)") == R"(dir\file.txt)"); + REQUIRE(dir_name(R"(\dir\file.txt)") == R"(\dir)"); + REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)"); REQUIRE(dir_name(R"(..\file.txt)") == ".."); REQUIRE(dir_name(R"(.\file.txt)") == "."); - REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == "c://a/b/c/d"); - //REQUIRE(dir_name(R"(c:\\a)") == "c://"); + REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == R"(c:\\a\b\c\d)"); +#else + REQUIRE(dir_name("dir/") == "dir"); + REQUIRE(dir_name("dir///") == "dir//"); + REQUIRE(dir_name("dir/file") == "dir"); + REQUIRE(dir_name("dir/file.txt") == "dir"); + REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt"); + REQUIRE(dir_name("/dir/file.txt") == "/dir"); + REQUIRE(dir_name("//dir/file.txt") == "//dir"); + REQUIRE(dir_name("../file.txt") == ".."); + REQUIRE(dir_name("./file.txt") == "."); #endif } From a1f283946ec3102e2ec493d10cfcabe6d91af023 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 14:29:57 +0300 Subject: [PATCH 27/96] updated os::dir_name and tests --- build-branch/example/logs2/basic-log.txt | 0 build-branch/tests/Debug/filename_2019-10-25 | 0 .../tests/Debug/test_logs/custom_err2.txt | 1 + include/spdlog/details/os-inl.h | 6 +++- tests/test_create_dir.cpp | 33 +++++++++---------- 5 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 build-branch/example/logs2/basic-log.txt create mode 100644 build-branch/tests/Debug/filename_2019-10-25 create mode 100644 build-branch/tests/Debug/test_logs/custom_err2.txt diff --git a/build-branch/example/logs2/basic-log.txt b/build-branch/example/logs2/basic-log.txt new file mode 100644 index 00000000..e69de29b diff --git a/build-branch/tests/Debug/filename_2019-10-25 b/build-branch/tests/Debug/filename_2019-10-25 new file mode 100644 index 00000000..e69de29b diff --git a/build-branch/tests/Debug/test_logs/custom_err2.txt b/build-branch/tests/Debug/test_logs/custom_err2.txt new file mode 100644 index 00000000..58a61097 --- /dev/null +++ b/build-branch/tests/Debug/test_logs/custom_err2.txt @@ -0,0 +1 @@ +This is async handler error message \ No newline at end of file diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 1764eeeb..a24b8005 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -519,7 +519,11 @@ SPDLOG_INLINE bool create_dir(filename_t path) // "abc" => "" // "abc///" => "abc//" SPDLOG_INLINE filename_t dir_name(filename_t path) -{ +{ +#ifdef _WIN32 + // support forward slash in windows + std::replace(path.begin(), path.end(), '/', folder_sep); +#endif auto pos = path.find_last_of(folder_sep); return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; } diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 1c20e5c1..56f4282d 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -6,12 +6,11 @@ using spdlog::details::os::create_dir; using spdlog::details::os::file_exists; -void test_create_dir(const char *path, const char *normalized_path) -{ - printf("Test Create dir %s\n", path); +bool try_create_dir(const char *path, const char *normalized_path) +{ auto rv = create_dir(path); REQUIRE(rv == true); - REQUIRE(file_exists(normalized_path)); + return file_exists(normalized_path); } #include "spdlog/sinks/stdout_color_sinks.h" @@ -19,23 +18,21 @@ void test_create_dir(const char *path, const char *normalized_path) TEST_CASE("create_dir", "[create_dir]") { prepare_logdir(); + + REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); + REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); //test existing + REQUIRE(try_create_dir("test_logs/dir1///dir2//", "test_logs/dir1/dir2")); + REQUIRE(try_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3")); + REQUIRE(try_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4")); + #ifdef WIN32 - test_create_dir("test_logs/dir1/dir1", "test_logs\\dir1\\dir1"); - test_create_dir("test_logs/dir1/dir1", "test_logs\\dir1\\dir1"); //test existing - test_create_dir("test_logs/dir1///dir2//", "test_logs\\dir1\\dir2"); - test_create_dir("./test_logs/dir1/dir3", "test_logs\\dir1\\dir3"); - test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs\\dir1\\dir4"); // test backslash - test_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222"); - test_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\"); - test_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23"); -#else - test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); - test_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1"); // test existing - test_create_dir("test_logs/dir1///dir2", "test_logs/dir1/dir2"); - test_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3"); - test_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4"); + REQUIRE(try_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222")); + REQUIRE(try_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\")); + REQUIRE(try_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23")); + REQUIRE(try_create_dir("test_logs\\..\\test_logs\\dir1\\dir5", "test_logs\\dir1\\dir5")); #endif + } TEST_CASE("dir_name", "[create_dir]") From bfc76278a9ab5ff1e5530d32ed24f263e55aa187 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 15:24:54 +0300 Subject: [PATCH 28/96] update tests --- build-branch/example/Debug/logs/mylog.txt | 2 ++ build-branch/example/Debug/mylog.txt | 1 + build-branch/example/logs2/basic-log.txt | 0 build-branch/example/mylog.txt | 2 ++ build-branch/tests/Debug/test_logs/custom_err2.txt | 1 - tests/test_create_dir.cpp | 11 +++++++++-- 6 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 build-branch/example/Debug/logs/mylog.txt create mode 100644 build-branch/example/Debug/mylog.txt delete mode 100644 build-branch/example/logs2/basic-log.txt create mode 100644 build-branch/example/mylog.txt delete mode 100644 build-branch/tests/Debug/test_logs/custom_err2.txt diff --git a/build-branch/example/Debug/logs/mylog.txt b/build-branch/example/Debug/logs/mylog.txt new file mode 100644 index 00000000..040f5132 --- /dev/null +++ b/build-branch/example/Debug/logs/mylog.txt @@ -0,0 +1,2 @@ +[2019-10-25 15:16:33.502] [LOGGER] [info] Hello +[2019-10-25 15:16:41.712] [LOGGER] [info] Hello diff --git a/build-branch/example/Debug/mylog.txt b/build-branch/example/Debug/mylog.txt new file mode 100644 index 00000000..f4e0ab68 --- /dev/null +++ b/build-branch/example/Debug/mylog.txt @@ -0,0 +1 @@ +[2019-10-25 15:16:58.845] [LOGGER] [info] Hello diff --git a/build-branch/example/logs2/basic-log.txt b/build-branch/example/logs2/basic-log.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/build-branch/example/mylog.txt b/build-branch/example/mylog.txt new file mode 100644 index 00000000..a7126955 --- /dev/null +++ b/build-branch/example/mylog.txt @@ -0,0 +1,2 @@ +[2019-10-25 15:16:53.945] [LOGGER] [info] Hello +[2019-10-25 15:18:50.127] [LOGGER] [info] Hello diff --git a/build-branch/tests/Debug/test_logs/custom_err2.txt b/build-branch/tests/Debug/test_logs/custom_err2.txt deleted file mode 100644 index 58a61097..00000000 --- a/build-branch/tests/Debug/test_logs/custom_err2.txt +++ /dev/null @@ -1 +0,0 @@ -This is async handler error message \ No newline at end of file diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 56f4282d..9ce13675 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -13,7 +13,6 @@ bool try_create_dir(const char *path, const char *normalized_path) return file_exists(normalized_path); } -#include "spdlog/sinks/stdout_color_sinks.h" TEST_CASE("create_dir", "[create_dir]") { @@ -26,7 +25,7 @@ TEST_CASE("create_dir", "[create_dir]") REQUIRE(try_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4")); #ifdef WIN32 - // test backslash + // test backslash folder separator REQUIRE(try_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222")); REQUIRE(try_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\")); REQUIRE(try_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23")); @@ -45,13 +44,21 @@ TEST_CASE("dir_name", "[create_dir]") REQUIRE(dir_name(R"(dir\)") == "dir"); REQUIRE(dir_name(R"(dir\\\)") == R"(dir\\)"); REQUIRE(dir_name(R"(dir\file)") == "dir"); + REQUIRE(dir_name(R"(dir/file)") == "dir"); REQUIRE(dir_name(R"(dir\file.txt)") == "dir"); + REQUIRE(dir_name(R"(dir/file)") == "dir"); REQUIRE(dir_name(R"(dir\file.txt\)") == R"(dir\file.txt)"); + REQUIRE(dir_name(R"(dir/file.txt/)") == R"(dir\file.txt)"); REQUIRE(dir_name(R"(\dir\file.txt)") == R"(\dir)"); + REQUIRE(dir_name(R"(/dir/file.txt)") == R"(\dir)"); REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)"); + REQUIRE(dir_name(R"(//dir/file.txt)") == R"(\\dir)"); REQUIRE(dir_name(R"(..\file.txt)") == ".."); + REQUIRE(dir_name(R"(../file.txt)") == ".."); REQUIRE(dir_name(R"(.\file.txt)") == "."); + REQUIRE(dir_name(R"(./file.txt)") == "."); REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == R"(c:\\a\b\c\d)"); + REQUIRE(dir_name(R"(c://a/b/c/d/file.txt)") == R"(c:\\a\b\c\d)"); #else REQUIRE(dir_name("dir/") == "dir"); REQUIRE(dir_name("dir///") == "dir//"); From c40555c0ac2c2f067fcd954a89fd1b2d2fa9f304 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 15:44:53 +0300 Subject: [PATCH 29/96] clang-format --- include/spdlog/details/os-inl.h | 52 +++++++++++++-------------- tests/test_create_dir.cpp | 63 ++++++++++++++++----------------- 2 files changed, 56 insertions(+), 59 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index a24b8005..31b20cea 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -485,30 +485,30 @@ SPDLOG_INLINE bool create_dir(filename_t path) { return true; } - + #ifdef _WIN32 // support forward slash in windows - std::replace(path.begin(), path.end(), '/', folder_sep); -#endif - - size_t search_offset = 0; - do - { - auto token_pos = path.find(folder_sep, search_offset); - // treat the entire path as a folder if no folder separator not found - if (token_pos == filename_t::npos) - { - token_pos = path.size(); - } - - auto subdir = path.substr(0, token_pos); - - if (!subdir.empty() && !file_exists(subdir) && !mkdir_(subdir)) - { - return false; // return error if failed creating dir - } - search_offset = token_pos + 1; - } while (search_offset < path.size()); + std::replace(path.begin(), path.end(), '/', folder_sep); +#endif + + size_t search_offset = 0; + do + { + auto token_pos = path.find(folder_sep, search_offset); + // treat the entire path as a folder if no folder separator not found + if (token_pos == filename_t::npos) + { + token_pos = path.size(); + } + + auto subdir = path.substr(0, token_pos); + + if (!subdir.empty() && !file_exists(subdir) && !mkdir_(subdir)) + { + return false; // return error if failed creating dir + } + search_offset = token_pos + 1; + } while (search_offset < path.size()); return true; } @@ -519,11 +519,11 @@ SPDLOG_INLINE bool create_dir(filename_t path) // "abc" => "" // "abc///" => "abc//" SPDLOG_INLINE filename_t dir_name(filename_t path) -{ +{ #ifdef _WIN32 - // support forward slash in windows - std::replace(path.begin(), path.end(), '/', folder_sep); -#endif + // support forward slash in windows + std::replace(path.begin(), path.end(), '/', folder_sep); +#endif auto pos = path.find_last_of(folder_sep); return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; } diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 9ce13675..3e428d74 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -7,31 +7,29 @@ using spdlog::details::os::create_dir; using spdlog::details::os::file_exists; bool try_create_dir(const char *path, const char *normalized_path) -{ +{ auto rv = create_dir(path); REQUIRE(rv == true); return file_exists(normalized_path); } - TEST_CASE("create_dir", "[create_dir]") { prepare_logdir(); - REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); - REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); //test existing - REQUIRE(try_create_dir("test_logs/dir1///dir2//", "test_logs/dir1/dir2")); - REQUIRE(try_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3")); - REQUIRE(try_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4")); + REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); + REQUIRE(try_create_dir("test_logs/dir1/dir1", "test_logs/dir1/dir1")); // test existing + REQUIRE(try_create_dir("test_logs/dir1///dir2//", "test_logs/dir1/dir2")); + REQUIRE(try_create_dir("./test_logs/dir1/dir3", "test_logs/dir1/dir3")); + REQUIRE(try_create_dir("test_logs/../test_logs/dir1/dir4", "test_logs/dir1/dir4")); #ifdef WIN32 - // test backslash folder separator - REQUIRE(try_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222")); - REQUIRE(try_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\")); - REQUIRE(try_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23")); - REQUIRE(try_create_dir("test_logs\\..\\test_logs\\dir1\\dir5", "test_logs\\dir1\\dir5")); + // test backslash folder separator + REQUIRE(try_create_dir("test_logs\\dir1\\dir222", "test_logs\\dir1\\dir222")); + REQUIRE(try_create_dir("test_logs\\dir1\\dir223\\", "test_logs\\dir1\\dir223\\")); + REQUIRE(try_create_dir(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23", "test_logs\\dir1\\dir2\\dir23")); + REQUIRE(try_create_dir("test_logs\\..\\test_logs\\dir1\\dir5", "test_logs\\dir1\\dir5")); #endif - } TEST_CASE("dir_name", "[create_dir]") @@ -39,36 +37,35 @@ TEST_CASE("dir_name", "[create_dir]") using spdlog::details::os::dir_name; REQUIRE(dir_name("").empty()); REQUIRE(dir_name("dir").empty()); - + #ifdef WIN32 REQUIRE(dir_name(R"(dir\)") == "dir"); REQUIRE(dir_name(R"(dir\\\)") == R"(dir\\)"); REQUIRE(dir_name(R"(dir\file)") == "dir"); - REQUIRE(dir_name(R"(dir/file)") == "dir"); + REQUIRE(dir_name(R"(dir/file)") == "dir"); REQUIRE(dir_name(R"(dir\file.txt)") == "dir"); - REQUIRE(dir_name(R"(dir/file)") == "dir"); + REQUIRE(dir_name(R"(dir/file)") == "dir"); REQUIRE(dir_name(R"(dir\file.txt\)") == R"(dir\file.txt)"); - REQUIRE(dir_name(R"(dir/file.txt/)") == R"(dir\file.txt)"); + REQUIRE(dir_name(R"(dir/file.txt/)") == R"(dir\file.txt)"); REQUIRE(dir_name(R"(\dir\file.txt)") == R"(\dir)"); - REQUIRE(dir_name(R"(/dir/file.txt)") == R"(\dir)"); - REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)"); - REQUIRE(dir_name(R"(//dir/file.txt)") == R"(\\dir)"); + REQUIRE(dir_name(R"(/dir/file.txt)") == R"(\dir)"); + REQUIRE(dir_name(R"(\\dir\file.txt)") == R"(\\dir)"); + REQUIRE(dir_name(R"(//dir/file.txt)") == R"(\\dir)"); REQUIRE(dir_name(R"(..\file.txt)") == ".."); - REQUIRE(dir_name(R"(../file.txt)") == ".."); + REQUIRE(dir_name(R"(../file.txt)") == ".."); REQUIRE(dir_name(R"(.\file.txt)") == "."); - REQUIRE(dir_name(R"(./file.txt)") == "."); + REQUIRE(dir_name(R"(./file.txt)") == "."); REQUIRE(dir_name(R"(c:\\a\b\c\d\file.txt)") == R"(c:\\a\b\c\d)"); - REQUIRE(dir_name(R"(c://a/b/c/d/file.txt)") == R"(c:\\a\b\c\d)"); + REQUIRE(dir_name(R"(c://a/b/c/d/file.txt)") == R"(c:\\a\b\c\d)"); #else - REQUIRE(dir_name("dir/") == "dir"); - REQUIRE(dir_name("dir///") == "dir//"); - REQUIRE(dir_name("dir/file") == "dir"); - REQUIRE(dir_name("dir/file.txt") == "dir"); - REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt"); - REQUIRE(dir_name("/dir/file.txt") == "/dir"); - REQUIRE(dir_name("//dir/file.txt") == "//dir"); - REQUIRE(dir_name("../file.txt") == ".."); - REQUIRE(dir_name("./file.txt") == "."); + REQUIRE(dir_name("dir/") == "dir"); + REQUIRE(dir_name("dir///") == "dir//"); + REQUIRE(dir_name("dir/file") == "dir"); + REQUIRE(dir_name("dir/file.txt") == "dir"); + REQUIRE(dir_name("dir/file.txt/") == "dir/file.txt"); + REQUIRE(dir_name("/dir/file.txt") == "/dir"); + REQUIRE(dir_name("//dir/file.txt") == "//dir"); + REQUIRE(dir_name("../file.txt") == ".."); + REQUIRE(dir_name("./file.txt") == "."); #endif - } From dbe5c17a9610cecb2dcab311a4b9844bfe94cf83 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 15:56:23 +0300 Subject: [PATCH 30/96] Renamed file_exists()->path_exists() --- include/spdlog/details/file_helper-inl.h | 5 ----- include/spdlog/details/file_helper.h | 1 - include/spdlog/details/os-inl.h | 10 +++++----- include/spdlog/details/os.h | 2 +- include/spdlog/sinks/rotating_file_sink-inl.h | 3 ++- tests/test_create_dir.cpp | 4 ++-- tests/test_file_helper.cpp | 8 -------- 7 files changed, 10 insertions(+), 23 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index cd6b5480..98ef82fa 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -97,11 +97,6 @@ SPDLOG_INLINE const filename_t &file_helper::filename() const return _filename; } -SPDLOG_INLINE bool file_helper::file_exists(const filename_t &fname) -{ - return os::file_exists(fname); -} - // // return file path and its extension: // diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 990d99e3..89d6b244 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -29,7 +29,6 @@ public: void write(const memory_buf_t &buf); size_t size() const; const filename_t &filename() const; - static bool file_exists(const filename_t &fname); // // return file path and its extension: diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 31b20cea..b5669ac7 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -178,7 +178,7 @@ SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT { - return file_exists(filename) ? remove(filename) : 0; + return path_exists(filename) ? remove(filename) : 0; } SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT @@ -190,8 +190,8 @@ SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename #endif } -// Return true if file exists -SPDLOG_INLINE bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT +// Return true if path exists (file or directory) +SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT { #ifdef _WIN32 #ifdef SPDLOG_WCHAR_FILENAMES @@ -481,7 +481,7 @@ SPDLOG_INLINE bool mkdir_(const filename_t &path) // return true on success SPDLOG_INLINE bool create_dir(filename_t path) { - if (file_exists(path)) + if (path_exists(path)) { return true; } @@ -503,7 +503,7 @@ SPDLOG_INLINE bool create_dir(filename_t path) auto subdir = path.substr(0, token_pos); - if (!subdir.empty() && !file_exists(subdir) && !mkdir_(subdir)) + if (!subdir.empty() && !path_exists(subdir) && !mkdir_(subdir)) { return false; // return error if failed creating dir } diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 2ab828ba..e275a40f 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -53,7 +53,7 @@ int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT; int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT; // Return if file exists. -bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT; +bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT; // Return file size according to open FILE* object size_t filesize(FILE *f); diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index 15443c5a..82347001 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -88,11 +88,12 @@ template SPDLOG_INLINE void rotating_file_sink::rotate_() { using details::os::filename_to_str; + using details::os::path_exists; file_helper_.close(); for (auto i = max_files_; i > 0; --i) { filename_t src = calc_filename(base_filename_, i - 1); - if (!details::file_helper::file_exists(src)) + if (!path_exists(src)) { continue; } diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 3e428d74..a41c63a5 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -4,13 +4,13 @@ #include "includes.h" using spdlog::details::os::create_dir; -using spdlog::details::os::file_exists; +using spdlog::details::os::path_exists; bool try_create_dir(const char *path, const char *normalized_path) { auto rv = create_dir(path); REQUIRE(rv == true); - return file_exists(normalized_path); + return path_exists(normalized_path); } TEST_CASE("create_dir", "[create_dir]") diff --git a/tests/test_file_helper.cpp b/tests/test_file_helper.cpp index 85f62ae2..0aaa4ce6 100644 --- a/tests/test_file_helper.cpp +++ b/tests/test_file_helper.cpp @@ -38,14 +38,6 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]") REQUIRE(get_filesize(target_filename) == expected_size); } -TEST_CASE("file_helper_exists", "[file_helper::file_exists()]]") -{ - prepare_logdir(); - REQUIRE(!file_helper::file_exists(target_filename)); - file_helper helper; - helper.open(target_filename); - REQUIRE(file_helper::file_exists(target_filename)); -} TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]") { From a4602021d86a394df6b0e790e3ce0cbc0b34258c Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:04:07 +0300 Subject: [PATCH 31/96] Renamed private members of file_helper --- include/spdlog/details/file_helper-inl.h | 18 +++++++++--------- include/spdlog/details/file_helper.h | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 98ef82fa..e7161132 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -30,9 +30,9 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) close(); auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); auto folder_name = os::dir_name(fname); - _filename = fname; + filename_ = fname; - for (int tries = 0; tries < open_tries; ++tries) + for (int tries = 0; tries < open_tries_; ++tries) { if (!folder_name.empty()) { @@ -44,19 +44,19 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) return; } - details::os::sleep_for_millis(open_interval); + details::os::sleep_for_millis(open_interval_); } - SPDLOG_THROW(spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno)); + SPDLOG_THROW(spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno)); } SPDLOG_INLINE void file_helper::reopen(bool truncate) { - if (_filename.empty()) + if (filename_.empty()) { SPDLOG_THROW(spdlog_ex("Failed re opening file - was not opened before")); } - open(_filename, truncate); + this->open(filename_, truncate); } SPDLOG_INLINE void file_helper::flush() @@ -79,7 +79,7 @@ SPDLOG_INLINE void file_helper::write(const memory_buf_t &buf) auto data = buf.data(); if (std::fwrite(data, 1, msg_size, fd_) != msg_size) { - SPDLOG_THROW(spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno)); + SPDLOG_THROW(spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno)); } } @@ -87,14 +87,14 @@ SPDLOG_INLINE size_t file_helper::size() const { if (fd_ == nullptr) { - SPDLOG_THROW(spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename))); + SPDLOG_THROW(spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(filename_))); } return os::filesize(fd_); } SPDLOG_INLINE const filename_t &file_helper::filename() const { - return _filename; + return filename_; } // diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 89d6b244..5ac4c27b 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -46,10 +46,10 @@ public: static std::tuple split_by_extension(const filename_t &fname); private: - const int open_tries = 5; - const int open_interval = 10; + const int open_tries_ = 5; + const int open_interval_ = 10; std::FILE *fd_{nullptr}; - filename_t _filename; + filename_t filename_; }; } // namespace details } // namespace spdlog From 88335bd92ecb8c9714be738e88032a7210f01055 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:05:12 +0300 Subject: [PATCH 32/96] clang-format --- tests/test_file_helper.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_file_helper.cpp b/tests/test_file_helper.cpp index 0aaa4ce6..3a3545fe 100644 --- a/tests/test_file_helper.cpp +++ b/tests/test_file_helper.cpp @@ -38,7 +38,6 @@ TEST_CASE("file_helper_size", "[file_helper::size()]]") REQUIRE(get_filesize(target_filename) == expected_size); } - TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]") { prepare_logdir(); From bd92c23addf3b15390f7703f84d8259fc4698b7d Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:07:10 +0300 Subject: [PATCH 33/96] comment --- include/spdlog/details/file_helper-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index e7161132..eabc9aaa 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -36,7 +36,7 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) { if (!folder_name.empty()) { - os::create_dir(folder_name); + os::create_dir(folder_name); // will not created if already exists } if (!os::fopen_s(&fd_, fname, mode)) From c19e325b83fcd42e40bd99b9325d7c60bf482c90 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:17:02 +0300 Subject: [PATCH 34/96] Added some tests for create_dir --- include/spdlog/details/file_helper-inl.h | 6 ++---- include/spdlog/details/os-inl.h | 7 ++++++- tests/test_create_dir.cpp | 8 ++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index eabc9aaa..be670347 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -34,10 +34,8 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) for (int tries = 0; tries < open_tries_; ++tries) { - if (!folder_name.empty()) - { - os::create_dir(folder_name); // will not created if already exists - } + // will not created if already exists or empty. + os::create_dir(folder_name); if (!os::fopen_s(&fd_, fname, mode)) { diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index b5669ac7..95a67917 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -478,7 +478,7 @@ SPDLOG_INLINE bool mkdir_(const filename_t &path) } // create the given directory - and all directories leading to it -// return true on success +// return true on success or if the directory already exists SPDLOG_INLINE bool create_dir(filename_t path) { if (path_exists(path)) @@ -486,6 +486,11 @@ SPDLOG_INLINE bool create_dir(filename_t path) return true; } + if(path.empty()) + { + return false; + } + #ifdef _WIN32 // support forward slash in windows std::replace(path.begin(), path.end(), '/', folder_sep); diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index a41c63a5..41f91ef3 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -32,6 +32,14 @@ TEST_CASE("create_dir", "[create_dir]") #endif } +TEST_CASE("create_invalid_dir", "[create_dir]") +{ + REQUIRE(create_dir("") == false); +#ifdef __linux__ + REQUIRE(create_dir("/proc/spdlog-utest") == false); +#endif +} + TEST_CASE("dir_name", "[create_dir]") { using spdlog::details::os::dir_name; From aac7dccf458625312a44b202905f4154c47a884d Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:17:55 +0300 Subject: [PATCH 35/96] comment --- include/spdlog/details/file_helper-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index be670347..1935ddf2 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -34,7 +34,7 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) for (int tries = 0; tries < open_tries_; ++tries) { - // will not created if already exists or empty. + // create if not empty and not exists already os::create_dir(folder_name); if (!os::fopen_s(&fd_, fname, mode)) From 594d226056edb6d099cf65c14e477038226542fe Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:20:24 +0300 Subject: [PATCH 36/96] update tests --- tests/test_create_dir.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 41f91ef3..b538e80e 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -35,6 +35,7 @@ TEST_CASE("create_dir", "[create_dir]") TEST_CASE("create_invalid_dir", "[create_dir]") { REQUIRE(create_dir("") == false); + REQUIRE(create_dir(spdlog::filename_t{}) == false); #ifdef __linux__ REQUIRE(create_dir("/proc/spdlog-utest") == false); #endif From 49eb9cbdd8ed5869f2210159d06252519bc6dc0b Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:26:21 +0300 Subject: [PATCH 37/96] Removed junk folder --- build-branch/example/Debug/logs/mylog.txt | 2 -- build-branch/example/Debug/mylog.txt | 1 - build-branch/example/mylog.txt | 2 -- build-branch/tests/Debug/filename_2019-10-25 | 0 4 files changed, 5 deletions(-) delete mode 100644 build-branch/example/Debug/logs/mylog.txt delete mode 100644 build-branch/example/Debug/mylog.txt delete mode 100644 build-branch/example/mylog.txt delete mode 100644 build-branch/tests/Debug/filename_2019-10-25 diff --git a/build-branch/example/Debug/logs/mylog.txt b/build-branch/example/Debug/logs/mylog.txt deleted file mode 100644 index 040f5132..00000000 --- a/build-branch/example/Debug/logs/mylog.txt +++ /dev/null @@ -1,2 +0,0 @@ -[2019-10-25 15:16:33.502] [LOGGER] [info] Hello -[2019-10-25 15:16:41.712] [LOGGER] [info] Hello diff --git a/build-branch/example/Debug/mylog.txt b/build-branch/example/Debug/mylog.txt deleted file mode 100644 index f4e0ab68..00000000 --- a/build-branch/example/Debug/mylog.txt +++ /dev/null @@ -1 +0,0 @@ -[2019-10-25 15:16:58.845] [LOGGER] [info] Hello diff --git a/build-branch/example/mylog.txt b/build-branch/example/mylog.txt deleted file mode 100644 index a7126955..00000000 --- a/build-branch/example/mylog.txt +++ /dev/null @@ -1,2 +0,0 @@ -[2019-10-25 15:16:53.945] [LOGGER] [info] Hello -[2019-10-25 15:18:50.127] [LOGGER] [info] Hello diff --git a/build-branch/tests/Debug/filename_2019-10-25 b/build-branch/tests/Debug/filename_2019-10-25 deleted file mode 100644 index e69de29b..00000000 From 0c60107e62222f0052a408269fda0a6744826b38 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:50:06 +0300 Subject: [PATCH 38/96] refactoed file_helper --- include/spdlog/details/file_helper-inl.h | 9 ++++----- include/spdlog/details/os-inl.h | 1 + 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 1935ddf2..cc21075c 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -28,15 +28,14 @@ SPDLOG_INLINE file_helper::~file_helper() SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) { close(); - auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); - auto folder_name = os::dir_name(fname); filename_ = fname; + // create containing folder if not empty string and not exists already + os::create_dir(os::dir_name(fname)); + + auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); for (int tries = 0; tries < open_tries_; ++tries) { - // create if not empty and not exists already - os::create_dir(folder_name); - if (!os::fopen_s(&fd_, fname, mode)) { return; diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 95a67917..35a8934a 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -499,6 +499,7 @@ SPDLOG_INLINE bool create_dir(filename_t path) size_t search_offset = 0; do { + fmt::string_view t; auto token_pos = path.find(folder_sep, search_offset); // treat the entire path as a folder if no folder separator not found if (token_pos == filename_t::npos) From 9f96545fa7ee3370047eac5e00937ea894944323 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:52:12 +0300 Subject: [PATCH 39/96] refactoed file_helper --- include/spdlog/details/file_helper-inl.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index cc21075c..3c1d8056 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -29,13 +29,12 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) { close(); filename_ = fname; - - // create containing folder if not empty string and not exists already - os::create_dir(os::dir_name(fname)); - auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); + for (int tries = 0; tries < open_tries_; ++tries) { + // create containing folder if not exists already. + os::create_dir(os::dir_name(fname)); if (!os::fopen_s(&fd_, fname, mode)) { return; From 05105155f8839a02879932aebcff2c7a48d9c346 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 25 Oct 2019 16:55:24 +0300 Subject: [PATCH 40/96] refactoed file_helper --- include/spdlog/details/file_helper-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 3c1d8056..669999b4 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -30,7 +30,7 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) close(); filename_ = fname; auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); - + for (int tries = 0; tries < open_tries_; ++tries) { // create containing folder if not exists already. From 491a2e873259dd7b8be70842d62a8a144af4a839 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Fri, 25 Oct 2019 18:59:02 +0300 Subject: [PATCH 41/96] Update os-inl.h --- include/spdlog/details/os-inl.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 35a8934a..cf11e622 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include From 5881fcb0d6e965273cf66c1c65c8feac52d7a1d8 Mon Sep 17 00:00:00 2001 From: Michael Mullin Date: Fri, 25 Oct 2019 14:47:38 -0400 Subject: [PATCH 42/96] Remove unused variable --- include/spdlog/details/os-inl.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index cf11e622..73a464c9 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -498,7 +498,6 @@ SPDLOG_INLINE bool create_dir(filename_t path) size_t search_offset = 0; do { - fmt::string_view t; auto token_pos = path.find(folder_sep, search_offset); // treat the entire path as a folder if no folder separator not found if (token_pos == filename_t::npos) From 288ea11534d2d0be8e1cd6c94d416cd7edd0552a Mon Sep 17 00:00:00 2001 From: David Yip Date: Mon, 28 Oct 2019 02:52:59 -0500 Subject: [PATCH 43/96] Use _FILE_OFFSET_BITS=64 when building tests on Linux 32-bit systems When cross-compiling spdlog from x86-64 to armhf, I ran spdlog-utests via qemu-arm and noticed that the "daily_logger rotate" test was failing because count_files always returned zero. Investigation of count_files revealed that readdir was returning nullptr immediately and setting errno to 75, i.e. "value too large for defined data type". I suspected this had something to do with some 64 vs. 32-bit thing, so I added _FILE_OFFSET_BITS=64 to the build and that seems to have made readdir happy. It might be safe to add _FILE_OFFSET_BITS=64 for all Linux builds, but it only seems to be necessary for the 32-bit case (which is a pretty small audience these days -- I'm only building for armhf to target a Raspberry Pi 3 running Raspbian, which runs in 32-bit mode). --- tests/CMakeLists.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5fd9900c..2b842203 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -48,7 +48,13 @@ function(spdlog_prepare_test test_target spdlog_lib) if(SPDLOG_SANITIZE_ADDRESS) spdlog_enable_sanitizer(${test_target}) endif() - add_test(NAME ${test_target} COMMAND ${test_target}) + add_test(NAME ${test_target} COMMAND ${test_target}) + + if (CMAKE_SYSTEM_NAME STREQUAL Linux) + if (CMAKE_SIZEOF_VOID_P EQUAL 4) + target_compile_definitions(${test_target} PRIVATE _FILE_OFFSET_BITS=64) + endif() + endif() endfunction() # The compiled library tests From 2b8afb38b73a457f2e160e25bad795c8b0d98613 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 28 Oct 2019 23:41:40 +0200 Subject: [PATCH 44/96] Revert pull #1288 --- tests/CMakeLists.txt | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2b842203..17529f2a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -49,12 +49,6 @@ function(spdlog_prepare_test test_target spdlog_lib) spdlog_enable_sanitizer(${test_target}) endif() add_test(NAME ${test_target} COMMAND ${test_target}) - - if (CMAKE_SYSTEM_NAME STREQUAL Linux) - if (CMAKE_SIZEOF_VOID_P EQUAL 4) - target_compile_definitions(${test_target} PRIVATE _FILE_OFFSET_BITS=64) - endif() - endif() endfunction() # The compiled library tests From a44560ddb60a5bfc0a8927207c798209883f4dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kundr=C3=A1t?= Date: Tue, 29 Oct 2019 20:39:26 +0100 Subject: [PATCH 45/96] journald: fix source file location This is what my manpage says, and what the original blog post [1] says as well. Also, `sd_journal_send` can add the location of its own invocation to the log. That's typically not what we want, so we have to suppress that feature and instead put whatever is inside the spdlog message into the journal. [1] http://0pointer.de/blog/projects/journal-submit.html --- include/spdlog/sinks/systemd_sink.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index bad6442d..a9867a6f 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -8,6 +8,9 @@ #include "spdlog/details/synchronous_factory.h" #include +#ifndef SD_JOURNAL_SUPPRESS_LOCATION +#define SD_JOURNAL_SUPPRESS_LOCATION +#endif #include namespace spdlog { @@ -63,7 +66,7 @@ protected: else { err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), - "SOURCE_FILE=%s", msg.source.filename, "SOURCE_LINE=%d", msg.source.line, "SOURCE_FUNC=%s", msg.source.funcname, nullptr); + "CODE_FILE=%s", msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr); } if (err) From 17513a6dce1b9c8f54885efa3d6764e5b46fe59d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kundr=C3=A1t?= Date: Wed, 30 Oct 2019 10:51:07 +0100 Subject: [PATCH 46/96] journald: structured output for logger's name Previously, the logger name was effectively lost. There were two choices on how to add it: - Via a formatter, which would mean that `journalctl` would not be able to filter against that. That would be suboptimal. - As a "syslog identifier". This means that `journalctl` will, by default, stop showing the daemon's executable name and replace that via the logger name. The PID is still shown, and if one would like to go back to the previous behavior, it is still possible via `journalctl -o with-unit`. I think that the second option is strictly better than the first one. fixes #1289 --- include/spdlog/sinks/systemd_sink.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index a9867a6f..2887e58e 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -61,11 +61,13 @@ protected: { // Note: function call inside '()' to avoid macro expansion err = (sd_journal_send)( - "MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), nullptr); + "MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), + "SYSLOG_IDENTIFIER=%.*s", static_cast(msg.logger_name.size()), msg.logger_name.data(), nullptr); } else { err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), + "SYSLOG_IDENTIFIER=%.*s", static_cast(msg.logger_name.size()), msg.logger_name.data(), "CODE_FILE=%s", msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr); } From eb4a169cfb5067d9304a62101029fd418888e860 Mon Sep 17 00:00:00 2001 From: Paul Kunysch Date: Sat, 2 Nov 2019 09:40:37 +0100 Subject: [PATCH 47/96] Improve log macros --- include/spdlog/spdlog.h | 4 ++-- tests/test_macros.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 392a91c5..401d22eb 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -288,8 +288,8 @@ inline void critical(wstring_view_t fmt, const Args &... args) #define SPDLOG_LOGGER_CALL(logger, level, ...) \ do \ { \ - if (logger->should_log(level) || logger->should_backtrace()) \ - logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); \ + if ((logger)->should_log(level) || (logger)->should_backtrace()) \ + (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); \ } while (0) #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index 75f53ab2..83a95dbd 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -40,6 +40,14 @@ TEST_CASE("disable param evaluation", "[macros]") SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated")); } +TEST_CASE("compile with reference to logger", "[macros]") +{ + auto logger = spdlog::create("refmacro"); + auto& ref = *logger; + SPDLOG_LOGGER_TRACE(&ref, "Test message 1"); + SPDLOG_LOGGER_DEBUG(&ref, "Test message 2"); +} + // ensure that even if right macro level is on- don't evaluate if the logger's level is not high enough TEST_CASE("disable param evaluation2", "[macros]") { From 15b393193ad06d839d94011a03960c16369262fd Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 3 Nov 2019 15:16:03 +0200 Subject: [PATCH 48/96] Replaced a forgotten try with SPDLOG_TRY --- include/spdlog/logger.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 6f6f341d..96847579 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -278,7 +278,7 @@ public: return; } - try + SPDLOG_TRY { memory_buf_t buf; details::os::wstr_to_utf8buf(msg, buf); From cae6c9ab361ffee0e2d7d2e4b89a333c88a1019d Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 3 Nov 2019 15:19:59 +0200 Subject: [PATCH 49/96] Removed lazy argument evaluation from macros --- include/spdlog/details/file_helper-inl.h | 2 +- include/spdlog/details/os-inl.h | 2 +- include/spdlog/sinks/systemd_sink.h | 7 +++---- include/spdlog/spdlog.h | 7 +------ tests/test_macros.cpp | 20 ++++++++++---------- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 669999b4..3c1d8056 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -30,7 +30,7 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) close(); filename_ = fname; auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab"); - + for (int tries = 0; tries < open_tries_; ++tries) { // create containing folder if not exists already. diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 73a464c9..f03cf310 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -485,7 +485,7 @@ SPDLOG_INLINE bool create_dir(filename_t path) return true; } - if(path.empty()) + if (path.empty()) { return false; } diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index 2887e58e..cee889a4 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -60,15 +60,14 @@ protected: if (msg.source.empty()) { // Note: function call inside '()' to avoid macro expansion - err = (sd_journal_send)( - "MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), + err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), "SYSLOG_IDENTIFIER=%.*s", static_cast(msg.logger_name.size()), msg.logger_name.data(), nullptr); } else { err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), - "SYSLOG_IDENTIFIER=%.*s", static_cast(msg.logger_name.size()), msg.logger_name.data(), - "CODE_FILE=%s", msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr); + "SYSLOG_IDENTIFIER=%.*s", static_cast(msg.logger_name.size()), msg.logger_name.data(), "CODE_FILE=%s", + msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr); } if (err) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 401d22eb..9e0af330 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -285,12 +285,7 @@ inline void critical(wstring_view_t fmt, const Args &... args) // SPDLOG_LEVEL_OFF // -#define SPDLOG_LOGGER_CALL(logger, level, ...) \ - do \ - { \ - if ((logger)->should_log(level) || (logger)->should_backtrace()) \ - (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); \ - } while (0) +#define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE #define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index 83a95dbd..b18c1cbf 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -40,20 +40,20 @@ TEST_CASE("disable param evaluation", "[macros]") SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated")); } -TEST_CASE("compile with reference to logger", "[macros]") +TEST_CASE("pass logger pointer", "[macros]") { auto logger = spdlog::create("refmacro"); - auto& ref = *logger; + auto &ref = *logger; SPDLOG_LOGGER_TRACE(&ref, "Test message 1"); SPDLOG_LOGGER_DEBUG(&ref, "Test message 2"); } // ensure that even if right macro level is on- don't evaluate if the logger's level is not high enough -TEST_CASE("disable param evaluation2", "[macros]") -{ - auto logger = std::make_shared("test-macro"); - logger->set_level(spdlog::level::off); - int x = 0; - SPDLOG_LOGGER_DEBUG(logger, "Test message {}", ++x); - REQUIRE(x == 0); -} +//TEST_CASE("disable param evaluation2", "[macros]") +//{ +// auto logger = std::make_shared("test-macro"); +// logger->set_level(spdlog::level::off); +// int x = 0; +// SPDLOG_LOGGER_DEBUG(logger, "Test message {}", ++x); +// REQUIRE(x == 0); +//} From 4037942a26d70c6af9cec9842179f88a9efe5947 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 3 Nov 2019 15:22:54 +0200 Subject: [PATCH 50/96] suppress modernize-use-trailing-return-type fron clang-tidy --- scripts/.clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/.clang-tidy b/scripts/.clang-tidy index 6c4c87c2..20766308 100644 --- a/scripts/.clang-tidy +++ b/scripts/.clang-tidy @@ -1,4 +1,4 @@ -Checks: 'modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*,-misc-non-private-member-variables-in-classes' +Checks: 'modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*,-misc-non-private-member-variables-in-classes, -modernize-use-trailing-return-type' WarningsAsErrors: '' HeaderFilterRegex: 'async.h|async_logger.h|common.h|details|formatter.h|logger.h|sinks|spdlog.h|tweakme.h|version.h' AnalyzeTemporaryDtors: false From 79468cf6769cca63bed2bb1f74bdbeae1c82da61 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 3 Nov 2019 17:00:12 +0200 Subject: [PATCH 51/96] Micro-optimze log_it_ --- include/spdlog/logger-inl.h | 6 +++--- include/spdlog/logger.h | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 4908ede0..84cbefbf 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -172,13 +172,13 @@ SPDLOG_INLINE std::shared_ptr logger::clone(std::string logger_name) } // protected methods -SPDLOG_INLINE void logger::log_it_(const details::log_msg &log_msg) +SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, bool log_enabled, bool traceback_enabled) {}(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled) { - if (should_log(log_msg.level)) + if (log_enabled) { sink_it_(log_msg); } - if (tracer_.enabled()) + if (traceback_enabled) { tracer_.push_back(log_msg); } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 96847579..314e5c42 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -76,7 +76,9 @@ public: template void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) { - if (!should_log(lvl) && !tracer_.enabled()) + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { return; } @@ -85,7 +87,7 @@ public: memory_buf_t buf; fmt::format_to(buf, fmt, args...); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - log_it_(log_msg); + log_it_(log_msg, log_enabled, traceback_enabled); } SPDLOG_LOGGER_CATCH() } @@ -142,13 +144,15 @@ public: template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - if (!should_log(lvl) && !tracer_.enabled()) + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { return; } details::log_msg log_msg(loc, name_, lvl, msg); - log_it_(log_msg); + log_it_(log_msg, log_enabled, traceback_enabled); } void log(level::level_enum lvl, string_view_t msg) @@ -209,7 +213,9 @@ public: template void log(source_loc loc, level::level_enum lvl, wstring_view_t fmt, const Args &... args) { - if (!should_log(lvl) && !tracer_.enabled()) + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { return; } @@ -222,7 +228,7 @@ public: 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())); - log_it_(log_msg); + log_it_(log_msg, log_enabled, traceback_enabled); } SPDLOG_LOGGER_CATCH() } @@ -273,7 +279,9 @@ public: template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - if (!should_log(lvl) && !tracer_.enabled()) + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { return; } @@ -283,7 +291,7 @@ public: memory_buf_t buf; details::os::wstr_to_utf8buf(msg, buf); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - log_it_(log_msg); + log_it_(log_msg, log_enabled, traceback_enabled); } SPDLOG_LOGGER_CATCH() } @@ -340,7 +348,7 @@ protected: // log the given message (if the given log level is high enough), // and save backtrace (if backtrace is enabled). - void log_it_(const details::log_msg &log_msg); + void log_it_(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled); virtual void sink_it_(const details::log_msg &msg); virtual void flush_(); void dump_backtrace_(); From 3ee4f2810d23f0b0fa4f85a607c6f41186ef07f7 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 3 Nov 2019 17:01:58 +0200 Subject: [PATCH 52/96] Micro-optimze log_it_ --- include/spdlog/logger-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 84cbefbf..021aae9d 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -172,7 +172,7 @@ SPDLOG_INLINE std::shared_ptr logger::clone(std::string logger_name) } // protected methods -SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, bool log_enabled, bool traceback_enabled) {}(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled) +SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, bool log_enabled, bool traceback_enabled) { if (log_enabled) { From bf40855825383a96bfa60f4594f29b4c36acf9cc Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 3 Nov 2019 17:04:34 +0200 Subject: [PATCH 53/96] Micro-optimize level checking by inlining --- include/spdlog/logger-inl.h | 10 ---------- include/spdlog/logger.h | 10 ++++++++-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 021aae9d..bc8232d8 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -64,16 +64,6 @@ SPDLOG_INLINE void swap(logger &a, logger &b) a.swap(b); } -SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const -{ - return msg_level >= level_.load(std::memory_order_relaxed); -} - -SPDLOG_INLINE bool logger::should_backtrace() const -{ - return tracer_.enabled(); -} - SPDLOG_INLINE void logger::set_level(level::level_enum log_level) { level_.store(log_level); diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 314e5c42..0e182875 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -299,10 +299,16 @@ public: #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT // return true logging is enabled for the given level. - bool should_log(level::level_enum msg_level) const; + bool should_log(level::level_enum msg_level) const + { + return msg_level >= level_.load(std::memory_order_relaxed); + } // return true if backtrace logging is enabled. - bool should_backtrace() const; + bool should_backtrace() const + { + return tracer_.enabled(); + } void set_level(level::level_enum log_level); From e3699070a494edc7592809e76d5fad8ecefdc14c Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 3 Nov 2019 17:07:37 +0200 Subject: [PATCH 54/96] clang-format --- include/spdlog/details/thread_pool-inl.h | 12 ++++-------- tests/test_macros.cpp | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/include/spdlog/details/thread_pool-inl.h b/include/spdlog/details/thread_pool-inl.h index 77d0b1fb..f9cf0e78 100644 --- a/include/spdlog/details/thread_pool-inl.h +++ b/include/spdlog/details/thread_pool-inl.h @@ -98,24 +98,20 @@ bool SPDLOG_INLINE thread_pool::process_next_msg_() switch (incoming_async_msg.msg_type) { - case async_msg_type::log: - { + case async_msg_type::log: { incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg); return true; } - case async_msg_type::flush: - { + case async_msg_type::flush: { incoming_async_msg.worker_ptr->backend_flush_(); return true; } - case async_msg_type::terminate: - { + case async_msg_type::terminate: { return false; } - default: - { + default: { assert(false && "Unexpected async_msg_type"); } } diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index b18c1cbf..4c621bb3 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -49,7 +49,7 @@ TEST_CASE("pass logger pointer", "[macros]") } // ensure that even if right macro level is on- don't evaluate if the logger's level is not high enough -//TEST_CASE("disable param evaluation2", "[macros]") +// TEST_CASE("disable param evaluation2", "[macros]") //{ // auto logger = std::make_shared("test-macro"); // logger->set_level(spdlog::level::off); From 0e09ecbaa5d90746f130c12ea4329d54b8e0d9ae Mon Sep 17 00:00:00 2001 From: Charles Milette Date: Sun, 3 Nov 2019 22:41:55 -0500 Subject: [PATCH 55/96] Correctly guard SetHandleInformation API call `__cplusplus_winrt` only detected C++/CX (which can be used without compiling for UWP, SetHandleInformation would be available in those cases), and did not detect native UWP C++. This patch fixes that by using the WINAPI_FAMILY_PARTITION macro in the Windows SDK headers in the same way those headers remove SetHandleInformation in UWP builds. --- include/spdlog/details/os-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index f03cf310..6f5ee52b 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -130,7 +130,7 @@ SPDLOG_INLINE void prevent_child_fd(FILE *f) { #ifdef _WIN32 -#if !defined(__cplusplus_winrt) +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM) auto file_handle = reinterpret_cast(_get_osfhandle(::_fileno(f))); if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) SPDLOG_THROW(spdlog_ex("SetHandleInformation failed", errno)); From 57085c892f9cc4cd36f9738109864a78ec7d7e3f Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 16:42:58 +0200 Subject: [PATCH 56/96] exclude from compilation prevent_child_fd() if SPDLOG_PREVENT_CHILD_FD not defined --- include/spdlog/details/os-inl.h | 6 +++--- include/spdlog/details/os.h | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 6f5ee52b..a6714ce0 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -126,15 +126,13 @@ SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT return gmtime(now_t); } +#ifdef SPDLOG_PREVENT_CHILD_FD SPDLOG_INLINE void prevent_child_fd(FILE *f) { - #ifdef _WIN32 -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM) auto file_handle = reinterpret_cast(_get_osfhandle(::_fileno(f))); if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) SPDLOG_THROW(spdlog_ex("SetHandleInformation failed", errno)); -#endif #else auto fd = ::fileno(f); if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) @@ -143,6 +141,7 @@ SPDLOG_INLINE void prevent_child_fd(FILE *f) } #endif } +#endif //SPDLOG_PREVENT_CHILD_FD // fopen_s on non windows for writing SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) @@ -158,6 +157,7 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename #endif #ifdef SPDLOG_PREVENT_CHILD_FD + // prevent child processes from inheriting log file descriptors if (*fp != nullptr) { prevent_child_fd(*fp); diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index e275a40f..0b1e6006 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -38,7 +38,9 @@ static const char folder_sep = '\\'; SPDLOG_CONSTEXPR static const char folder_sep = '/'; #endif +#ifdef SPDLOG_PREVENT_CHILD_FD void prevent_child_fd(FILE *f); +#endif // fopen_s on non windows for writing bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode); From dae1aeb1f737fa00797ce92e057a7e44bc92684f Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 16:48:54 +0200 Subject: [PATCH 57/96] clang-format --- include/spdlog/details/os-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index a6714ce0..af796740 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -141,7 +141,7 @@ SPDLOG_INLINE void prevent_child_fd(FILE *f) } #endif } -#endif //SPDLOG_PREVENT_CHILD_FD +#endif // SPDLOG_PREVENT_CHILD_FD // fopen_s on non windows for writing SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) From 18edb8bd63deb0f4660b7a29fa82afb9c9903527 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 17:19:18 +0200 Subject: [PATCH 58/96] Added tweakme options to CMakeLists.txt --- CMakeLists.txt | 53 +++++++++++++++++++++++++++++++++++++++----- tests/CMakeLists.txt | 8 ++++++- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5946b2a0..79f03392 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,19 +63,26 @@ option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF) # install options option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF) +option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF) + +# misc tweakme options if(WIN32) - option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF) - option(SPDLOG_WCHAR_FILENAMES "Support wchar filenames" OFF) + option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF) + option(SPDLOG_WCHAR_FILENAMES "Support wchar filenames" OFF) +endif() +if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + option(SPDLOG_CLOCK_COARSE "Use the much faster(but less accurate) CLOCK_REALTIME_COARSE instead of the regular clock," OFF) endif() -option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF) - +option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" OFF) +option(SPDLOG_NO_DATETIME, "Prevent spdlog from querying the clock on each log call if no datetime is needed" OFF) +option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each log call if thread id is not needed" OFF) +option(SPDLOG_NO_TLS "prevent spdlog from using thread local storage" OFF) +option(SPDLOG_NO_ATOMIC_LEVELS "prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently" OFF) find_package(Threads REQUIRED) - message(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) - #--------------------------------------------------------------------------------------- # Static/Shared library (shared not supported in windows yet) #--------------------------------------------------------------------------------------- @@ -140,6 +147,9 @@ if(SPDLOG_FMT_EXTERNAL) set(PKG_CONFIG_REQUIRES fmt) # add dependency to pkg-config endif() +#--------------------------------------------------------------------------------------- +# Misc definitions according to options +#--------------------------------------------------------------------------------------- if(SPDLOG_WCHAR_SUPPORT) target_compile_definitions(spdlog PUBLIC SPDLOG_WCHAR_TO_UTF8_SUPPORT) target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_WCHAR_TO_UTF8_SUPPORT) @@ -160,6 +170,37 @@ if(SPDLOG_WCHAR_SUPPORT) endif() endif() +if(SPDLOG_CLOCK_COARSE) + target_compile_definitions(spdlog PRIVATE SPDLOG_CLOCK_COARSE) + target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_CLOCK_COARSE) +endif() + +if(SPDLOG_PREVENT_CHILD_FD) + target_compile_definitions(spdlog PRIVATE SPDLOG_PREVENT_CHILD_FD) + target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_PREVENT_CHILD_FD) +endif() + + +if(SPDLOG_NO_DATETIME) + target_compile_definitions(spdlog PUBLIC SPDLOG_NO_DATETIME) + target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_NO_DATETIME) +endif() + +if(SPDLOG_NO_THREAD_ID) + target_compile_definitions(spdlog PRIVATE SPDLOG_NO_THREAD_ID) + target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_NO_THREAD_ID) +endif() + +if(SPDLOG_NO_TLS) + target_compile_definitions(spdlog PRIVATE SPDLOG_NO_TLS) + target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_NO_TLS) +endif() + +if(SPDLOG_NO_ATOMIC_LEVELS) + target_compile_definitions(spdlog PUBLIC SPDLOG_NO_ATOMIC_LEVELS) + target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_NO_ATOMIC_LEVELS) +endif() + #--------------------------------------------------------------------------------------- # Build binaries diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 17529f2a..c3b346cc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -24,7 +24,6 @@ set(SPDLOG_UTESTS_SOURCES test_sink.h test_fmt_helper.cpp test_stdout_api.cpp - test_dup_filter.cpp test_backtrace.cpp test_create_dir.cpp) @@ -36,6 +35,13 @@ if(systemd_FOUND) list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp) endif() +if(NOT SPDLOG_NO_DATETIME) + list(APPEND SPDLOG_UTESTS_SOURCES test_dup_filter.cpp) +endif() + + + + enable_testing() function(spdlog_prepare_test test_target spdlog_lib) From 10116b771708f086f6330cb95586dc7cec7c1a20 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 17:43:30 +0200 Subject: [PATCH 59/96] Removed SPDLOG_NO_DATETIME option --- CMakeLists.txt | 7 ------- include/spdlog/details/log_msg-inl.h | 3 --- include/spdlog/details/pattern_formatter-inl.h | 9 +-------- include/spdlog/sinks/daily_file_sink.h | 5 ----- include/spdlog/sinks/dup_filter_sink.h | 4 ---- include/spdlog/tweakme.h | 13 ------------- tests/CMakeLists.txt | 7 +------ 7 files changed, 2 insertions(+), 46 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 79f03392..bef4544e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,7 +76,6 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") endif() option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" OFF) -option(SPDLOG_NO_DATETIME, "Prevent spdlog from querying the clock on each log call if no datetime is needed" OFF) option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each log call if thread id is not needed" OFF) option(SPDLOG_NO_TLS "prevent spdlog from using thread local storage" OFF) option(SPDLOG_NO_ATOMIC_LEVELS "prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently" OFF) @@ -180,12 +179,6 @@ if(SPDLOG_PREVENT_CHILD_FD) target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_PREVENT_CHILD_FD) endif() - -if(SPDLOG_NO_DATETIME) - target_compile_definitions(spdlog PUBLIC SPDLOG_NO_DATETIME) - target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_NO_DATETIME) -endif() - if(SPDLOG_NO_THREAD_ID) target_compile_definitions(spdlog PRIVATE SPDLOG_NO_THREAD_ID) target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_NO_THREAD_ID) diff --git a/include/spdlog/details/log_msg-inl.h b/include/spdlog/details/log_msg-inl.h index cb130532..a47fe660 100644 --- a/include/spdlog/details/log_msg-inl.h +++ b/include/spdlog/details/log_msg-inl.h @@ -15,10 +15,7 @@ namespace details { SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, string_view_t logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg) : logger_name(logger_name) , level(lvl) -#ifndef SPDLOG_NO_DATETIME , time(os::now()) -#endif - #ifndef SPDLOG_NO_THREAD_ID , thread_id(os::thread_id()) #endif diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index 33a57a13..f97e2c2f 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -904,8 +904,6 @@ public: using std::chrono::milliseconds; using std::chrono::seconds; -#ifndef SPDLOG_NO_DATETIME - // cache the date/time part for the next second. auto duration = msg.time.time_since_epoch(); auto secs = duration_cast(duration); @@ -941,10 +939,6 @@ public: dest.push_back(']'); dest.push_back(' '); -#else // no datetime needed - (void)tm_time; -#endif - #ifndef SPDLOG_NO_NAME if (msg.logger_name.size() > 0) { @@ -1014,14 +1008,13 @@ SPDLOG_INLINE std::unique_ptr pattern_formatter::clone() const SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) { -#ifndef SPDLOG_NO_DATETIME auto secs = std::chrono::duration_cast(msg.time.time_since_epoch()); if (secs != last_log_secs_) { cached_tm_ = get_time_(msg); last_log_secs_ = secs; } -#endif + for (auto &f : formatters_) { f->format(msg, cached_tm_, dest); diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 83b63e1a..d200480f 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -78,12 +78,7 @@ public: protected: void sink_it_(const details::log_msg &msg) override { -#ifdef SPDLOG_NO_DATETIME - auto time = log_clock::now(); -#else auto time = msg.time; -#endif - bool should_rotate = time >= rotation_tp_; if (should_rotate) { diff --git a/include/spdlog/sinks/dup_filter_sink.h b/include/spdlog/sinks/dup_filter_sink.h index ab116a05..ab501387 100644 --- a/include/spdlog/sinks/dup_filter_sink.h +++ b/include/spdlog/sinks/dup_filter_sink.h @@ -33,10 +33,6 @@ // [2019-06-25 17:50:56.512] [logger] [info] Skipped 3 duplicate messages.. // [2019-06-25 17:50:56.512] [logger] [info] Different Hello -#ifdef SPDLOG_NO_DATETIME -#error "spdlog::sinks::dup_filter_sink: cannot work when SPDLOG_NO_DATETIME is defined" -#endif - namespace spdlog { namespace sinks { template diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index 2f3241b8..b67043e0 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -19,19 +19,6 @@ // #define SPDLOG_CLOCK_COARSE /////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// -// Uncomment if date/time logging is not needed and never appear in the log -// pattern. -// This will prevent spdlog from querying the clock on each log call. -// -// WARNING: If the log pattern contains any date/time while this flag is on, the -// result is undefined. -// You must set new pattern(spdlog::set_pattern(..") without any -// date/time in it -// -// #define SPDLOG_NO_DATETIME -/////////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////////// // Uncomment if thread id logging is not needed (i.e. no %t in the log pattern). // This will prevent spdlog from querying the thread id on each log call. diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c3b346cc..14c03287 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -22,6 +22,7 @@ set(SPDLOG_UTESTS_SOURCES main.cpp test_mpmc_q.cpp test_sink.h + test_dup_filter.cpp test_fmt_helper.cpp test_stdout_api.cpp test_backtrace.cpp @@ -35,12 +36,6 @@ if(systemd_FOUND) list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp) endif() -if(NOT SPDLOG_NO_DATETIME) - list(APPEND SPDLOG_UTESTS_SOURCES test_dup_filter.cpp) -endif() - - - enable_testing() From e6ce39f76e1b86e451ad1cec1c8134f79bb40aa3 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 17:46:59 +0200 Subject: [PATCH 60/96] comment --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bef4544e..e116daad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -147,7 +147,7 @@ if(SPDLOG_FMT_EXTERNAL) endif() #--------------------------------------------------------------------------------------- -# Misc definitions according to options +# Misc definitions according to tweak options #--------------------------------------------------------------------------------------- if(SPDLOG_WCHAR_SUPPORT) target_compile_definitions(spdlog PUBLIC SPDLOG_WCHAR_TO_UTF8_SUPPORT) From 3f3000008881d2a2c508462b075dbd255727d2fc Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 17:51:57 +0200 Subject: [PATCH 61/96] comment --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e116daad..75b063ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,7 +72,7 @@ if(WIN32) option(SPDLOG_WCHAR_FILENAMES "Support wchar filenames" OFF) endif() if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") - option(SPDLOG_CLOCK_COARSE "Use the much faster(but less accurate) CLOCK_REALTIME_COARSE instead of the regular clock," OFF) + option(SPDLOG_CLOCK_COARSE "Use the much faster (but much less accurate) CLOCK_REALTIME_COARSE instead of the regular clock," OFF) endif() option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" OFF) From bb1b24c1784f89451e0e6e66dd6adfb9a4adf84d Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 23:16:21 +0200 Subject: [PATCH 62/96] Fixed #1295 --- tests/meson.build | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/meson.build b/tests/meson.build index 98b40797..130a73c0 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -1,20 +1,20 @@ test_sources = files([ - 'test_file_helper.cpp', - 'test_file_logging.cpp', - 'test_daily_logger.cpp', - 'test_misc.cpp', - 'test_pattern_formatter.cpp', - 'test_async.cpp', - 'includes.h', - 'test_registry.cpp', - 'test_macros.cpp', - 'utils.cpp', - 'main.cpp', - 'test_mpmc_q.cpp', - 'test_fmt_helper.cpp', - 'test_stdout_api.cpp', - 'test_dup_filter.cpp', - 'test_backtrace.cpp' + 'test_file_helper.cpp', + 'test_file_logging.cpp', + 'test_daily_logger.cpp', + 'test_misc.cpp', + 'test_pattern_formatter.cpp', + 'test_async.cpp', + 'test_registry.cpp', + 'test_macros.cpp', + 'utils.cpp', + 'main.cpp', + 'test_mpmc_q.cpp', + 'test_dup_filter.cpp', + 'test_fmt_helper.cpp', + 'test_stdout_api.cpp', + 'test_backtrace.cpp', + 'test_create_dir.cpp' ]) @@ -46,5 +46,5 @@ test_matrix = [ foreach i : test_matrix test_exe = executable(i[0], test_sources, dependencies: global_test_deps + [i[1]]) - test('test_' + i[0], test_exe) + test('test_' + i[0], test_exe, is_parallel : false) endforeach From 21f7f781303d1d60b37c286487a0ee2476f8eadb Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 23:16:44 +0200 Subject: [PATCH 63/96] Update tests CMakeLists.txt --- tests/CMakeLists.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 14c03287..3184e9b6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -14,14 +14,11 @@ set(SPDLOG_UTESTS_SOURCES test_misc.cpp test_pattern_formatter.cpp test_async.cpp - includes.h test_registry.cpp test_macros.cpp utils.cpp - utils.h main.cpp test_mpmc_q.cpp - test_sink.h test_dup_filter.cpp test_fmt_helper.cpp test_stdout_api.cpp From c08191932040d47f06a113debcab84263da5da9b Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 23:19:02 +0200 Subject: [PATCH 64/96] Enabled tests under meson by default --- meson_options.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson_options.txt b/meson_options.txt index 3eea2b8f..57374b0e 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,6 +1,6 @@ option('external_fmt', type: 'boolean', value: false, description: 'Use external fmt package instead of the bundled') option('enable_examples', type: 'boolean', value: true, description: 'Build examples') option('enable_benchmarks', type: 'boolean', value: false, description: 'Build benchmarks') -option('enable_tests', type: 'boolean', value: false, description: 'Build tests') +option('enable_tests', type: 'boolean', value: true, description: 'Build tests') option('library_type', type: 'combo', choices: ['static', 'shared'], value: 'static', description: 'Library build type') option('no_exceptions', type: 'boolean', value: false, description: 'Disabled exceptions - abort() instead any error') From a13b0abb7d051c51689e3a43dc0fa0ac8048a68c Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 23:29:05 +0200 Subject: [PATCH 65/96] Simplify meson for tests --- tests/meson.build | 46 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/tests/meson.build b/tests/meson.build index 130a73c0..68672221 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -1,23 +1,22 @@ test_sources = files([ - 'test_file_helper.cpp', - 'test_file_logging.cpp', - 'test_daily_logger.cpp', - 'test_misc.cpp', - 'test_pattern_formatter.cpp', - 'test_async.cpp', - 'test_registry.cpp', - 'test_macros.cpp', - 'utils.cpp', - 'main.cpp', - 'test_mpmc_q.cpp', - 'test_dup_filter.cpp', - 'test_fmt_helper.cpp', - 'test_stdout_api.cpp', - 'test_backtrace.cpp', - 'test_create_dir.cpp' + 'test_file_helper.cpp', + 'test_file_logging.cpp', + 'test_daily_logger.cpp', + 'test_misc.cpp', + 'test_pattern_formatter.cpp', + 'test_async.cpp', + 'test_registry.cpp', + 'test_macros.cpp', + 'utils.cpp', + 'main.cpp', + 'test_mpmc_q.cpp', + 'test_dup_filter.cpp', + 'test_fmt_helper.cpp', + 'test_stdout_api.cpp', + 'test_backtrace.cpp', + 'test_create_dir.cpp' ]) - if not get_option('no_exceptions') test_sources += 'test_errors.cpp' endif @@ -38,13 +37,8 @@ endif # -------------------------------------- # --- Build the test executables --- # -------------------------------------- +test_exe = executable('spdlog-utests', test_sources, dependencies: global_test_deps + [spdlog_dep]) +test('test_spdlog', test_exe, is_parallel : false) -test_matrix = [ - ['spdlog-utests', spdlog_dep], - ['spdlog-utests-ho', spdlog_headeronly_dep], -] - -foreach i : test_matrix - test_exe = executable(i[0], test_sources, dependencies: global_test_deps + [i[1]]) - test('test_' + i[0], test_exe, is_parallel : false) -endforeach +test_exe = executable('spdlog-utests-ho', test_sources, dependencies: global_test_deps + [spdlog_headeronly_dep]) +test('test_spdlog-ho', test_exe, is_parallel : false) \ No newline at end of file From 4a4f13be466cc73edbb2631e37fd716be17a7f9b Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 4 Nov 2019 23:38:31 +0200 Subject: [PATCH 66/96] Updated meson build --- meson.build | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index 0c3c2550..e925d598 100644 --- a/meson.build +++ b/meson.build @@ -41,11 +41,14 @@ spdlog_srcs = files([ 'src/async.cpp', 'src/color_sinks.cpp', 'src/file_sinks.cpp', - 'src/fmt.cpp', 'src/spdlog.cpp', 'src/stdout_sinks.cpp' ]) +if not get_option('external_fmt') + spdlog_srcs+= 'src/fmt.cpp' +endif + if get_option('library_type') == 'static' spdlog = static_library( 'spdlog', @@ -76,7 +79,6 @@ spdlog_dep = declare_dependency( # ---------------------------------- # --- Header only dependency --- # ---------------------------------- - spdlog_headeronly_dep = declare_dependency( include_directories : spdlog_inc, compile_args : compile_args, From a6f7edf94b1bc3fca4c0cea0b99a75a52a65bf6e Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 5 Nov 2019 00:16:45 +0200 Subject: [PATCH 67/96] Updated meson build options --- meson.build | 101 ++++++++++++++++++++++++++++++++-------------- meson_options.txt | 10 ++++- 2 files changed, 79 insertions(+), 32 deletions(-) diff --git a/meson.build b/meson.build index e925d598..336eaa36 100644 --- a/meson.build +++ b/meson.build @@ -1,12 +1,12 @@ project('spdlog', ['cpp'], - license : 'MIT', - version : run_command(find_program('scripts/extract_version.py')).stdout().strip(), - default_options : [ + license : 'MIT', + version : run_command(find_program('scripts/extract_version.py')).stdout().strip(), + default_options : [ 'warning_level=3', 'cpp_std=c++11', 'buildtype=release', 'b_colorout=always', - ], + ], ) # ------------------------ @@ -20,17 +20,57 @@ dep_list += dependency('threads') # Check for FMT if get_option('external_fmt') - if not meson.version().version_compare('>=0.49.0') + if not meson.version().version_compare('>=0.49.0') warning('Finding fmt can fail with meson versions before 0.49.0') - endif - dep_list += dependency('fmt') - compile_args += '-DSPDLOG_FMT_EXTERNAL' + endif + dep_list += dependency('fmt') + compile_args += '-DSPDLOG_FMT_EXTERNAL' endif if get_option('no_exceptions') compile_args += '-DSPDLOG_NO_EXCEPTIONS' endif +if get_option('wchar_support') + if build_machine.system() != 'windows' + error('wchar_support only supported under windows') + endif + compile_args += '-DSPDLOG_WCHAR_TO_UTF8_SUPPORT' +endif + +if get_option('wchar_filenames') + if build_machine.system() != 'windows' + error('wchar_support only supported under windows') + endif + compile_args += '-DSPDLOG_WCHAR_FILENAMES' +endif + +if get_option('clock_coarse') + if build_machine.system() != 'linux' + error('clock_coarse only supported under linux') + endif + compile_args += '-DSPDLOG_CLOCK_COARSE' +endif + +if get_option('prevent_child_fd') + compile_args += '-DSPDLOG_PREVENT_CHILD_FD' +endif + +if get_option('no_thread_id') + compile_args += '-DSPDLOG_NO_THREAD_ID' +endif + +if get_option('no_tls') + compile_args += '-DSPDLOG_NO_TLS' +endif + +if get_option('no_atomic_levels') + compile_args += '-DSPDLOG_NO_ATOMIC_LEVELS' +endif + +compile_args_compiled = compile_args + ['-DSPDLOG_COMPILED_LIB'] +compile_args_ho = compile_args + # ------------------------------------ # --- Compiled library version --- # ------------------------------------ @@ -50,40 +90,40 @@ if not get_option('external_fmt') endif if get_option('library_type') == 'static' - spdlog = static_library( + spdlog = static_library( 'spdlog', spdlog_srcs, - cpp_args : [compile_args] + ['-DSPDLOG_COMPILED_LIB'], + cpp_args : compile_args_compiled, include_directories : spdlog_inc, dependencies : dep_list, install : not meson.is_subproject() ) else - spdlog = shared_library('spdlog', + spdlog = shared_library('spdlog', spdlog_srcs, - cpp_args : [compile_args] + ['-DSPDLOG_COMPILED_LIB'], + cpp_args : compile_args_compiled, include_directories : spdlog_inc, dependencies : dep_list, install : not meson.is_subproject(), - ) + ) endif spdlog_dep = declare_dependency( - link_with : spdlog, - include_directories : spdlog_inc, - compile_args : compile_args + ['-DSPDLOG_COMPILED_LIB'], - dependencies : dep_list, - version : meson.project_version(), + link_with : spdlog, + include_directories : spdlog_inc, + compile_args : compile_args_compiled, + dependencies : dep_list, + version : meson.project_version(), ) # ---------------------------------- # --- Header only dependency --- # ---------------------------------- spdlog_headeronly_dep = declare_dependency( - include_directories : spdlog_inc, - compile_args : compile_args, - dependencies : dep_list, - version : meson.project_version(), + include_directories : spdlog_inc, + compile_args : compile_args_ho, + dependencies : dep_list, + version : meson.project_version(), ) # ------------------------ @@ -92,15 +132,15 @@ spdlog_headeronly_dep = declare_dependency( # Do not install when spdlog is used as a subproject if not meson.is_subproject() - install_subdir('include/spdlog', install_dir: get_option('includedir')) + install_subdir('include/spdlog', install_dir: get_option('includedir')) - pkg = import('pkgconfig') - pkg.generate(spdlog, + pkg = import('pkgconfig') + pkg.generate(spdlog, name : 'spdlog', description : 'Fast C++ logging library', url : 'https://github.com/gabime/spdlog', - extra_cflags : ['-DSPDLOG_COMPILED_LIB'] - ) + extra_cflags : compile_args_compiled + ) endif # ------------------------------------- @@ -108,15 +148,15 @@ endif # ------------------------------------- if get_option('enable_tests') - subdir('tests') + subdir('tests') endif if get_option('enable_examples') - subdir('example') + subdir('example') endif if get_option('enable_benchmarks') - subdir('bench') + subdir('bench') endif # ------------------- @@ -124,7 +164,6 @@ endif # ------------------- summary_str = '''spdlog build summary: - - using external fmt: @0@ - building tests: @1@ - building examples: @2@ diff --git a/meson_options.txt b/meson_options.txt index 57374b0e..01060ab1 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -3,4 +3,12 @@ option('enable_examples', type: 'boolean', value: true, description: 'Build ex option('enable_benchmarks', type: 'boolean', value: false, description: 'Build benchmarks') option('enable_tests', type: 'boolean', value: true, description: 'Build tests') option('library_type', type: 'combo', choices: ['static', 'shared'], value: 'static', description: 'Library build type') -option('no_exceptions', type: 'boolean', value: false, description: 'Disabled exceptions - abort() instead any error') +option('no_exceptions', type: 'boolean', value: false, description: 'Disabled exceptions - abort() instead any error') + +option('wchar_support', type: 'boolean', value: false, description:'(Windows only) Support wchar api') +option('wchar_filenames', type: 'boolean', value: false, description: '(Windows only) Support wchar filenames') +option('clock_coarse', type: 'boolean', value: false, description: '(Linux only) Use the much faster (but much less accurate) CLOCK_REALTIME_COARSE instead of the regular clock') +option('prevent_child_fd', type: 'boolean', value: false, description: 'Prevent from child processes to inherit log file descriptors') +option('no_thread_id', type: 'boolean', value: false, description: 'prevent spdlog from querying the thread id on each log call if thread id is not needed') +option('no_tls', type: 'boolean', value: false, description: 'prevent spdlog from using thread local storage') +option('no_atomic_levels', type: 'boolean', value: false, description: 'prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently') From 58320e26787d91301cce275bf2fd43ada8c79af0 Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 5 Nov 2019 00:22:30 +0200 Subject: [PATCH 68/96] Updated meson build options --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 336eaa36..47f84d57 100644 --- a/meson.build +++ b/meson.build @@ -40,7 +40,7 @@ endif if get_option('wchar_filenames') if build_machine.system() != 'windows' - error('wchar_support only supported under windows') + error('wchar_filenames only supported under windows') endif compile_args += '-DSPDLOG_WCHAR_FILENAMES' endif From 436ce16e79b302a759a5687377aafb668f6d4101 Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 5 Nov 2019 00:31:36 +0200 Subject: [PATCH 69/96] Updated meson tests build --- meson.build | 2 +- meson_options.txt | 25 +++++++++++++------------ tests/meson.build | 12 ++++++++---- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/meson.build b/meson.build index 47f84d57..e3d4f94f 100644 --- a/meson.build +++ b/meson.build @@ -147,7 +147,7 @@ endif # --- Conditionally add subdirs --- # ------------------------------------- -if get_option('enable_tests') +if get_option('enable_tests') or get_option('enable_tests-ho') subdir('tests') endif diff --git a/meson_options.txt b/meson_options.txt index 01060ab1..711c7dd6 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,14 +1,15 @@ -option('external_fmt', type: 'boolean', value: false, description: 'Use external fmt package instead of the bundled') -option('enable_examples', type: 'boolean', value: true, description: 'Build examples') -option('enable_benchmarks', type: 'boolean', value: false, description: 'Build benchmarks') -option('enable_tests', type: 'boolean', value: true, description: 'Build tests') +option('external_fmt', type: 'boolean', value: false, description: 'Use external fmt package instead of the bundled') +option('enable_examples', type: 'boolean', value: true, description: 'Build examples') +option('enable_benchmarks', type: 'boolean', value: false, description: 'Build benchmarks') +option('enable_tests', type: 'boolean', value: true, description: 'Build tests') +option('enable_tests_ho', type: 'boolean', value: false, description: 'Build header-only tests') option('library_type', type: 'combo', choices: ['static', 'shared'], value: 'static', description: 'Library build type') -option('no_exceptions', type: 'boolean', value: false, description: 'Disabled exceptions - abort() instead any error') +option('no_exceptions', type: 'boolean', value: false, description: 'Disabled exceptions - abort() instead any error') -option('wchar_support', type: 'boolean', value: false, description:'(Windows only) Support wchar api') -option('wchar_filenames', type: 'boolean', value: false, description: '(Windows only) Support wchar filenames') -option('clock_coarse', type: 'boolean', value: false, description: '(Linux only) Use the much faster (but much less accurate) CLOCK_REALTIME_COARSE instead of the regular clock') -option('prevent_child_fd', type: 'boolean', value: false, description: 'Prevent from child processes to inherit log file descriptors') -option('no_thread_id', type: 'boolean', value: false, description: 'prevent spdlog from querying the thread id on each log call if thread id is not needed') -option('no_tls', type: 'boolean', value: false, description: 'prevent spdlog from using thread local storage') -option('no_atomic_levels', type: 'boolean', value: false, description: 'prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently') +option('wchar_support', type: 'boolean', value: false, description:'(Windows only) Support wchar api') +option('wchar_filenames', type: 'boolean', value: false, description: '(Windows only) Support wchar filenames') +option('clock_coarse', type: 'boolean', value: false, description: '(Linux only) Use the much faster (but much less accurate) CLOCK_REALTIME_COARSE instead of the regular clock') +option('prevent_child_fd', type: 'boolean', value: false, description: 'Prevent from child processes to inherit log file descriptors') +option('no_thread_id', type: 'boolean', value: false, description: 'prevent spdlog from querying the thread id on each log call if thread id is not needed') +option('no_tls', type: 'boolean', value: false, description: 'prevent spdlog from using thread local storage') +option('no_atomic_levels', type: 'boolean', value: false, description: 'prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently') diff --git a/tests/meson.build b/tests/meson.build index 68672221..24feb0a6 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -37,8 +37,12 @@ endif # -------------------------------------- # --- Build the test executables --- # -------------------------------------- -test_exe = executable('spdlog-utests', test_sources, dependencies: global_test_deps + [spdlog_dep]) -test('test_spdlog', test_exe, is_parallel : false) +if get_option('enable_tests') + test_exe = executable('spdlog-utests', test_sources, dependencies: global_test_deps + [spdlog_dep]) + test('test_spdlog', test_exe, is_parallel : false) +endif -test_exe = executable('spdlog-utests-ho', test_sources, dependencies: global_test_deps + [spdlog_headeronly_dep]) -test('test_spdlog-ho', test_exe, is_parallel : false) \ No newline at end of file +if get_option('enable_tests_ho') + test_exe = executable('spdlog-utests-ho', test_sources, dependencies: global_test_deps + [spdlog_headeronly_dep]) + test('test_spdlog-ho', test_exe, is_parallel : false) +endif \ No newline at end of file From 7cdd65075cca64ca55d0027d0626336a6a9a3569 Mon Sep 17 00:00:00 2001 From: Cristian Morales Vega Date: Wed, 6 Nov 2019 19:09:30 +0000 Subject: [PATCH 70/96] "#include" instead of "spdlog/.*" The meaning of using quotes to #include is implementation defined, so it may or not may be what we want. At least POSIX (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/c99.html) says: "headers whose names are enclosed in double-quotes ( "" ) shall be searched for first in the directory of the file with the #include line", so not what we want since "spdlog" ends up twice in the path. --- include/spdlog/async.h | 6 +++--- include/spdlog/async_logger-inl.h | 6 +++--- include/spdlog/async_logger.h | 2 +- include/spdlog/common-inl.h | 2 +- include/spdlog/common.h | 6 +++--- include/spdlog/details/backtracer-inl.h | 2 +- include/spdlog/details/backtracer.h | 4 ++-- include/spdlog/details/console_globals.h | 2 +- include/spdlog/details/file_helper-inl.h | 6 +++--- include/spdlog/details/file_helper.h | 2 +- include/spdlog/details/fmt_helper.h | 4 ++-- include/spdlog/details/log_msg-inl.h | 4 ++-- include/spdlog/details/log_msg.h | 2 +- include/spdlog/details/log_msg_buffer-inl.h | 2 +- include/spdlog/details/log_msg_buffer.h | 2 +- include/spdlog/details/mpmc_blocking_q.h | 2 +- include/spdlog/details/os-inl.h | 4 ++-- include/spdlog/details/os.h | 2 +- include/spdlog/details/pattern_formatter-inl.h | 12 ++++++------ include/spdlog/details/pattern_formatter.h | 8 ++++---- include/spdlog/details/periodic_worker-inl.h | 2 +- include/spdlog/details/registry-inl.h | 14 +++++++------- include/spdlog/details/registry.h | 2 +- include/spdlog/details/thread_pool-inl.h | 4 ++-- include/spdlog/details/thread_pool.h | 8 ++++---- include/spdlog/fmt/fmt.h | 4 ++-- include/spdlog/formatter.h | 4 ++-- include/spdlog/logger-inl.h | 8 ++++---- include/spdlog/logger.h | 8 ++++---- include/spdlog/sinks/android_sink.h | 10 +++++----- include/spdlog/sinks/ansicolor_sink-inl.h | 6 +++--- include/spdlog/sinks/ansicolor_sink.h | 6 +++--- include/spdlog/sinks/base_sink-inl.h | 6 +++--- include/spdlog/sinks/base_sink.h | 6 +++--- include/spdlog/sinks/basic_file_sink-inl.h | 6 +++--- include/spdlog/sinks/basic_file_sink.h | 8 ++++---- include/spdlog/sinks/daily_file_sink.h | 14 +++++++------- include/spdlog/sinks/dist_sink.h | 6 +++--- include/spdlog/sinks/dup_filter_sink.h | 6 +++--- include/spdlog/sinks/msvc_sink.h | 4 ++-- include/spdlog/sinks/null_sink.h | 6 +++--- include/spdlog/sinks/ostream_sink.h | 4 ++-- include/spdlog/sinks/rotating_file_sink-inl.h | 10 +++++----- include/spdlog/sinks/rotating_file_sink.h | 8 ++++---- include/spdlog/sinks/sink-inl.h | 4 ++-- include/spdlog/sinks/sink.h | 4 ++-- include/spdlog/sinks/stdout_color_sinks-inl.h | 6 +++--- include/spdlog/sinks/stdout_color_sinks.h | 6 +++--- include/spdlog/sinks/stdout_sinks-inl.h | 6 +++--- include/spdlog/sinks/stdout_sinks.h | 6 +++--- include/spdlog/sinks/syslog_sink.h | 4 ++-- include/spdlog/sinks/systemd_sink.h | 6 +++--- include/spdlog/sinks/wincolor_sink-inl.h | 6 +++--- include/spdlog/sinks/wincolor_sink.h | 8 ++++---- include/spdlog/spdlog-inl.h | 6 +++--- include/spdlog/spdlog.h | 10 +++++----- 56 files changed, 156 insertions(+), 156 deletions(-) diff --git a/include/spdlog/async.h b/include/spdlog/async.h index dc99492d..afaf263f 100644 --- a/include/spdlog/async.h +++ b/include/spdlog/async.h @@ -14,9 +14,9 @@ // This is because each message in the queue holds a shared_ptr to the // originating logger. -#include "spdlog/async_logger.h" -#include "spdlog/details/registry.h" -#include "spdlog/details/thread_pool.h" +#include +#include +#include #include #include diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index cfeda2de..356db0e7 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -4,11 +4,11 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/async_logger.h" +#include #endif -#include "spdlog/sinks/sink.h" -#include "spdlog/details/thread_pool.h" +#include +#include #include #include diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index 0a685186..829c5acc 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -14,7 +14,7 @@ // Upon destruction, logs all remaining messages in the queue before // destructing.. -#include "spdlog/logger.h" +#include namespace spdlog { diff --git a/include/spdlog/common-inl.h b/include/spdlog/common-inl.h index 7ba12511..a21284d2 100644 --- a/include/spdlog/common-inl.h +++ b/include/spdlog/common-inl.h @@ -4,7 +4,7 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/common.h" +#include #endif namespace spdlog { diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 13fbe970..6edd6fa9 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -3,8 +3,8 @@ #pragma once -#include "spdlog/tweakme.h" -#include "spdlog/details/null_mutex.h" +#include +#include #include #include @@ -35,7 +35,7 @@ #define SPDLOG_INLINE inline #endif -#include "spdlog/fmt/fmt.h" +#include // visual studio upto 2013 does not support noexcept nor constexpr #if defined(_MSC_VER) && (_MSC_VER < 1900) diff --git a/include/spdlog/details/backtracer-inl.h b/include/spdlog/details/backtracer-inl.h index 8aa34b97..d840ad08 100644 --- a/include/spdlog/details/backtracer-inl.h +++ b/include/spdlog/details/backtracer-inl.h @@ -4,7 +4,7 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/details/backtracer.h" +#include #endif namespace spdlog { namespace details { diff --git a/include/spdlog/details/backtracer.h b/include/spdlog/details/backtracer.h index 713d00e9..0e779cab 100644 --- a/include/spdlog/details/backtracer.h +++ b/include/spdlog/details/backtracer.h @@ -3,8 +3,8 @@ #pragma once -#include "spdlog/details/log_msg_buffer.h" -#include "spdlog/details/circular_q.h" +#include +#include #include #include diff --git a/include/spdlog/details/console_globals.h b/include/spdlog/details/console_globals.h index 6761a30d..665201dd 100644 --- a/include/spdlog/details/console_globals.h +++ b/include/spdlog/details/console_globals.h @@ -3,7 +3,7 @@ #pragma once -#include "spdlog/details/null_mutex.h" +#include #include namespace spdlog { diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index 3c1d8056..cdd45f17 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -4,11 +4,11 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/details/file_helper.h" +#include #endif -#include "spdlog/details/os.h" -#include "spdlog/common.h" +#include +#include #include #include diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 5ac4c27b..3228ce84 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -3,7 +3,7 @@ #pragma once -#include "spdlog/common.h" +#include #include namespace spdlog { diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 5298e0a7..85b988e6 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -4,8 +4,8 @@ #include #include -#include "spdlog/fmt/fmt.h" -#include "spdlog/common.h" +#include +#include // Some fmt helpers to efficiently format and pad ints and strings namespace spdlog { diff --git a/include/spdlog/details/log_msg-inl.h b/include/spdlog/details/log_msg-inl.h index a47fe660..1e4eaaff 100644 --- a/include/spdlog/details/log_msg-inl.h +++ b/include/spdlog/details/log_msg-inl.h @@ -4,10 +4,10 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/details/log_msg.h" +#include #endif -#include "spdlog/details/os.h" +#include namespace spdlog { namespace details { diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index ccd41ecb..9ae473d4 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -3,7 +3,7 @@ #pragma once -#include "spdlog/common.h" +#include #include namespace spdlog { diff --git a/include/spdlog/details/log_msg_buffer-inl.h b/include/spdlog/details/log_msg_buffer-inl.h index c8b9e5ef..e443015a 100644 --- a/include/spdlog/details/log_msg_buffer-inl.h +++ b/include/spdlog/details/log_msg_buffer-inl.h @@ -4,7 +4,7 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/details/log_msg_buffer.h" +#include #endif namespace spdlog { diff --git a/include/spdlog/details/log_msg_buffer.h b/include/spdlog/details/log_msg_buffer.h index 5ddcbc10..f0dccf62 100644 --- a/include/spdlog/details/log_msg_buffer.h +++ b/include/spdlog/details/log_msg_buffer.h @@ -3,7 +3,7 @@ #pragma once -#include "spdlog/details/log_msg.h" +#include namespace spdlog { namespace details { diff --git a/include/spdlog/details/mpmc_blocking_q.h b/include/spdlog/details/mpmc_blocking_q.h index 397cd56c..7f8a2535 100644 --- a/include/spdlog/details/mpmc_blocking_q.h +++ b/include/spdlog/details/mpmc_blocking_q.h @@ -10,7 +10,7 @@ // dequeue_for(..) - will block until the queue is not empty or timeout have // passed. -#include "spdlog/details/circular_q.h" +#include #include #include diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index af796740..3ec87da9 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -4,10 +4,10 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/details/os.h" +#include #endif -#include "spdlog/common.h" +#include #include #include diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 0b1e6006..0894a6c0 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -3,7 +3,7 @@ #pragma once -#include "spdlog/common.h" +#include #include // std::time_t namespace spdlog { diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index f97e2c2f..6e79df5d 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -4,14 +4,14 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/details/pattern_formatter.h" +#include #endif -#include "spdlog/details/fmt_helper.h" -#include "spdlog/details/log_msg.h" -#include "spdlog/details/os.h" -#include "spdlog/fmt/fmt.h" -#include "spdlog/formatter.h" +#include +#include +#include +#include +#include #include #include diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index 7191ea9b..b03d3394 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -3,10 +3,10 @@ #pragma once -#include "spdlog/common.h" -#include "spdlog/details/log_msg.h" -#include "spdlog/details/os.h" -#include "spdlog/formatter.h" +#include +#include +#include +#include #include #include diff --git a/include/spdlog/details/periodic_worker-inl.h b/include/spdlog/details/periodic_worker-inl.h index 1af6288a..1d794994 100644 --- a/include/spdlog/details/periodic_worker-inl.h +++ b/include/spdlog/details/periodic_worker-inl.h @@ -4,7 +4,7 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/details/periodic_worker.h" +#include #endif namespace spdlog { diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index 784ec237..562aa06c 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -4,20 +4,20 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/details/registry.h" +#include #endif -#include "spdlog/common.h" -#include "spdlog/details/periodic_worker.h" -#include "spdlog/logger.h" -#include "spdlog/details/pattern_formatter.h" +#include +#include +#include +#include #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER // support for the default stdout color logger #ifdef _WIN32 -#include "spdlog/sinks/wincolor_sink.h" +#include #else -#include "spdlog/sinks/ansicolor_sink.h" +#include #endif #endif // SPDLOG_DISABLE_DEFAULT_LOGGER diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index abb95c32..6ac571dc 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -8,7 +8,7 @@ // If user requests a non existing logger, nullptr will be returned // This class is thread safe -#include "spdlog/common.h" +#include #include #include diff --git a/include/spdlog/details/thread_pool-inl.h b/include/spdlog/details/thread_pool-inl.h index f9cf0e78..910eb2a0 100644 --- a/include/spdlog/details/thread_pool-inl.h +++ b/include/spdlog/details/thread_pool-inl.h @@ -4,10 +4,10 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/details/thread_pool.h" +#include #endif -#include "spdlog/common.h" +#include namespace spdlog { namespace details { diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index 81bd12de..12078044 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -3,9 +3,9 @@ #pragma once -#include "spdlog/details/log_msg_buffer.h" -#include "spdlog/details/mpmc_blocking_q.h" -#include "spdlog/details/os.h" +#include +#include +#include #include #include @@ -27,7 +27,7 @@ enum class async_msg_type terminate }; -#include "spdlog/details/log_msg_buffer.h" +#include // Async msg to move to/from the queue // Movable only. should never be copied struct async_msg : log_msg_buffer diff --git a/include/spdlog/fmt/fmt.h b/include/spdlog/fmt/fmt.h index 5d039b8c..ea2efb17 100644 --- a/include/spdlog/fmt/fmt.h +++ b/include/spdlog/fmt/fmt.h @@ -22,6 +22,6 @@ #include "bundled/core.h" #include "bundled/format.h" #else // SPDLOG_FMT_EXTERNAL is defined - use external fmtlib -#include "fmt/core.h" -#include "fmt/format.h" +#include +#include #endif diff --git a/include/spdlog/formatter.h b/include/spdlog/formatter.h index 9a4ed035..5086fb21 100644 --- a/include/spdlog/formatter.h +++ b/include/spdlog/formatter.h @@ -3,8 +3,8 @@ #pragma once -#include "fmt/fmt.h" -#include "spdlog/details/log_msg.h" +#include +#include namespace spdlog { diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index bc8232d8..062304ba 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -4,12 +4,12 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/logger.h" +#include #endif -#include "spdlog/sinks/sink.h" -#include "spdlog/details/backtracer.h" -#include "spdlog/details/pattern_formatter.h" +#include +#include +#include #include diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 0e182875..43f39600 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -14,12 +14,12 @@ // The use of private formatter per sink provides the opportunity to cache some // formatted data, and support for different format per sink. -#include "spdlog/common.h" -#include "spdlog/details/log_msg.h" -#include "spdlog/details/backtracer.h" +#include +#include +#include #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT -#include "spdlog/details/os.h" +#include #endif #include diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h index d1e87f2d..bdbe542b 100644 --- a/include/spdlog/sinks/android_sink.h +++ b/include/spdlog/sinks/android_sink.h @@ -5,11 +5,11 @@ #ifdef __ANDROID__ -#include "spdlog/details/fmt_helper.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/details/os.h" -#include "spdlog/sinks/base_sink.h" -#include "spdlog/details/synchronous_factory.h" +#include +#include +#include +#include +#include #include #include diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 280ad7cd..8480b06d 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -4,11 +4,11 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/sinks/ansicolor_sink.h" +#include #endif -#include "spdlog/details/pattern_formatter.h" -#include "spdlog/details/os.h" +#include +#include namespace spdlog { namespace sinks { diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 6ef10e78..16433019 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -3,9 +3,9 @@ #pragma once -#include "spdlog/details/console_globals.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/sinks/sink.h" +#include +#include +#include #include #include #include diff --git a/include/spdlog/sinks/base_sink-inl.h b/include/spdlog/sinks/base_sink-inl.h index 565cdc17..2883c058 100644 --- a/include/spdlog/sinks/base_sink-inl.h +++ b/include/spdlog/sinks/base_sink-inl.h @@ -4,11 +4,11 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/sinks/base_sink.h" +#include #endif -#include "spdlog/common.h" -#include "spdlog/details/pattern_formatter.h" +#include +#include #include diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h index be3956a7..bc832763 100644 --- a/include/spdlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -9,9 +9,9 @@ // implementers.. // -#include "spdlog/common.h" -#include "spdlog/details/log_msg.h" -#include "spdlog/sinks/sink.h" +#include +#include +#include namespace spdlog { namespace sinks { diff --git a/include/spdlog/sinks/basic_file_sink-inl.h b/include/spdlog/sinks/basic_file_sink-inl.h index 0e5275a9..1260d15c 100644 --- a/include/spdlog/sinks/basic_file_sink-inl.h +++ b/include/spdlog/sinks/basic_file_sink-inl.h @@ -4,11 +4,11 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/sinks/basic_file_sink.h" +#include #endif -#include "spdlog/common.h" -#include "spdlog/details/os.h" +#include +#include namespace spdlog { namespace sinks { diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h index 899b89fa..0ab9a4a1 100644 --- a/include/spdlog/sinks/basic_file_sink.h +++ b/include/spdlog/sinks/basic_file_sink.h @@ -3,10 +3,10 @@ #pragma once -#include "spdlog/details/file_helper.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/sinks/base_sink.h" -#include "spdlog/details/synchronous_factory.h" +#include +#include +#include +#include #include #include diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index d200480f..d3eb95a3 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -3,13 +3,13 @@ #pragma once -#include "spdlog/common.h" -#include "spdlog/details/file_helper.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/fmt/fmt.h" -#include "spdlog/sinks/base_sink.h" -#include "spdlog/details/os.h" -#include "spdlog/details/synchronous_factory.h" +#include +#include +#include +#include +#include +#include +#include #include #include diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h index 8f931cf7..a6a33370 100644 --- a/include/spdlog/sinks/dist_sink.h +++ b/include/spdlog/sinks/dist_sink.h @@ -4,9 +4,9 @@ #pragma once #include "base_sink.h" -#include "spdlog/details/log_msg.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/details/pattern_formatter.h" +#include +#include +#include #include #include diff --git a/include/spdlog/sinks/dup_filter_sink.h b/include/spdlog/sinks/dup_filter_sink.h index ab501387..8ee63e40 100644 --- a/include/spdlog/sinks/dup_filter_sink.h +++ b/include/spdlog/sinks/dup_filter_sink.h @@ -4,8 +4,8 @@ #pragma once #include "dist_sink.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/details/log_msg.h" +#include +#include #include #include @@ -16,7 +16,7 @@ // // Example: // -// #include "spdlog/sinks/dup_filter_sink.h" +// #include // // int main() { // auto dup_filter = std::make_shared(std::chrono::seconds(5)); diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index 39b2ce91..6db10bc9 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -5,8 +5,8 @@ #if defined(_WIN32) -#include "spdlog/details/null_mutex.h" -#include "spdlog/sinks/base_sink.h" +#include +#include #include diff --git a/include/spdlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h index 44a45a3e..eb832801 100644 --- a/include/spdlog/sinks/null_sink.h +++ b/include/spdlog/sinks/null_sink.h @@ -3,9 +3,9 @@ #pragma once -#include "spdlog/details/null_mutex.h" -#include "spdlog/sinks/base_sink.h" -#include "spdlog/details/synchronous_factory.h" +#include +#include +#include #include diff --git a/include/spdlog/sinks/ostream_sink.h b/include/spdlog/sinks/ostream_sink.h index 8d47d770..95c1e962 100644 --- a/include/spdlog/sinks/ostream_sink.h +++ b/include/spdlog/sinks/ostream_sink.h @@ -3,8 +3,8 @@ #pragma once -#include "spdlog/details/null_mutex.h" -#include "spdlog/sinks/base_sink.h" +#include +#include #include #include diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index 82347001..bd2d175d 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -4,14 +4,14 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/sinks/rotating_file_sink.h" +#include #endif -#include "spdlog/common.h" +#include -#include "spdlog/details/file_helper.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/fmt/fmt.h" +#include +#include +#include #include #include diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index f8c49e75..5be8583a 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -3,10 +3,10 @@ #pragma once -#include "spdlog/sinks/base_sink.h" -#include "spdlog/details/file_helper.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/details/synchronous_factory.h" +#include +#include +#include +#include #include #include diff --git a/include/spdlog/sinks/sink-inl.h b/include/spdlog/sinks/sink-inl.h index d1da94a0..a8dd6a6c 100644 --- a/include/spdlog/sinks/sink-inl.h +++ b/include/spdlog/sinks/sink-inl.h @@ -4,10 +4,10 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/sinks/sink.h" +#include #endif -#include "spdlog/common.h" +#include SPDLOG_INLINE bool spdlog::sinks::sink::should_log(spdlog::level::level_enum msg_level) const { diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index 6515f1f1..b2ca4db1 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -3,8 +3,8 @@ #pragma once -#include "spdlog/details/log_msg.h" -#include "spdlog/formatter.h" +#include +#include namespace spdlog { diff --git a/include/spdlog/sinks/stdout_color_sinks-inl.h b/include/spdlog/sinks/stdout_color_sinks-inl.h index 0effb4db..653aca80 100644 --- a/include/spdlog/sinks/stdout_color_sinks-inl.h +++ b/include/spdlog/sinks/stdout_color_sinks-inl.h @@ -4,11 +4,11 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/sinks/stdout_color_sinks.h" +#include #endif -#include "spdlog/logger.h" -#include "spdlog/common.h" +#include +#include namespace spdlog { diff --git a/include/spdlog/sinks/stdout_color_sinks.h b/include/spdlog/sinks/stdout_color_sinks.h index d11ec0a1..e67aa91b 100644 --- a/include/spdlog/sinks/stdout_color_sinks.h +++ b/include/spdlog/sinks/stdout_color_sinks.h @@ -4,12 +4,12 @@ #pragma once #ifdef _WIN32 -#include "spdlog/sinks/wincolor_sink.h" +#include #else -#include "spdlog/sinks/ansicolor_sink.h" +#include #endif -#include "spdlog/details/synchronous_factory.h" +#include namespace spdlog { namespace sinks { diff --git a/include/spdlog/sinks/stdout_sinks-inl.h b/include/spdlog/sinks/stdout_sinks-inl.h index e7894987..adbcb632 100644 --- a/include/spdlog/sinks/stdout_sinks-inl.h +++ b/include/spdlog/sinks/stdout_sinks-inl.h @@ -4,11 +4,11 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/sinks/stdout_sinks.h" +#include #endif -#include "spdlog/details/console_globals.h" -#include "spdlog/details/pattern_formatter.h" +#include +#include #include namespace spdlog { diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index 04d2fa42..1cc47bd0 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -3,9 +3,9 @@ #pragma once -#include "spdlog/details/console_globals.h" -#include "spdlog/details/synchronous_factory.h" -#include "spdlog/sinks/sink.h" +#include +#include +#include #include namespace spdlog { diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 697ba241..2f4e3fde 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -3,8 +3,8 @@ #pragma once -#include "spdlog/sinks/base_sink.h" -#include "spdlog/details/null_mutex.h" +#include +#include #include #include diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index cee889a4..d90edb22 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -3,9 +3,9 @@ #pragma once -#include "spdlog/sinks/base_sink.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/details/synchronous_factory.h" +#include +#include +#include #include #ifndef SD_JOURNAL_SUPPRESS_LOCATION diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index aee0bb95..6ce9cac3 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -4,11 +4,11 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/sinks/wincolor_sink.h" +#include #endif -#include "spdlog/common.h" -#include "spdlog/details/pattern_formatter.h" +#include +#include namespace spdlog { namespace sinks { diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 22099e28..743db5c6 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -3,10 +3,10 @@ #pragma once -#include "spdlog/common.h" -#include "spdlog/details/console_globals.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/sinks/sink.h" +#include +#include +#include +#include #include #include diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h index 34e8d9d0..43aa49ef 100644 --- a/include/spdlog/spdlog-inl.h +++ b/include/spdlog/spdlog-inl.h @@ -4,11 +4,11 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include "spdlog/spdlog.h" +#include #endif -#include "spdlog/common.h" -#include "spdlog/details/pattern_formatter.h" +#include +#include namespace spdlog { diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 9e0af330..925f5021 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -9,11 +9,11 @@ #pragma once -#include "spdlog/common.h" -#include "spdlog/details/registry.h" -#include "spdlog/logger.h" -#include "spdlog/version.h" -#include "spdlog/details/synchronous_factory.h" +#include +#include +#include +#include +#include #include #include From ff3e6c7248a0bfb1d44e72fd0f822320178263bc Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 14:08:31 +0200 Subject: [PATCH 71/96] Fix issue #1306 --- include/spdlog/details/os-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 3ec87da9..15e7f2df 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -277,7 +277,7 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) return offset; #else -#if defined(sun) || defined(__sun) || defined(_AIX) +#if defined(sun) || defined(__sun) || defined(_AIX) || (!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE)) // 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris struct helper { From 844d54d7e634b13002de6249707c81fbe0d263ad Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 14:27:05 +0200 Subject: [PATCH 72/96] Fix #1302 --- include/spdlog/details/os-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 15e7f2df..c8d8ab37 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -428,7 +428,7 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT #if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) { - if (wstr.size() > static_cast(std::numeric_limits::max())) + if (wstr.size() > static_cast((std::numeric_limits::max()))) { SPDLOG_THROW(spdlog::spdlog_ex("UTF-16 string is too big to be converted to UTF-8")); } From de2c07ac62d44dfad11583cefc14f263af63eec3 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 14:37:52 +0200 Subject: [PATCH 73/96] always cache gmt offset --- include/spdlog/details/pattern_formatter-inl.h | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index 6e79df5d..8eeea621 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -593,14 +593,7 @@ public: const size_t field_size = 6; ScopedPadder p(field_size, padinfo_, dest); -#ifdef _WIN32 - int total_minutes = get_cached_offset(msg, tm_time); -#else - // No need to cache under gcc, - // it is very fast (already stored in tm.tm_gmtoff) - (void)(msg); - int total_minutes = os::utc_minutes_offset(tm_time); -#endif + auto total_minutes = get_cached_offset(msg, tm_time); bool is_negative = total_minutes < 0; if (is_negative) { @@ -619,7 +612,6 @@ public: private: log_clock::time_point last_update_{std::chrono::seconds(0)}; -#ifdef _WIN32 int offset_minutes_{0}; int get_cached_offset(const log_msg &msg, const std::tm &tm_time) @@ -632,7 +624,6 @@ private: } return offset_minutes_; } -#endif }; // Thread id From 255f7f2deec7d6f56743e22d5bf14db714c75a0f Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 15:09:57 +0200 Subject: [PATCH 74/96] Optimze backtracer operator= --- include/spdlog/details/backtracer-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/backtracer-inl.h b/include/spdlog/details/backtracer-inl.h index d840ad08..21553c26 100644 --- a/include/spdlog/details/backtracer-inl.h +++ b/include/spdlog/details/backtracer-inl.h @@ -26,7 +26,7 @@ SPDLOG_INLINE backtracer &backtracer::operator=(backtracer other) { std::lock_guard lock(mutex_); enabled_ = other.enabled(); - messages_ = other.messages_; + messages_ = std::move(other.messages_); return *this; } From be336e75140a0a9468ce9fb73fa126d243d62b20 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 15:21:18 +0200 Subject: [PATCH 75/96] Added noexcept to log_msg move constructor --- include/spdlog/details/log_msg_buffer-inl.h | 2 +- include/spdlog/details/log_msg_buffer.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/details/log_msg_buffer-inl.h b/include/spdlog/details/log_msg_buffer-inl.h index e443015a..0e1d7b02 100644 --- a/include/spdlog/details/log_msg_buffer-inl.h +++ b/include/spdlog/details/log_msg_buffer-inl.h @@ -26,7 +26,7 @@ SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other) update_string_views(); } -SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) +SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT : log_msg{std::move(other)} , buffer{std::move(other.buffer)} { diff --git a/include/spdlog/details/log_msg_buffer.h b/include/spdlog/details/log_msg_buffer.h index f0dccf62..1a204e22 100644 --- a/include/spdlog/details/log_msg_buffer.h +++ b/include/spdlog/details/log_msg_buffer.h @@ -20,7 +20,7 @@ public: log_msg_buffer() = default; explicit log_msg_buffer(const log_msg &orig_msg); log_msg_buffer(const log_msg_buffer &other); - log_msg_buffer(log_msg_buffer &&other); + log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT; log_msg_buffer &operator=(const log_msg_buffer &other); log_msg_buffer &operator=(log_msg_buffer &&other); }; From 93008b23693cb0a2798f62a8c575919d0da91984 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 15:31:33 +0200 Subject: [PATCH 76/96] Fix clang-tidy warning about non existing move --- include/spdlog/details/log_msg_buffer-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/log_msg_buffer-inl.h b/include/spdlog/details/log_msg_buffer-inl.h index 0e1d7b02..704cd9fa 100644 --- a/include/spdlog/details/log_msg_buffer-inl.h +++ b/include/spdlog/details/log_msg_buffer-inl.h @@ -27,7 +27,7 @@ SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other) } SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT - : log_msg{std::move(other)} + : log_msg{other} , buffer{std::move(other.buffer)} { update_string_views(); From bff85725d216584e5b4263dab52aaa4d30a60e6b Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 16:20:41 +0200 Subject: [PATCH 77/96] Fixed some more clang-tidy warnings --- include/spdlog/details/log_msg_buffer-inl.h | 4 ++-- include/spdlog/details/log_msg_buffer.h | 2 +- include/spdlog/details/pattern_formatter-inl.h | 2 +- include/spdlog/logger-inl.h | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/log_msg_buffer-inl.h b/include/spdlog/details/log_msg_buffer-inl.h index 704cd9fa..0c1d9f35 100644 --- a/include/spdlog/details/log_msg_buffer-inl.h +++ b/include/spdlog/details/log_msg_buffer-inl.h @@ -42,9 +42,9 @@ SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(const log_msg_buffer &ot return *this; } -SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(log_msg_buffer &&other) +SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(log_msg_buffer &&other) SPDLOG_NOEXCEPT { - log_msg::operator=(std::move(other)); + log_msg::operator=(other); buffer = std::move(other.buffer); update_string_views(); return *this; diff --git a/include/spdlog/details/log_msg_buffer.h b/include/spdlog/details/log_msg_buffer.h index 1a204e22..c20ae7b0 100644 --- a/include/spdlog/details/log_msg_buffer.h +++ b/include/spdlog/details/log_msg_buffer.h @@ -22,7 +22,7 @@ public: log_msg_buffer(const log_msg_buffer &other); log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT; log_msg_buffer &operator=(const log_msg_buffer &other); - log_msg_buffer &operator=(log_msg_buffer &&other); + log_msg_buffer &operator=(log_msg_buffer &&other) SPDLOG_NOEXCEPT; }; } // namespace details diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index 8eeea621..8bee8a51 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -876,7 +876,7 @@ public: fmt_helper::pad6(static_cast(delta_units.count()), dest); } -protected: +private: log_clock::time_point last_message_time_; }; diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 062304ba..95a485f1 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -150,7 +150,7 @@ SPDLOG_INLINE std::vector &logger::sinks() // error handler SPDLOG_INLINE void logger::set_error_handler(err_handler handler) { - custom_err_handler_ = handler; + custom_err_handler_ = std::move(handler); } // create new logger with same sinks and configuration. From 3999613ecaba623b57e2e6a6f9e0a85391d16339 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 16:27:08 +0200 Subject: [PATCH 78/96] Added performance chacks tp .clang-tidy --- scripts/.clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/.clang-tidy b/scripts/.clang-tidy index 20766308..ce2a8cd7 100644 --- a/scripts/.clang-tidy +++ b/scripts/.clang-tidy @@ -1,4 +1,4 @@ -Checks: 'modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*,-misc-non-private-member-variables-in-classes, -modernize-use-trailing-return-type' +Checks: 'performance-*,modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*,-misc-non-private-member-variables-in-classes, -modernize-use-trailing-return-type' WarningsAsErrors: '' HeaderFilterRegex: 'async.h|async_logger.h|common.h|details|formatter.h|logger.h|sinks|spdlog.h|tweakme.h|version.h' AnalyzeTemporaryDtors: false From acf32be842d654770fadb4f7a799ac9fd260860f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Fri, 8 Nov 2019 15:29:20 +0100 Subject: [PATCH 79/96] Add ringbuffer sink (requires boost::circular_buffer) Ringbuffer sink keeps user-given number of most recent log messages in memory and returns them upon request (using the ringbuffer_sink::last method). This can be useful for e.g. remote debugging of a running app. --- include/spdlog/sinks/ringbuffer_sink-inl.h | 48 +++++++++++++++++ include/spdlog/sinks/ringbuffer_sink.h | 60 ++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 include/spdlog/sinks/ringbuffer_sink-inl.h create mode 100644 include/spdlog/sinks/ringbuffer_sink.h diff --git a/include/spdlog/sinks/ringbuffer_sink-inl.h b/include/spdlog/sinks/ringbuffer_sink-inl.h new file mode 100644 index 00000000..413fb57b --- /dev/null +++ b/include/spdlog/sinks/ringbuffer_sink-inl.h @@ -0,0 +1,48 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY +#include "spdlog/sinks/ringbuffer_sink.h" +#endif + +#include "spdlog/common.h" +#include "spdlog/details/os.h" + +#include + +namespace spdlog { +namespace sinks { + +template +SPDLOG_INLINE ringbuffer_sink::ringbuffer_sink(size_t buf_size) +{ + buf.set_capacity(buf_size); +} + +template +SPDLOG_INLINE void ringbuffer_sink::sink_it_(const details::log_msg &msg) +{ + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + buf.push_front(fmt::to_string(formatted)); +} + +template +SPDLOG_INLINE std::vector ringbuffer_sink::last(size_t lim) +{ + std::lock_guard lock(base_sink::mutex_); + std::vector ret; + ret.reserve(lim); + size_t num=0; + for(const std::string& msg: buf){ + num++; + ret.push_back(msg); + if(lim>0 && num==lim) break; + } + return ret; +} + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h new file mode 100644 index 00000000..35b5e86b --- /dev/null +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -0,0 +1,60 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include "spdlog/details/null_mutex.h" +#include "spdlog/sinks/base_sink.h" +#include "spdlog/details/synchronous_factory.h" + +#include +#include +#include + +#include + +namespace spdlog { +namespace sinks { +/* + * Ring buffer sink + */ +template +class ringbuffer_sink final : public base_sink +{ +public: + explicit ringbuffer_sink(size_t buf_size); + std::vector last(size_t lim=0); + +protected: + void sink_it_(const details::log_msg &msg) override; + void flush_() override {}; + +private: + boost::circular_buffer buf; +}; + +using ringbuffer_sink_mt = ringbuffer_sink; +using ringbuffer_sink_st = ringbuffer_sink; + +} // namespace sinks + +// +// factory functions +// +template +inline std::shared_ptr basic_logger_mt(const std::string &logger_name, size_t buf_size) +{ + return Factory::template create(logger_name, buf_size); +} + +template +inline std::shared_ptr basic_logger_st(const std::string &logger_name, size_t buf_size) +{ + return Factory::template create(logger_name, buf_size); +} + +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY +#include "ringbuffer_sink-inl.h" +#endif From 840adfbbcfb41cc63ada4b978562a1e441976595 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 16:32:57 +0200 Subject: [PATCH 80/96] Added performance chacks tp .clang-tidy --- scripts/.clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/.clang-tidy b/scripts/.clang-tidy index ce2a8cd7..13021cae 100644 --- a/scripts/.clang-tidy +++ b/scripts/.clang-tidy @@ -1,4 +1,4 @@ -Checks: 'performance-*,modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*,-misc-non-private-member-variables-in-classes, -modernize-use-trailing-return-type' +Checks: 'performance-*,-performance-unnecessary-value-param,modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*,-misc-non-private-member-variables-in-classes, -modernize-use-trailing-return-type' WarningsAsErrors: '' HeaderFilterRegex: 'async.h|async_logger.h|common.h|details|formatter.h|logger.h|sinks|spdlog.h|tweakme.h|version.h' AnalyzeTemporaryDtors: false From be2a7515136b47d15aa21d93582ea6be97e7b533 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 16:35:18 +0200 Subject: [PATCH 81/96] Fixed clang-tidy warning in example --- example/example.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/example.cpp b/example/example.cpp index 2a18677a..0bc838bd 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -143,7 +143,7 @@ void async_example() #include "spdlog/fmt/bin_to_hex.h" void binary_example() { - std::vector buf; + std::vector buf(80); for (int i = 0; i < 80; i++) { buf.push_back(static_cast(i & 0xff)); From 653ec05c0e82eed9c758ee28ecf40128da92a221 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 8 Nov 2019 16:42:16 +0200 Subject: [PATCH 82/96] Updated .clang-tidy with more checks --- scripts/.clang-tidy | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/.clang-tidy b/scripts/.clang-tidy index 13021cae..c3802ca0 100644 --- a/scripts/.clang-tidy +++ b/scripts/.clang-tidy @@ -1,4 +1,17 @@ -Checks: 'performance-*,-performance-unnecessary-value-param,modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*,-misc-non-private-member-variables-in-classes, -modernize-use-trailing-return-type' +Checks: '\ +cppcoreguidelines-*,\ +performance-*,\ +-performance-unnecessary-value-param,\ +modernize-*,\ +-modernize-use-trailing-return-type,\ +google-*,\ +-google-runtime-references,\ +misc-*,\ +-misc-non-private-member-variables-in-classes,\ +cert-*,\ +readability-*,\ +clang-analyzer-*' + WarningsAsErrors: '' HeaderFilterRegex: 'async.h|async_logger.h|common.h|details|formatter.h|logger.h|sinks|spdlog.h|tweakme.h|version.h' AnalyzeTemporaryDtors: false From 6f0cb6365ed6bb1c2fa6cf25b6f3471474b2ae0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Fri, 8 Nov 2019 19:25:31 +0100 Subject: [PATCH 83/96] Move ringbuffer_sink to spdlog::details::circular_q, enhance its API: size(), at(i) --- include/spdlog/details/circular_q.h | 14 ++++++++++++++ include/spdlog/sinks/ringbuffer_sink-inl.h | 10 ++++------ include/spdlog/sinks/ringbuffer_sink.h | 5 ++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index 4a87940d..aac4223f 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -72,6 +72,20 @@ public: return v_[head_]; } + // Return number of elements actually stored + size_t size() const + { + return (tail_ - head_) % max_items_; + } + + // Return const reference to item by index. + // If index is out of range 0…size()-1, the behavior is undefined. + const T &at(size_t i) const + { + assert(i < size()); + return v_[(head_+ i) % max_items_]; + } + // Pop item from front. // If there are no elements in the container, the behavior is undefined. void pop_front() diff --git a/include/spdlog/sinks/ringbuffer_sink-inl.h b/include/spdlog/sinks/ringbuffer_sink-inl.h index 413fb57b..d0f1a45b 100644 --- a/include/spdlog/sinks/ringbuffer_sink-inl.h +++ b/include/spdlog/sinks/ringbuffer_sink-inl.h @@ -10,15 +10,13 @@ #include "spdlog/common.h" #include "spdlog/details/os.h" -#include - namespace spdlog { namespace sinks { template SPDLOG_INLINE ringbuffer_sink::ringbuffer_sink(size_t buf_size) { - buf.set_capacity(buf_size); + buf=details::circular_q(buf_size); } template @@ -26,7 +24,7 @@ SPDLOG_INLINE void ringbuffer_sink::sink_it_(const details::log_msg &msg) { memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); - buf.push_front(fmt::to_string(formatted)); + buf.push_back(fmt::to_string(formatted)); } template @@ -36,9 +34,9 @@ SPDLOG_INLINE std::vector ringbuffer_sink::last(size_t lim) std::vector ret; ret.reserve(lim); size_t num=0; - for(const std::string& msg: buf){ + for(size_t i=0; i0 && num==lim) break; } return ret; diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h index 35b5e86b..c51628f4 100644 --- a/include/spdlog/sinks/ringbuffer_sink.h +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -6,13 +6,12 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/sinks/base_sink.h" #include "spdlog/details/synchronous_factory.h" +#include "spdlog/details/circular_q.h" #include #include #include -#include - namespace spdlog { namespace sinks { /* @@ -30,7 +29,7 @@ protected: void flush_() override {}; private: - boost::circular_buffer buf; + details::circular_q buf; }; using ringbuffer_sink_mt = ringbuffer_sink; From bad728446564bc705ee5b348015a8ef0afcb7336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Sat, 9 Nov 2019 09:30:27 +0100 Subject: [PATCH 84/96] Fix cicular_q::size() --- include/spdlog/details/circular_q.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index aac4223f..d07f0725 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -75,7 +75,14 @@ public: // Return number of elements actually stored size_t size() const { - return (tail_ - head_) % max_items_; + if (tail_ > head_) + { + return (tail_ - head_) % max_items; + } + else + { + return max_items - (head_ - tail_ ) % max_items; + } } // Return const reference to item by index. From 042045b99842d07ba193f36a4226a9232524398c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Sat, 9 Nov 2019 09:35:05 +0100 Subject: [PATCH 85/96] typo fix --- include/spdlog/details/circular_q.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index d07f0725..f06cd8e5 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -77,11 +77,11 @@ public: { if (tail_ > head_) { - return (tail_ - head_) % max_items; + return (tail_ - head_) % max_items_; } else { - return max_items - (head_ - tail_ ) % max_items; + return max_items_ - (head_ - tail_ ) % max_items_; } } From daef0a23747690091eb62aeeebdafd90672febfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Sat, 9 Nov 2019 13:15:57 +0100 Subject: [PATCH 86/96] Fix circular_q::size() --- include/spdlog/details/circular_q.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index f06cd8e5..a37b7770 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -77,11 +77,11 @@ public: { if (tail_ > head_) { - return (tail_ - head_) % max_items_; + return tail_ - head_; } else { - return max_items_ - (head_ - tail_ ) % max_items_; + return max_items_ - (head_ - tail_ ); } } From 62e09e73f7437c2d022611397f39b10dd21d7c2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Sat, 9 Nov 2019 13:48:04 +0100 Subject: [PATCH 87/96] defer formatting, use log_msg_buffer for intermediate storage --- include/spdlog/sinks/ringbuffer_sink-inl.h | 12 ++++++------ include/spdlog/sinks/ringbuffer_sink.h | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/spdlog/sinks/ringbuffer_sink-inl.h b/include/spdlog/sinks/ringbuffer_sink-inl.h index d0f1a45b..d218a975 100644 --- a/include/spdlog/sinks/ringbuffer_sink-inl.h +++ b/include/spdlog/sinks/ringbuffer_sink-inl.h @@ -16,15 +16,13 @@ namespace sinks { template SPDLOG_INLINE ringbuffer_sink::ringbuffer_sink(size_t buf_size) { - buf=details::circular_q(buf_size); + buf_=details::circular_q(buf_size); } template SPDLOG_INLINE void ringbuffer_sink::sink_it_(const details::log_msg &msg) { - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - buf.push_back(fmt::to_string(formatted)); + buf_.push_back(details::log_msg_buffer{msg}); } template @@ -34,9 +32,11 @@ SPDLOG_INLINE std::vector ringbuffer_sink::last(size_t lim) std::vector ret; ret.reserve(lim); size_t num=0; - for(size_t i=0; i::formatter_->format(buf_.at(i), formatted); + ret.push_back(fmt::to_string(formatted)); if(lim>0 && num==lim) break; } return ret; diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h index c51628f4..a5933744 100644 --- a/include/spdlog/sinks/ringbuffer_sink.h +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -7,6 +7,7 @@ #include "spdlog/sinks/base_sink.h" #include "spdlog/details/synchronous_factory.h" #include "spdlog/details/circular_q.h" +#include "spdlog/details/log_msg_buffer.h" #include #include @@ -29,7 +30,7 @@ protected: void flush_() override {}; private: - details::circular_q buf; + details::circular_q buf_; }; using ringbuffer_sink_mt = ringbuffer_sink; From 63837530ed5575c87f740842d68c2137bc900683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Sat, 9 Nov 2019 19:26:49 +0100 Subject: [PATCH 88/96] Fix circular_q::size() empty case --- include/spdlog/details/circular_q.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index a37b7770..60b8d157 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -75,7 +75,7 @@ public: // Return number of elements actually stored size_t size() const { - if (tail_ > head_) + if (tail_ >= head_) { return tail_ - head_; } From 0cb38085a1e5da94fca9abbd6ba97cf929053a45 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 10 Nov 2019 00:08:50 +0200 Subject: [PATCH 89/96] Updated ringbuffer sink --- include/spdlog/details/circular_q.h | 6 +- include/spdlog/details/log_msg_buffer-inl.h | 4 +- include/spdlog/sinks/ringbuffer_sink-inl.h | 36 ++++++++---- include/spdlog/sinks/ringbuffer_sink.h | 63 ++++++++++++--------- 4 files changed, 65 insertions(+), 44 deletions(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index 60b8d157..325dbfea 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -77,11 +77,11 @@ public: { if (tail_ >= head_) { - return tail_ - head_; + return tail_ - head_; } else { - return max_items_ - (head_ - tail_ ); + return max_items_ - (head_ - tail_); } } @@ -90,7 +90,7 @@ public: const T &at(size_t i) const { assert(i < size()); - return v_[(head_+ i) % max_items_]; + return v_[(head_ + i) % max_items_]; } // Pop item from front. diff --git a/include/spdlog/details/log_msg_buffer-inl.h b/include/spdlog/details/log_msg_buffer-inl.h index 0c1d9f35..51f4d5b9 100644 --- a/include/spdlog/details/log_msg_buffer-inl.h +++ b/include/spdlog/details/log_msg_buffer-inl.h @@ -26,9 +26,7 @@ SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other) update_string_views(); } -SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT - : log_msg{other} - , buffer{std::move(other.buffer)} +SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT : log_msg{other}, buffer{std::move(other.buffer)} { update_string_views(); } diff --git a/include/spdlog/sinks/ringbuffer_sink-inl.h b/include/spdlog/sinks/ringbuffer_sink-inl.h index d218a975..7f4f48aa 100644 --- a/include/spdlog/sinks/ringbuffer_sink-inl.h +++ b/include/spdlog/sinks/ringbuffer_sink-inl.h @@ -3,20 +3,19 @@ #pragma once -#ifndef SPDLOG_HEADER_ONLY -#include "spdlog/sinks/ringbuffer_sink.h" -#endif +//#ifndef SPDLOG_HEADER_ONLY +//#include "spdlog/sinks/ringbuffer_sink.h" +//#endif #include "spdlog/common.h" -#include "spdlog/details/os.h" namespace spdlog { namespace sinks { template -SPDLOG_INLINE ringbuffer_sink::ringbuffer_sink(size_t buf_size) +SPDLOG_INLINE ringbuffer_sink::ringbuffer_sink(size_t n_items) { - buf_=details::circular_q(buf_size); + buf_ = details::circular_q(n_items); } template @@ -26,18 +25,31 @@ SPDLOG_INLINE void ringbuffer_sink::sink_it_(const details::log_msg &msg) } template -SPDLOG_INLINE std::vector ringbuffer_sink::last(size_t lim) +SPDLOG_INLINE std::vector ringbuffer_sink::formatted_messages(size_t lim) { std::lock_guard lock(base_sink::mutex_); + auto n_items = lim > 0 ? (std::min)(lim, buf_.size()) : buf_.size(); std::vector ret; - ret.reserve(lim); - size_t num=0; - for(size_t i=0; i::formatter_->format(buf_.at(i), formatted); ret.push_back(fmt::to_string(formatted)); - if(lim>0 && num==lim) break; + } + return ret; +} + +template +SPDLOG_INLINE std::vector ringbuffer_sink::raw_messages(size_t lim) +{ + std::lock_guard lock(base_sink::mutex_); + auto n_items = lim > 0 ? (std::min)(lim, buf_.size()) : buf_.size(); + std::vector ret; + ret.reserve(n_items); + for (size_t i = 0; i < n_items; i++) + { + ret.push_back(buf_.at(i)); } return ret; } diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h index a5933744..ee6ef555 100644 --- a/include/spdlog/sinks/ringbuffer_sink.h +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -3,9 +3,7 @@ #pragma once -#include "spdlog/details/null_mutex.h" #include "spdlog/sinks/base_sink.h" -#include "spdlog/details/synchronous_factory.h" #include "spdlog/details/circular_q.h" #include "spdlog/details/log_msg_buffer.h" @@ -22,15 +20,47 @@ template class ringbuffer_sink final : public base_sink { public: - explicit ringbuffer_sink(size_t buf_size); - std::vector last(size_t lim=0); + explicit ringbuffer_sink(size_t n_items) + : q_{n_items} + {} + + std::vector last_raw(size_t lim = 0) + { + std::lock_guard lock(base_sink::mutex_); + auto n_items = lim > 0 ? (std::min)(lim, q_.size()) : q_.size(); + std::vector ret; + ret.reserve(n_items); + for (size_t i = 0; i < n_items; i++) + { + ret.push_back(q_.at(i)); + } + return ret; + } + + std::vector last_formatted(size_t lim = 0) + { + std::lock_guard lock(base_sink::mutex_); + auto n_items = lim > 0 ? (std::min)(lim, q_.size()) : q_.size(); + std::vector ret; + ret.reserve(n_items); + for (size_t i = 0; i < n_items; i++) + { + memory_buf_t formatted; + base_sink::formatter_->format(q_.at(i), formatted); + ret.push_back(fmt::to_string(formatted)); + } + return ret; + } protected: - void sink_it_(const details::log_msg &msg) override; - void flush_() override {}; + void sink_it_(const details::log_msg &msg) override + { + q_.push_back(details::log_msg_buffer{msg}); + } + void flush_() override {} private: - details::circular_q buf_; + details::circular_q q_; }; using ringbuffer_sink_mt = ringbuffer_sink; @@ -38,23 +68,4 @@ using ringbuffer_sink_st = ringbuffer_sink; } // namespace sinks -// -// factory functions -// -template -inline std::shared_ptr basic_logger_mt(const std::string &logger_name, size_t buf_size) -{ - return Factory::template create(logger_name, buf_size); -} - -template -inline std::shared_ptr basic_logger_st(const std::string &logger_name, size_t buf_size) -{ - return Factory::template create(logger_name, buf_size); -} - } // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY -#include "ringbuffer_sink-inl.h" -#endif From 26f706ebe3e2c731fc2f00632fa8fcdbf2c90ec6 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 10 Nov 2019 00:11:39 +0200 Subject: [PATCH 90/96] Fixed #1302 --- include/spdlog/details/os-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index c8d8ab37..168cf59b 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -428,7 +428,7 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT #if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) { - if (wstr.size() > static_cast((std::numeric_limits::max()))) + if (wstr.size() > static_cast((std::numeric_limits::max)())) { SPDLOG_THROW(spdlog::spdlog_ex("UTF-16 string is too big to be converted to UTF-8")); } From 89b5bcfdc711d7100ce88c7ccb00994df2b99db9 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 10 Nov 2019 00:18:24 +0200 Subject: [PATCH 91/96] Updated ringbuffer sink --- include/spdlog/sinks/ringbuffer_sink.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h index ee6ef555..16f7cb56 100644 --- a/include/spdlog/sinks/ringbuffer_sink.h +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -6,6 +6,7 @@ #include "spdlog/sinks/base_sink.h" #include "spdlog/details/circular_q.h" #include "spdlog/details/log_msg_buffer.h" +#include "spdlog/details/null_mutex.h" #include #include From 4f66313440fe4d307b1d1e0a2550a9c12af75a95 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 10 Nov 2019 00:35:34 +0200 Subject: [PATCH 92/96] Fix #1308 --- include/spdlog/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 6edd6fa9..830220ef 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -62,7 +62,7 @@ #endif #ifndef SPDLOG_FUNCTION -#define SPDLOG_FUNCTION __FUNCTION__ +#define SPDLOG_FUNCTION static_cast(__FUNCTION__) #endif #ifdef SPDLOG_NO_EXCEPTIONS From 6db8beeadec80d2631cda1b4fbc268483794014c Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 10 Nov 2019 00:55:50 +0200 Subject: [PATCH 93/96] Removed un needed file --- include/spdlog/sinks/ringbuffer_sink-inl.h | 58 ---------------------- 1 file changed, 58 deletions(-) delete mode 100644 include/spdlog/sinks/ringbuffer_sink-inl.h diff --git a/include/spdlog/sinks/ringbuffer_sink-inl.h b/include/spdlog/sinks/ringbuffer_sink-inl.h deleted file mode 100644 index 7f4f48aa..00000000 --- a/include/spdlog/sinks/ringbuffer_sink-inl.h +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -//#ifndef SPDLOG_HEADER_ONLY -//#include "spdlog/sinks/ringbuffer_sink.h" -//#endif - -#include "spdlog/common.h" - -namespace spdlog { -namespace sinks { - -template -SPDLOG_INLINE ringbuffer_sink::ringbuffer_sink(size_t n_items) -{ - buf_ = details::circular_q(n_items); -} - -template -SPDLOG_INLINE void ringbuffer_sink::sink_it_(const details::log_msg &msg) -{ - buf_.push_back(details::log_msg_buffer{msg}); -} - -template -SPDLOG_INLINE std::vector ringbuffer_sink::formatted_messages(size_t lim) -{ - std::lock_guard lock(base_sink::mutex_); - auto n_items = lim > 0 ? (std::min)(lim, buf_.size()) : buf_.size(); - std::vector ret; - ret.reserve(n_items); - for (size_t i = 0; i < n_items; i++) - { - memory_buf_t formatted; - base_sink::formatter_->format(buf_.at(i), formatted); - ret.push_back(fmt::to_string(formatted)); - } - return ret; -} - -template -SPDLOG_INLINE std::vector ringbuffer_sink::raw_messages(size_t lim) -{ - std::lock_guard lock(base_sink::mutex_); - auto n_items = lim > 0 ? (std::min)(lim, buf_.size()) : buf_.size(); - std::vector ret; - ret.reserve(n_items); - for (size_t i = 0; i < n_items; i++) - { - ret.push_back(buf_.at(i)); - } - return ret; -} - -} // namespace sinks -} // namespace spdlog From e8f7f80f2be419669e28a127ae957fc63c4484e4 Mon Sep 17 00:00:00 2001 From: Zhi Sun Date: Mon, 11 Nov 2019 18:41:58 +0800 Subject: [PATCH 94/96] Remove redundant semicolon --- include/spdlog/spdlog.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 925f5021..facb0b3f 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -285,7 +285,7 @@ inline void critical(wstring_view_t fmt, const Args &... args) // SPDLOG_LEVEL_OFF // -#define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); +#define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE #define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) From cee35f7d246f7afb5fa6cc9d1509bacbea9201e7 Mon Sep 17 00:00:00 2001 From: gabime Date: Thu, 14 Nov 2019 12:28:23 +0200 Subject: [PATCH 95/96] Added truncate flag (issue #1297) --- .../spdlog/details/pattern_formatter-inl.h | 50 ++++++++++------ include/spdlog/details/pattern_formatter.h | 8 ++- tests/test_pattern_formatter.cpp | 58 ++++++++++++++++++- 3 files changed, 95 insertions(+), 21 deletions(-) diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index 8bee8a51..83d0283b 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -39,47 +39,50 @@ public: : padinfo_(padinfo) , dest_(dest) { - - if (padinfo_.width_ <= wrapped_size) + remaining_pad_ = static_cast(padinfo.width_) - static_cast(wrapped_size); + if (remaining_pad_ <= 0) { - total_pad_ = 0; return; } - total_pad_ = padinfo.width_ - wrapped_size; if (padinfo_.side_ == padding_info::left) { - pad_it(total_pad_); - total_pad_ = 0; + pad_it(remaining_pad_); + remaining_pad_ = 0; } else if (padinfo_.side_ == padding_info::center) { - auto half_pad = total_pad_ / 2; - auto reminder = total_pad_ & 1; + auto half_pad = remaining_pad_ / 2; + auto reminder = remaining_pad_ & 1; pad_it(half_pad); - total_pad_ = half_pad + reminder; // for the right side + remaining_pad_ = half_pad + reminder; // for the right side } } ~scoped_padder() { - if (total_pad_) + if (remaining_pad_ >= 0) + { + pad_it(remaining_pad_); + } + else if (padinfo_.truncate_) { - pad_it(total_pad_); + long new_size = static_cast(dest_.size()) + remaining_pad_; + dest_.resize(static_cast(new_size)); } } private: - void pad_it(size_t count) + void pad_it(long count) { // count = std::min(count, spaces_.size()); - assert(count <= spaces_.size()); + // assert(count <= spaces_.size()); fmt_helper::append_string_view(string_view_t(spaces_.data(), count), dest_); } const padding_info &padinfo_; memory_buf_t &dest_; - size_t total_pad_; + long remaining_pad_; string_view_t spaces_{" ", 64}; }; @@ -1209,7 +1212,7 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i } } -// Extract given pad spec (e.g. %8X) +// Extract given pad spec (e.g. %8X, %=8X, %-8!X, %8!X, %=8!X, %-8!X, %+8!X) // Advance the given it pass the end of the padding spec found (if any) // Return padding. SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end) @@ -1240,7 +1243,7 @@ SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::stri if (it == end || !std::isdigit(static_cast(*it))) { - return padding_info{0, side}; + return padding_info{}; // no padding if no digit found here } auto width = static_cast(*it) - '0'; @@ -1249,7 +1252,20 @@ SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::stri auto digit = static_cast(*it) - '0'; width = width * 10 + digit; } - return details::padding_info{std::min(width, max_width), side}; + + // search for the optional truncate marker '!' + bool truncate; + if (it != end && *it == '!') + { + truncate = true; + ++it; + } + else + { + truncate = false; + } + + return details::padding_info{std::min(width, max_width), side, truncate}; } SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &pattern) diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index b03d3394..fa134ada 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -29,17 +29,21 @@ struct padding_info }; padding_info() = default; - padding_info(size_t width, padding_info::pad_side side) + padding_info(size_t width, padding_info::pad_side side, bool truncate) : width_(width) , side_(side) + , truncate_(truncate) + , enabled_(true) {} bool enabled() const { - return width_ != 0; + return enabled_; } const size_t width_ = 0; const pad_side side_ = left; + bool truncate_ = false; + bool enabled_ = false; }; class flag_formatter diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 9bf8d4b1..6aff454c 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -1,4 +1,5 @@ #include "includes.h" +#include "test_sink.h" using spdlog::memory_buf_t; @@ -138,58 +139,111 @@ TEST_CASE("color range test6", "[pattern_formatter]") TEST_CASE("level_left_padded", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%8l] %v", spdlog::pattern_time_type::local, "\n") == "[ info] Some message\n"); + REQUIRE(log_to_str("Some message", "[%8!l] %v", spdlog::pattern_time_type::local, "\n") == "[ info] Some message\n"); } TEST_CASE("level_right_padded", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%-8l] %v", spdlog::pattern_time_type::local, "\n") == "[info ] Some message\n"); + REQUIRE(log_to_str("Some message", "[%-8!l] %v", spdlog::pattern_time_type::local, "\n") == "[info ] Some message\n"); } TEST_CASE("level_center_padded", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%=8l] %v", spdlog::pattern_time_type::local, "\n") == "[ info ] Some message\n"); + REQUIRE(log_to_str("Some message", "[%=8!l] %v", spdlog::pattern_time_type::local, "\n") == "[ info ] Some message\n"); } TEST_CASE("short level_left_padded", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%3L] %v", spdlog::pattern_time_type::local, "\n") == "[ I] Some message\n"); + REQUIRE(log_to_str("Some message", "[%3!L] %v", spdlog::pattern_time_type::local, "\n") == "[ I] Some message\n"); } TEST_CASE("short level_right_padded", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%-3L] %v", spdlog::pattern_time_type::local, "\n") == "[I ] Some message\n"); + REQUIRE(log_to_str("Some message", "[%-3!L] %v", spdlog::pattern_time_type::local, "\n") == "[I ] Some message\n"); } TEST_CASE("short level_center_padded", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%=3L] %v", spdlog::pattern_time_type::local, "\n") == "[ I ] Some message\n"); + REQUIRE(log_to_str("Some message", "[%=3!L] %v", spdlog::pattern_time_type::local, "\n") == "[ I ] Some message\n"); } TEST_CASE("left_padded_short", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%3n] %v", spdlog::pattern_time_type::local, "\n") == "[pattern_tester] Some message\n"); + REQUIRE(log_to_str("Some message", "[%3!n] %v", spdlog::pattern_time_type::local, "\n") == "[pat] Some message\n"); } TEST_CASE("right_padded_short", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%-3n] %v", spdlog::pattern_time_type::local, "\n") == "[pattern_tester] Some message\n"); + REQUIRE(log_to_str("Some message", "[%-3!n] %v", spdlog::pattern_time_type::local, "\n") == "[pat] Some message\n"); } TEST_CASE("center_padded_short", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%=3n] %v", spdlog::pattern_time_type::local, "\n") == "[pattern_tester] Some message\n"); + REQUIRE(log_to_str("Some message", "[%=3!n] %v", spdlog::pattern_time_type::local, "\n") == "[pat] Some message\n"); } TEST_CASE("left_padded_huge", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%-300n] %v", spdlog::pattern_time_type::local, "\n") == - "[pattern_tester ] Some message\n"); + "[pattern_tester ] Some message\n"); + + REQUIRE(log_to_str("Some message", "[%-300!n] %v", spdlog::pattern_time_type::local, "\n") == + "[pattern_tester ] Some message\n"); } TEST_CASE("left_padded_max", "[pattern_formatter]") { REQUIRE(log_to_str("Some message", "[%-64n] %v", spdlog::pattern_time_type::local, "\n") == - "[pattern_tester ] Some message\n"); + "[pattern_tester ] Some message\n"); + + REQUIRE(log_to_str("Some message", "[%-64!n] %v", spdlog::pattern_time_type::local, "\n") == + "[pattern_tester ] Some message\n"); +} + +// Test padding + truncate flag + +TEST_CASE("paddinng_truncate", "[pattern_formatter]") +{ + REQUIRE(log_to_str("123456", "%6!v", spdlog::pattern_time_type::local, "\n") == "123456\n"); + REQUIRE(log_to_str("123456", "%5!v", spdlog::pattern_time_type::local, "\n") == "12345\n"); + REQUIRE(log_to_str("123456", "%7!v", spdlog::pattern_time_type::local, "\n") == " 123456\n"); + + REQUIRE(log_to_str("123456", "%-6!v", spdlog::pattern_time_type::local, "\n") == "123456\n"); + REQUIRE(log_to_str("123456", "%-5!v", spdlog::pattern_time_type::local, "\n") == "12345\n"); + REQUIRE(log_to_str("123456", "%-7!v", spdlog::pattern_time_type::local, "\n") == "123456 \n"); + + REQUIRE(log_to_str("123456", "%=6!v", spdlog::pattern_time_type::local, "\n") == "123456\n"); + REQUIRE(log_to_str("123456", "%=5!v", spdlog::pattern_time_type::local, "\n") == "12345\n"); + REQUIRE(log_to_str("123456", "%=7!v", spdlog::pattern_time_type::local, "\n") == "123456 \n"); + + REQUIRE(log_to_str("123456", "%0!v", spdlog::pattern_time_type::local, "\n") == "\n"); +} + +TEST_CASE("paddinng_truncate_funcname", "[pattern_formatter]") +{ + spdlog::sinks::test_sink_st test_sink; + + const char* pattern = "%v [%5!!]"; + auto formatter = std::unique_ptr(new spdlog::pattern_formatter(pattern)); + test_sink.set_formatter(std::move(formatter)); + + spdlog::details::log_msg msg1{spdlog::source_loc{"ignored", 1, "func"}, "test_logger", spdlog::level::info, "message"}; + test_sink.log(msg1); + + spdlog::details::log_msg msg2{spdlog::source_loc{"ignored", 1, "function"}, "test_logger", spdlog::level::info, "message"}; + test_sink.log(msg2); + + auto lines = test_sink.lines(); + REQUIRE(lines[0] == "message [ func]"); + REQUIRE(lines[1] == "message [funct]"); } TEST_CASE("clone-default-formatter", "[pattern_formatter]") From 79259fdb3f57283f65185a8e41840968dae4043c Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sat, 16 Nov 2019 13:23:00 +0200 Subject: [PATCH 96/96] Update os-inl.h --- include/spdlog/details/os-inl.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 168cf59b..84b99969 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -238,9 +238,8 @@ SPDLOG_INLINE size_t filesize(FILE *f) { return static_cast(st.st_size); } -#else // unix 32 bits or cygwin +#else // other unix or linux 32 bits or cygwin struct stat st; - if (::fstat(fd, &st) == 0) { return static_cast(st.st_size);