From 3b87eb3d080b068e2641ae8f85bf7b8e6eeeecb0 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sat, 21 Mar 2020 23:25:12 +0200 Subject: [PATCH] Moved throw to dedicated function throw_spdlog_ex --- include/spdlog/async_logger-inl.h | 4 ++-- include/spdlog/common-inl.h | 10 ++++++++++ include/spdlog/common.h | 4 +++- include/spdlog/details/file_helper-inl.h | 8 ++++---- include/spdlog/details/os-inl.h | 5 +++-- include/spdlog/details/registry-inl.h | 2 +- include/spdlog/details/tcp_client-windows.h | 2 +- include/spdlog/details/thread_pool-inl.h | 4 ++-- include/spdlog/sinks/android_sink.h | 2 +- include/spdlog/sinks/daily_file_sink.h | 4 ++-- include/spdlog/sinks/systemd_sink.h | 2 +- include/spdlog/sinks/win_eventlog_sink.h | 2 +- include/spdlog/sinks/wincolor_sink-inl.h | 2 +- 13 files changed, 32 insertions(+), 19 deletions(-) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index 356db0e7..f8c9694c 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -32,7 +32,7 @@ SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg) } else { - SPDLOG_THROW(spdlog_ex("async log: thread pool doesn't exist anymore")); + throw_spdlog_ex("async log: thread pool doesn't exist anymore"); } } @@ -45,7 +45,7 @@ SPDLOG_INLINE void spdlog::async_logger::flush_() } else { - SPDLOG_THROW(spdlog_ex("async flush: thread pool doesn't exist anymore")); + throw_spdlog_ex("async flush: thread pool doesn't exist anymore"); } } diff --git a/include/spdlog/common-inl.h b/include/spdlog/common-inl.h index 411e2819..dcd1fea0 100644 --- a/include/spdlog/common-inl.h +++ b/include/spdlog/common-inl.h @@ -63,4 +63,14 @@ SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT return msg_.c_str(); } +SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno) +{ + SPDLOG_THROW( spdlog_ex(msg, last_errno)); +} + +SPDLOG_INLINE void throw_spdlog_ex(std::string msg) +{ + SPDLOG_THROW( spdlog_ex(std::move(msg))); +} + } // namespace spdlog diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 72a0e446..5b58c06a 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -204,6 +204,9 @@ private: std::string msg_; }; +void throw_spdlog_ex(const std::string &msg, int last_errno); +void throw_spdlog_ex(std::string msg); + struct source_loc { SPDLOG_CONSTEXPR source_loc() = default; @@ -236,7 +239,6 @@ std::unique_ptr make_unique(Args &&... args) } #endif } // namespace details - } // namespace spdlog #ifdef SPDLOG_HEADER_ONLY diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index cdd45f17..7cb00f2f 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -43,14 +43,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)); + 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")); + throw_spdlog_ex("Failed re opening file - was not opened before"); } this->open(filename_, truncate); } @@ -75,7 +75,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)); + throw_spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno); } } @@ -83,7 +83,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_))); + 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 06dd9ccb..98e4b7d7 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -204,7 +204,7 @@ SPDLOG_INLINE size_t filesize(FILE *f) { if (f == nullptr) { - SPDLOG_THROW(spdlog_ex("Failed getting file size. fd is null")); + throw_spdlog_ex("Failed getting file size. fd is null"); } #if defined(_WIN32) && !defined(__CYGWIN__) int fd = ::_fileno(f); @@ -245,7 +245,8 @@ SPDLOG_INLINE size_t filesize(FILE *f) } #endif #endif - SPDLOG_THROW(spdlog_ex("Failed getting file size from fd", errno)); + throw_spdlog_ex("Failed getting file size from fd", errno); + return 0; // will not be reached. } // Return utc offset in minutes or throw spdlog_ex on failure diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index b42383b7..33835d80 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -284,7 +284,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")); + throw_spdlog_ex("logger with name '" + logger_name + "' already exists"); } } diff --git a/include/spdlog/details/tcp_client-windows.h b/include/spdlog/details/tcp_client-windows.h index fb67351f..8f2e2fe7 100644 --- a/include/spdlog/details/tcp_client-windows.h +++ b/include/spdlog/details/tcp_client-windows.h @@ -55,7 +55,7 @@ class tcp_client ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL); - SPDLOG_THROW(spdlog_ex(fmt::format("tcp_sink - {}: {}", msg, buf))); + throw_spdlog_ex(fmt::format("tcp_sink - {}: {}", msg, buf)); } public: diff --git a/include/spdlog/details/thread_pool-inl.h b/include/spdlog/details/thread_pool-inl.h index 2789abfa..a211ea00 100644 --- a/include/spdlog/details/thread_pool-inl.h +++ b/include/spdlog/details/thread_pool-inl.h @@ -18,8 +18,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)")); + throw_spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid " + "range is 1-1000)"); } for (size_t i = 0; i < threads_n; i++) { diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h index bdbe542b..9686adff 100644 --- a/include/spdlog/sinks/android_sink.h +++ b/include/spdlog/sinks/android_sink.h @@ -64,7 +64,7 @@ protected: if (ret < 0) { - SPDLOG_THROW(spdlog_ex("__android_log_write() failed", ret)); + throw_spdlog_ex("__android_log_write() failed", ret); } } diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 8ed5a043..a3843af7 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -56,7 +56,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")); + throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); } auto now = log_clock::now(); @@ -164,7 +164,7 @@ private: if (!ok) { filenames_q_.push_back(std::move(current_file)); - SPDLOG_THROW(spdlog_ex("Failed removing daily file " + filename_to_str(old_filename), errno)); + throw_spdlog_ex("Failed removing daily 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 d90edb22..d23824b8 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -72,7 +72,7 @@ protected: if (err) { - SPDLOG_THROW(spdlog_ex("Failed writing to systemd", errno)); + throw_spdlog_ex("Failed writing to systemd", errno); } } diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index 3e858940..a0a14841 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -91,7 +91,7 @@ public: { if (!::IsValidSid(psid)) { - SPDLOG_THROW(spdlog_ex("sid_t::sid_t(): invalid SID received")); + throw_spdlog_ex("sid_t::sid_t(): invalid SID received"); } auto const sid_length{::GetLengthSid(psid)}; diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 04b74942..71e3a8ce 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -158,7 +158,7 @@ void SPDLOG_INLINE wincolor_sink::write_to_file_(const memory_buf_ bool ok = ::WriteFile(out_handle_, formatted.data() + total_written, size - total_written, &bytes_written, nullptr) != 0; if (!ok || bytes_written == 0) { - SPDLOG_THROW(spdlog_ex("wincolor_sink: write_to_file_ failed. GetLastError(): " + std::to_string(::GetLastError()))); + throw_spdlog_ex("wincolor_sink: write_to_file_ failed. GetLastError(): " + std::to_string(::GetLastError())); } total_written += bytes_written; } while (total_written < size);