From cf2bf488a22373b46eee36513335ff0e44355abc Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 4 Oct 2019 21:49:20 +0300 Subject: [PATCH 1/7] 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 2/7] 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 3/7] 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 4/7] 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 5/7] 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 6/7] 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 7/7] 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).