From 3fbac8e2b7867611a2136d127fb83da768c731bc Mon Sep 17 00:00:00 2001 From: JamesReynolds Date: Fri, 1 Jun 2018 11:51:08 +0100 Subject: [PATCH 1/3] Closes #717 Failure to compile to systems that don't have a valid definition of strerror_r. --- include/spdlog/details/os.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index a9246de2..08adca5d 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -397,6 +397,11 @@ inline std::string errno_to_string(char buf[256], int res) return "Unknown error"; } +inline std::string errno_to_string(char buf[256], const fmt::internal::Null<> &/*tag*/) +{ + return errno_to_string(buf, -1); +} + // Return errno string (thread safe) inline std::string errno_str(int err_num) { From aa9012fa07a732711097f32e4ea56ca5dedba264 Mon Sep 17 00:00:00 2001 From: JamesReynolds Date: Fri, 1 Jun 2018 12:14:17 +0100 Subject: [PATCH 2/3] Fixes #717 This fix uses the fmt::safe_strerror instead of the underlying strerror_r functionality. I think this is better - but I'm not sure if there is a reason for avoiding the use of this function. --- include/spdlog/details/os.h | 49 +++---------------------------------- 1 file changed, 4 insertions(+), 45 deletions(-) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 08adca5d..04e0d712 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -383,57 +383,16 @@ inline std::string filename_to_str(const filename_t &filename) } #endif -inline std::string errno_to_string(char[256], char *res) -{ - return std::string(res); -} - -inline std::string errno_to_string(char buf[256], int res) -{ - if (res == 0) - { - return std::string(buf); - } - return "Unknown error"; -} - -inline std::string errno_to_string(char buf[256], const fmt::internal::Null<> &/*tag*/) -{ - return errno_to_string(buf, -1); -} - // Return errno string (thread safe) inline std::string errno_str(int err_num) { - char buf[256]; + char buf[256], *buf_ptr = buf; SPDLOG_CONSTEXPR auto buf_size = sizeof(buf); - -#ifdef _WIN32 - if (strerror_s(buf, buf_size, err_num) == 0) + if (fmt::safe_strerror(err_num, buf_ptr, buf_size) == 0) { - return std::string(buf); + return std::string(buf_ptr); } - else - { - return "Unknown error"; - } - -#elif defined(__FreeBSD__) || defined(__APPLE__) || defined(ANDROID) || defined(__SUNPRO_CC) || \ - ((_POSIX_C_SOURCE >= 200112L) && !defined(_GNU_SOURCE)) // posix version - - if (strerror_r(err_num, buf, buf_size) == 0) - { - return std::string(buf); - } - else - { - return "Unknown error"; - } - -#else // gnu version (might not use the given buf, so its retval pointer must be used) - auto err = strerror_r(err_num, buf, buf_size); // let compiler choose type - return errno_to_string(buf, err); // use overloading to select correct stringify function -#endif + return "Unknown error"; } inline int pid() From 7bc598c9b587b8aef405c9af4ddff417fc26df30 Mon Sep 17 00:00:00 2001 From: JamesReynolds Date: Fri, 1 Jun 2018 14:32:33 +0100 Subject: [PATCH 3/3] Move errno code into common errno code is no longer OS specific. Moving to common. --- include/spdlog/common.h | 15 +++++++++------ include/spdlog/details/os.h | 12 ------------ 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index e7f47fd2..e6d9a557 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -143,11 +143,6 @@ enum class pattern_time_type // // Log exception // -namespace details { -namespace os { -std::string errno_str(int err_num); -} -} // namespace details class spdlog_ex : public std::exception { public: @@ -158,7 +153,15 @@ public: spdlog_ex(const std::string &msg, int last_errno) { - _msg = msg + ": " + details::os::errno_str(last_errno); + char buf[256], *buf_ptr = buf; + SPDLOG_CONSTEXPR auto buf_size = sizeof(buf); + if (fmt::safe_strerror(last_errno, buf_ptr, buf_size) != 0) + { + buf_ptr = buf; + char unknown[] = "Unknown error"; + std::copy(unknown, unknown + sizeof(unknown), buf_ptr); + } + _msg = msg + ": " + std::string(buf_ptr); } const char *what() const SPDLOG_NOEXCEPT override diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 04e0d712..28eb53c3 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -383,18 +383,6 @@ inline std::string filename_to_str(const filename_t &filename) } #endif -// Return errno string (thread safe) -inline std::string errno_str(int err_num) -{ - char buf[256], *buf_ptr = buf; - SPDLOG_CONSTEXPR auto buf_size = sizeof(buf); - if (fmt::safe_strerror(err_num, buf_ptr, buf_size) == 0) - { - return std::string(buf_ptr); - } - return "Unknown error"; -} - inline int pid() {