diff --git a/CMakeLists.txt b/CMakeLists.txt index 7efe5049..12320fbf 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(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}") + #--------------------------------------------------------------------------------------- + # 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/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). diff --git a/cmake/spdlog.pc.in b/cmake/spdlog.pc.in index 86f5905f..861707c3 100644 --- a/cmake/spdlog.pc.in +++ b/cmake/spdlog.pc.in @@ -7,5 +7,7 @@ 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 -pthread +Requires: @PKG_CONFIG_REQUIRES@ + 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/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; } 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/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) 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); +}