diff --git a/CMakeLists.txt b/CMakeLists.txt index b24e412c..3c912064 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,16 +56,14 @@ option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/ option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF) # install options -option(SPDLOG_INSTALL "Generate the install target." ${SPDLOG_MASTER_PROJECT}) +option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF) if(WIN32) option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF) endif() -option(SPDLOG_NO_EXCEPTIONS "Support for -fno-exceptions. Replace throw with std::abort" OFF) - - +option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF) find_package(Threads REQUIRED) @@ -125,6 +123,7 @@ if(SPDLOG_WCHAR_SUPPORT) if(SPDLOG_NO_EXCEPTIONS) target_compile_definitions(spdlog PUBLIC SPDLOG_NO_EXCEPTIONS) + target_compile_options(spdlog PRIVATE -fno-exceptions) target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_NO_EXCEPTIONS) endif() diff --git a/example/example.cpp b/example/example.cpp index ce57fc9d..b2ee6d08 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -23,7 +23,7 @@ void clone_example(); int main(int, char *[]) { - spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH); + spdlog::info("Welcome to spdlog version {}{}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH); spdlog::warn("Easy padding in numbers like {:08d}", 12); spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); spdlog::info("Support for floats {:03.2f}", 1.23456); @@ -89,7 +89,7 @@ void stdout_logger_example() void basic_example() { // Create basic file logger (not rotated). - auto my_logger = spdlog::basic_logger_mt("file_logger", "logs/basic-log.txt"); + auto my_logger = spdlog::basic_logger_mt("file_logger", "lodgs/basic-log.txt"); } #include "spdlog/sinks/rotating_file_sink.h" diff --git a/include/spdlog/async.h b/include/spdlog/async.h index c24ecc19..9516400c 100644 --- a/include/spdlog/async.h +++ b/include/spdlog/async.h @@ -40,7 +40,7 @@ struct async_factory_impl auto ®istry_inst = details::registry::instance(); // create global thread pool if not already exists.. - auto& mutex = registry_inst.tp_mutex(); + auto &mutex = registry_inst.tp_mutex(); std::lock_guard tp_lock(mutex); auto tp = registry_inst.get_tp(); if (tp == nullptr) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index aaa6f1e4..45c6d8a9 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -33,7 +33,7 @@ SPDLOG_INLINE void spdlog::async_logger::sink_it_(details::log_msg &msg) } else { - SPDLOG_THROW spdlog_ex("async log: thread pool doesn't exist anymore"); + SPDLOG_THROW(spdlog_ex("async log: thread pool doesn't exist anymore")); } } @@ -46,7 +46,7 @@ SPDLOG_INLINE void spdlog::async_logger::flush_() } else { - SPDLOG_THROW spdlog_ex("async flush: thread pool doesn't exist anymore"); + SPDLOG_THROW(spdlog_ex("async flush: thread pool doesn't exist anymore")); } } @@ -55,7 +55,7 @@ SPDLOG_INLINE void spdlog::async_logger::flush_() // SPDLOG_INLINE void spdlog::async_logger::backend_log_(const details::log_msg &incoming_log_msg) { - try + SPDLOG_TRY { for (auto &s : sinks_) { @@ -73,16 +73,10 @@ SPDLOG_INLINE void spdlog::async_logger::backend_log_(const details::log_msg &in } } -SPDLOG_INLINE void spdlog::async_logger::backend_flush_() -{ - try - { - for (auto &sink : sinks_) - { - sink->flush(); - } - } - SPDLOG_LOGGER_CATCH() +SPDLOG_INLINE void spdlog::async_logger::backend_flush_(){SPDLOG_TRY{for (auto &sink : sinks_){sink->flush(); +} +} +SPDLOG_LOGGER_CATCH() } SPDLOG_INLINE std::shared_ptr spdlog::async_logger::clone(std::string new_name) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index aa7a501b..8058cada 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -65,9 +65,18 @@ #endif #ifdef SPDLOG_NO_EXCEPTIONS -#define SPDLOG_THROW +#define SPDLOG_TRY +#define SPDLOG_THROW(ex) \ + do \ + { \ + printf("spdlog fatal error: %s\n", ex.what()); \ + std::abort(); \ + } while (0) +#define SPDLOG_CATCH_ALL() #else -#define SPDLOG_THROW throw +#define SPDLOG_TRY try +#define SPDLOG_THROW(ex) throw(ex) +#define SPDLOG_CATCH_ALL() catch (...) #endif namespace spdlog { @@ -103,11 +112,13 @@ using string_view_t = basic_string_view_t; using wstring_view_t = basic_string_view_t; template -struct is_convertible_to_wstring_view : std::is_convertible { }; +struct is_convertible_to_wstring_view : std::is_convertible +{}; #endif // _WIN32 #else template -struct is_convertible_to_wstring_view : std::false_type { }; +struct is_convertible_to_wstring_view : std::false_type +{}; #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT #if defined(SPDLOG_NO_ATOMIC_LEVELS) diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index b02da238..2175ed48 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -39,14 +39,14 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) 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()) { - SPDLOG_THROW spdlog_ex("Failed re opening file - was not opened before"); + SPDLOG_THROW(spdlog_ex("Failed re opening file - was not opened before")); } open(_filename, truncate); } @@ -71,7 +71,7 @@ SPDLOG_INLINE void file_helper::write(const fmt::memory_buffer &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)); } } @@ -79,7 +79,7 @@ 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_); } diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 2a238d94..27f23a03 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -126,7 +126,7 @@ SPDLOG_INLINE void prevent_child_fd(FILE *f) auto fd = fileno(f); if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { - SPDLOG_THROW spdlog_ex("fcntl with FD_CLOEXEC failed", errno); + SPDLOG_THROW(spdlog_ex("fcntl with FD_CLOEXEC failed", errno)); } #endif } @@ -192,7 +192,7 @@ SPDLOG_INLINE size_t filesize(FILE *f) { if (f == nullptr) { - SPDLOG_THROW spdlog_ex("Failed getting file size. fd is null"); + SPDLOG_THROW(spdlog_ex("Failed getting file size. fd is null")); } #if defined(_WIN32) && !defined(__CYGWIN__) int fd = _fileno(f); @@ -229,7 +229,7 @@ SPDLOG_INLINE size_t filesize(FILE *f) } #endif #endif - SPDLOG_THROW spdlog_ex("Failed getting file size from fd", errno); + SPDLOG_THROW(spdlog_ex("Failed getting file size from fd", errno)); } // Return utc offset in minutes or throw spdlog_ex on failure @@ -245,7 +245,7 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) auto rv = GetDynamicTimeZoneInformation(&tzinfo); #endif if (rv == TIME_ZONE_ID_INVALID) - throw spdlog::spdlog_ex("Failed getting timezone info. ", errno); + SPDLOG_THROW(spdlog::spdlog_ex("Failed getting timezone info. ", errno)); int offset = -tzinfo.Bias; if (tm.tm_isdst) @@ -408,7 +408,7 @@ SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t wstr, fmt::memor { if (wstr.size() > static_cast(std::numeric_limits::max())) { - throw spdlog::spdlog_ex("UTF-16 string is too big to be converted to UTF-8"); + SPDLOG_THROW(spdlog::spdlog_ex("UTF-16 string is too big to be converted to UTF-8")); } int wstr_size = static_cast(wstr.size()); @@ -436,7 +436,7 @@ SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t wstr, fmt::memor } } - throw spdlog::spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError())); + SPDLOG_THROW(spdlog::spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()))); } #endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index ac511620..5b3d1dc7 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -245,7 +245,7 @@ SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name) { if (loggers_.find(logger_name) != loggers_.end()) { - SPDLOG_THROW spdlog_ex("logger with name '" + logger_name + "' already exists"); + SPDLOG_THROW(spdlog_ex("logger with name '" + logger_name + "' already exists")); } } diff --git a/include/spdlog/details/thread_pool-inl.h b/include/spdlog/details/thread_pool-inl.h index e746ad13..eecfa45c 100644 --- a/include/spdlog/details/thread_pool-inl.h +++ b/include/spdlog/details/thread_pool-inl.h @@ -17,8 +17,8 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std { if (threads_n == 0 || threads_n > 1000) { - SPDLOG_THROW spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid " - "range is 1-1000)"); + SPDLOG_THROW(spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid " + "range is 1-1000)")); } for (size_t i = 0; i < threads_n; i++) { @@ -36,7 +36,7 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n) // message all threads to terminate gracefully join them SPDLOG_INLINE thread_pool::~thread_pool() { - try + SPDLOG_TRY { for (size_t i = 0; i < threads_.size(); i++) { @@ -48,8 +48,7 @@ SPDLOG_INLINE thread_pool::~thread_pool() t.join(); } } - catch (...) - {} + SPDLOG_CATCH_ALL() {} } void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr, details::log_msg &msg, async_overflow_policy overflow_policy) diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index fcfa00b4..ffc46d61 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -31,20 +31,20 @@ enum class async_msg_type // Movable only. should never be copied struct async_msg { - async_msg_type msg_type; - level::level_enum level; + async_msg_type msg_type; + level::level_enum level; log_clock::time_point time; - size_t thread_id; + size_t thread_id; fmt::basic_memory_buffer raw; source_loc source; async_logger_ptr worker_ptr; - async_msg() - :msg_type(async_msg_type::log), - level(level::info), - thread_id(0) - {} + async_msg() + : msg_type(async_msg_type::log) + , level(level::info) + , thread_id(0) + {} ~async_msg() = default; // should only be moved in or out of the queue.. @@ -52,17 +52,18 @@ struct async_msg // support for vs2013 move #if defined(_MSC_VER) && _MSC_VER <= 1800 - async_msg(async_msg &&other) : msg_type(other.msg_type), - level(other.level), - time(other.time), - thread_id(other.thread_id), - raw(move(other.raw)), - msg_id(other.msg_id), - source(other.source), - worker_ptr(std::move(other.worker_ptr)) + async_msg(async_msg &&other) + : msg_type(other.msg_type) + , level(other.level) + , time(other.time) + , thread_id(other.thread_id) + , raw(move(other.raw)) + , msg_id(other.msg_id) + , source(other.source) + , worker_ptr(std::move(other.worker_ptr)) {} - async_msg &operator=(async_msg &&other) + async_msg &operator=(async_msg &&other) { msg_type = other.msg_type; level = other.level; diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index f9f74ae6..52d3fd5f 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -23,12 +23,11 @@ SPDLOG_INLINE logger::logger(const logger &other) , custom_err_handler_(other.custom_err_handler_) {} -SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT - : name_(std::move(other.name_)) - , sinks_(std::move(other.sinks_)) - , level_(other.level_.load(std::memory_order_relaxed)) - , flush_level_(other.flush_level_.load(std::memory_order_relaxed)) - , custom_err_handler_(std::move(other.custom_err_handler_)) +SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT : name_(std::move(other.name_)), + sinks_(std::move(other.sinks_)), + level_(other.level_.load(std::memory_order_relaxed)), + flush_level_(other.flush_level_.load(std::memory_order_relaxed)), + custom_err_handler_(std::move(other.custom_err_handler_)) {} SPDLOG_INLINE logger &logger::operator=(logger other) SPDLOG_NOEXCEPT @@ -168,7 +167,7 @@ SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg) { if (sink->should_log(msg.level)) { - try + SPDLOG_TRY { sink->log(msg); } @@ -186,7 +185,7 @@ SPDLOG_INLINE void logger::flush_() { for (auto &sink : sinks_) { - try + SPDLOG_TRY { sink->flush(); } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index c782e21a..2778fef4 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -23,7 +23,7 @@ #endif #include - +#ifndef SPDLOG_NO_EXCEPTIONS #define SPDLOG_LOGGER_CATCH() \ catch (const std::exception &ex) \ { \ @@ -33,6 +33,9 @@ { \ err_handler_("Unknown exception in logger"); \ } +#else +#define SPDLOG_LOGGER_CATCH() +#endif namespace spdlog { class logger @@ -63,7 +66,7 @@ public: virtual ~logger() = default; - logger(const logger &other) ; + logger(const logger &other); logger(logger &&other) SPDLOG_NOEXCEPT; logger &operator=(logger other) SPDLOG_NOEXCEPT; @@ -72,7 +75,7 @@ public: template void force_log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) { - try + SPDLOG_TRY { fmt::memory_buffer buf; fmt::format_to(buf, fmt, args...); @@ -156,14 +159,16 @@ public: } // T cannot be statically converted to string_view or wstring_view - template::value && !is_convertible_to_wstring_view::value, T>::type * = nullptr> + template::value && + !is_convertible_to_wstring_view::value, + T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { if (!should_log(lvl)) { return; } - try + SPDLOG_TRY { fmt::memory_buffer buf; fmt::format_to(buf, "{}", msg); diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 86810bae..869b4466 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -52,7 +52,7 @@ public: { if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) { - SPDLOG_THROW spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); + SPDLOG_THROW(spdlog_ex("daily_file_sink: Invalid rotation time in ctor")); } auto now = log_clock::now(); file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(now)), truncate_); diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index 55f64bb9..cb67e337 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -112,7 +112,8 @@ SPDLOG_INLINE void rotating_file_sink::rotate_() { file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit! current_size_ = 0; - SPDLOG_THROW spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno); + SPDLOG_THROW( + spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno)); } } } diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index 7e05f8e1..a0d6e31e 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -66,7 +66,7 @@ protected: if (err) { - SPDLOG_THROW spdlog_ex("Failed writing to systemd", errno); + SPDLOG_THROW(spdlog_ex("Failed writing to systemd", errno)); } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 49c2744e..0c64ba43 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,7 +8,6 @@ if(PkgConfig_FOUND) endif() set(SPDLOG_UTESTS_SOURCES - test_errors.cpp test_file_helper.cpp test_file_logging.cpp test_misc.cpp @@ -26,6 +25,10 @@ set(SPDLOG_UTESTS_SOURCES test_stdout_api.cpp test_dup_filter.cpp) +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 ac23b8cc..9759a505 100644 --- a/tests/test_registry.cpp +++ b/tests/test_registry.cpp @@ -3,6 +3,7 @@ static const char *tested_logger_name = "null_logger"; static const char *tested_logger_name2 = "null_logger2"; +#ifndef SPDLOG_NO_EXCEPTIONS TEST_CASE("register_drop", "[registry]") { spdlog::drop_all(); @@ -21,6 +22,7 @@ TEST_CASE("explicit register", "[registry]") // Throw if registring existing name REQUIRE_THROWS_AS(spdlog::create(tested_logger_name), spdlog::spdlog_ex); } +#endif TEST_CASE("apply_all", "[registry]") {