diff --git a/CMakeLists.txt b/CMakeLists.txt index e4ea842f..e616b855 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,7 +76,6 @@ option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF) option(SPDLOG_SYSTEM_INCLUDES "Include as system headers (skip for clang-tidy)." OFF) option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of of fetching from gitub." OFF) -option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF) if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") option(SPDLOG_CLOCK_COARSE "Use CLOCK_REALTIME_COARSE instead of the regular clock," OFF) @@ -86,10 +85,6 @@ endif() option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" 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_ATOMIC_LEVELS - "prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently" - OFF) option(SPDLOG_DISABLE_DEFAULT_LOGGER "Disable default logger creation" OFF) # clang-tidy @@ -245,14 +240,6 @@ if(HAVE_FWRITE_UNLOCKED) set(SPDLOG_FWRITE_UNLOCKED 1) endif() -# --------------------------------------------------------------------------------------- -# Generate spdlog_config.h based on the current configuration -# --------------------------------------------------------------------------------------- -set(OUT_CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/include/spdlog/spdlog_config.h") -message(STATUS "Generating ${OUT_CONFIG_FILE}") -configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/spdlog_config.h.in" ${OUT_CONFIG_FILE} @ONLY) -list(APPEND SPDLOG_HEADERS ${OUT_CONFIG_FILE}) - # --------------------------------------------------------------------------------------- # spdlog library # --------------------------------------------------------------------------------------- @@ -307,17 +294,20 @@ if(ANDROID) endif() # --------------------------------------------------------------------------------------- -# If exceptions are disabled, disable them in the bundled fmt as well +# spdlog private defines according to the options # --------------------------------------------------------------------------------------- -if(SPDLOG_NO_EXCEPTIONS) - # Add compile definition for the fmt target - target_compile_definitions(fmt PUBLIC FMT_EXCEPTIONS=0) - if(NOT MSVC) - target_compile_options(spdlog PRIVATE -fno-exceptions) - else() - target_compile_options(spdlog PRIVATE /EHs-c-) +foreach( + SPDLOG_OPTION + SPDLOG_CLOCK_COARSE + SPDLOG_PREVENT_CHILD_FD + SPDLOG_NO_THREAD_ID + SPDLOG_DISABLE_DEFAULT_LOGGER + SPDLOG_FWRITE_UNLOCKED) + if(${SPDLOG_OPTION}) + target_compile_definitions(spdlog PRIVATE ${SPDLOG_OPTION}) endif() -endif() +endforeach() + # --------------------------------------------------------------------------------------- # Build binaries # --------------------------------------------------------------------------------------- @@ -394,8 +384,4 @@ if(SPDLOG_INSTALL) # Support creation of installable packages # --------------------------------------------------------------------------------------- include(cmake/spdlogCPack.cmake) - - # Install spdlog_config.h file Assume your tweakme.h.in is located in the same directory as CMakeLists.txt - set(TWEAKME_IN "${CMAKE_CURRENT_SOURCE_DIR}/tweakme.h.in") - set(TWEAKME_OUT "${CMAKE_CURRENT_BINARY_DIR}/include/spdlog/tweakme.h") endif() diff --git a/cmake/spdlog_config.h.in b/cmake/spdlog_config.h.in deleted file mode 100644 index 6c2fe3b4..00000000 --- a/cmake/spdlog_config.h.in +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -// DO NOT EDIT -// Auto generated file with compile time flags with which spdlog was compiled - -#pragma once - - -// No exceptions. Will do std::abort() instead -#cmakedefine SPDLOG_NO_EXCEPTIONS - -// Use CLOCK_REALTIME_COARSE on Linux -#cmakedefine SPDLOG_CLOCK_COARSE - -// Prevent child processes from inheriting log file descriptors -#cmakedefine SPDLOG_PREVENT_CHILD_FD - -// Prevent spdlog from querying the thread id on each log call -#cmakedefine SPDLOG_NO_THREAD_ID - -// Prevent spdlog from using std::atomic for log levels -#cmakedefine SPDLOG_NO_ATOMIC_LEVELS - -// Disable default logger creation -#cmakedefine SPDLOG_DISABLE_DEFAULT_LOGGER - -// Use external fmtlib instead of bundled -#cmakedefine SPDLOG_FMT_EXTERNAL - -// Whether fwrite_unlocked is available -#cmakedefine SPDLOG_FWRITE_UNLOCKED diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 9275ed45..da8e1a09 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -15,7 +15,7 @@ #include #include "./source_loc.h" -#include "./spdlog_config.h" + #if defined(SPDLOG_SHARED_LIB) #if defined(_WIN32) @@ -40,22 +40,6 @@ #define SPDLOG_FUNCTION static_cast(__FUNCTION__) #endif -#ifdef SPDLOG_NO_EXCEPTIONS - #define SPDLOG_TRY - #define SPDLOG_THROW(ex) \ - do { \ - printf("spdlog fatal error: %s\n", ex.what()); \ - std::abort(); \ - } while (0) - #define SPDLOG_CATCH_STD -#else - #define SPDLOG_TRY try - #define SPDLOG_THROW(ex) throw(ex) - #define SPDLOG_CATCH_STD \ - catch (const std::exception &) { \ - } -#endif - namespace spdlog { class formatter; @@ -106,11 +90,7 @@ enum class level { n_levels }; -#if defined(SPDLOG_NO_ATOMIC_LEVELS) -using atomic_level_t = details::null_atomic; -#else using atomic_level_t = std::atomic; -#endif [[nodiscard]] constexpr size_t level_to_number(level lvl) noexcept { return static_cast(lvl); } diff --git a/include/spdlog/fmt/fmt.h b/include/spdlog/fmt/fmt.h index af6f5203..a261e14b 100644 --- a/include/spdlog/fmt/fmt.h +++ b/include/spdlog/fmt/fmt.h @@ -5,5 +5,4 @@ #pragma once -#include "../spdlog_config.h" #include "fmt/format.h" diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 36f8df9a..fd009fbe 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -22,22 +22,20 @@ #include "./details/log_msg.h" #include "./sinks/sink.h" -#ifndef SPDLOG_NO_EXCEPTIONS - #define SPDLOG_LOGGER_CATCH(location) \ - catch (const std::exception &ex) { \ - if (!location.empty()) { \ - err_handler_(fmt_lib::format(SPDLOG_FMT_STRING("{} [{}({})]"), ex.what(), location.filename, location.line)); \ - } else { \ - err_handler_(ex.what()); \ - } \ - } \ - catch (...) { \ - err_handler_("Rethrowing unknown exception in logger"); \ - throw; \ - } -#else - #define SPDLOG_LOGGER_CATCH(location) -#endif + +#define SPDLOG_LOGGER_CATCH(location) \ + catch (const std::exception &ex) { \ + if (!location.empty()) { \ + err_handler_(fmt_lib::format(SPDLOG_FMT_STRING("{} [{}({})]"), ex.what(), location.filename, location.line)); \ + } else { \ + err_handler_(ex.what()); \ + } \ + } \ + catch (...) { \ + err_handler_("Rethrowing unknown exception in logger"); \ + throw; \ + } + namespace spdlog { @@ -186,7 +184,7 @@ protected: template void log_with_format_(source_loc loc, const level lvl, const format_string_t &format_string, Args &&...args) { assert(should_log(lvl)); - SPDLOG_TRY { + try { memory_buf_t buf; fmt::vformat_to(std::back_inserter(buf), format_string, fmt::make_format_args(args...)); sink_it_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()))); @@ -199,7 +197,7 @@ protected: assert(should_log(msg.log_level)); for (auto &sink : sinks_) { if (sink->should_log(msg.log_level)) { - SPDLOG_TRY { sink->log(msg); } + try { sink->log(msg); } SPDLOG_LOGGER_CATCH(msg.source) } } diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h index 963a054b..a4c72543 100644 --- a/include/spdlog/sinks/hourly_file_sink.h +++ b/include/spdlog/sinks/hourly_file_sink.h @@ -146,7 +146,7 @@ private: bool ok = remove_if_exists(old_filename) == 0; if (!ok) { filenames_q_.push_back(std::move(current_file)); - SPDLOG_THROW(spdlog_ex("Failed removing hourly file " + filename_to_str(old_filename), errno)); + throw(spdlog_ex("Failed removing hourly file " + filename_to_str(old_filename), errno)); } } filenames_q_.push_back(std::move(current_file)); diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index 28ac28ad..6d8b8df7 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -69,17 +69,13 @@ protected: // Note: function call inside '()' to avoid macro expansion err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), payload.data(), "PRIORITY=%d", syslog_level(msg.log_level), -#ifndef SPDLOG_NO_THREAD_ID "TID=%zu", msg.thread_id, -#endif "SYSLOG_IDENTIFIER=%.*s", static_cast(syslog_identifier.size()), syslog_identifier.data(), nullptr); } else { err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), payload.data(), "PRIORITY=%d", syslog_level(msg.log_level), -#ifndef SPDLOG_NO_THREAD_ID "TID=%zu", msg.thread_id, -#endif "SYSLOG_IDENTIFIER=%.*s", static_cast(syslog_identifier.size()), syslog_identifier.data(), "CODE_FILE=%s", msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr); diff --git a/src/async_logger.cpp b/src/async_logger.cpp index c0937a47..5e4db513 100644 --- a/src/async_logger.cpp +++ b/src/async_logger.cpp @@ -23,7 +23,7 @@ spdlog::async_logger::async_logger(std::string logger_name, // send the log message to the thread pool void spdlog::async_logger::sink_it_(const details::log_msg &msg) { - SPDLOG_TRY { + try { if (auto pool_ptr = thread_pool_.lock()) { pool_ptr->post_log(shared_from_this(), msg, overflow_policy_); } else { @@ -35,7 +35,7 @@ void spdlog::async_logger::sink_it_(const details::log_msg &msg) { // send flush request to the thread pool void spdlog::async_logger::flush_() { - SPDLOG_TRY { + try { if (auto pool_ptr = thread_pool_.lock()) { pool_ptr->post_flush(shared_from_this(), overflow_policy_); } else { @@ -51,7 +51,7 @@ void spdlog::async_logger::flush_() { void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) { for (auto &sink : sinks_) { if (sink->should_log(msg.log_level)) { - SPDLOG_TRY { sink->log(msg); } + try { sink->log(msg); } SPDLOG_LOGGER_CATCH(msg.source) } } @@ -63,7 +63,7 @@ void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) { void spdlog::async_logger::backend_flush_() { for (auto &sink : sinks_) { - SPDLOG_TRY { sink->flush(); } + try { sink->flush(); } SPDLOG_LOGGER_CATCH(source_loc()) } } diff --git a/src/common.cpp b/src/common.cpp index b1845bb7..82816cc7 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -33,8 +33,8 @@ spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) { const char *spdlog_ex::what() const noexcept { return msg_.c_str(); } -void throw_spdlog_ex(const std::string &msg, int last_errno) { SPDLOG_THROW(spdlog_ex(msg, last_errno)); } +void throw_spdlog_ex(const std::string &msg, int last_errno) { throw(spdlog_ex(msg, last_errno)); } -void throw_spdlog_ex(std::string msg) { SPDLOG_THROW(spdlog_ex(std::move(msg))); } +void throw_spdlog_ex(std::string msg) { throw(spdlog_ex(std::move(msg))); } } // namespace spdlog diff --git a/src/details/log_msg.cpp b/src/details/log_msg.cpp index ee1f7f9b..fe5f705b 100644 --- a/src/details/log_msg.cpp +++ b/src/details/log_msg.cpp @@ -15,12 +15,13 @@ log_msg::log_msg(spdlog::log_clock::time_point log_time, spdlog::string_view_t msg) : logger_name(a_logger_name), log_level(lvl), - time(log_time) -#ifndef SPDLOG_NO_THREAD_ID - , - thread_id(os::thread_id()) + time(log_time), + +#ifdef SPDLOG_NO_THREAD_ID + thread_id(0), +#else + thread_id(os::thread_id()), #endif - , source(loc), payload(msg) { } diff --git a/src/details/thread_pool.cpp b/src/details/thread_pool.cpp index 9f61b2a2..7bd0b93f 100644 --- a/src/details/thread_pool.cpp +++ b/src/details/thread_pool.cpp @@ -37,7 +37,7 @@ thread_pool::thread_pool(size_t q_max_items, size_t threads_n) // message all threads to terminate gracefully join them thread_pool::~thread_pool() { - SPDLOG_TRY { + try { for (size_t i = 0; i < threads_.size(); i++) { post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block); } @@ -46,7 +46,7 @@ thread_pool::~thread_pool() { t.join(); } } - SPDLOG_CATCH_STD + catch (...) {} } void thread_pool::post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy) { diff --git a/src/logger.cpp b/src/logger.cpp index 96dbcad6..2e6b99ae 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -75,7 +75,7 @@ std::shared_ptr logger::clone(std::string logger_name) { // private/protected methods void logger::flush_() { for (auto &sink : sinks_) { - SPDLOG_TRY { sink->flush(); } + try { sink->flush(); } SPDLOG_LOGGER_CATCH(source_loc()) } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a963fc72..647fff0f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -53,16 +53,13 @@ set(SPDLOG_UTESTS_SOURCES test_no_source_location.cpp test_log_level.cpp test_include_sinks.cpp - test_bin_to_hex.cpp) + test_bin_to_hex.cpp + test_errors.cpp) if(WIN32) list(APPEND SPDLOG_UTESTS_SOURCES test_eventlog.cpp) endif() -if(NOT SPDLOG_NO_EXCEPTIONS) - list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp) -endif() - if(systemd_FOUND) list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp) endif() diff --git a/tests/test_registry.cpp b/tests/test_registry.cpp index 1cbf6440..467d944f 100644 --- a/tests/test_registry.cpp +++ b/tests/test_registry.cpp @@ -7,7 +7,6 @@ static const char *const tested_logger_name = "null_logger"; static const char *const tested_logger_name2 = "null_logger2"; -#ifndef SPDLOG_NO_EXCEPTIONS TEST_CASE("register_drop", "[registry]") { spdlog::drop_all(); spdlog::create(tested_logger_name); @@ -24,7 +23,6 @@ TEST_CASE("explicit register", "[registry]") { // Throw if registering existing name REQUIRE_THROWS_AS(spdlog::create(tested_logger_name), spdlog::spdlog_ex); } -#endif TEST_CASE("apply_all", "[registry]") { spdlog::drop_all();