From c5c6baad74de64a557a71b9a556bd571995c93f8 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 15 Jul 2016 17:48:02 +0300 Subject: [PATCH] Added errno description to sdlog exception strings --- include/spdlog/common.h | 7 +++++++ include/spdlog/details/file_helper.h | 11 ++++++----- include/spdlog/details/os.h | 29 ++++++++++++++++++++++++++++ include/spdlog/sinks/file_sinks.h | 5 +++-- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 29f0eca9..04da10a6 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -100,10 +100,17 @@ enum class async_overflow_policy // // Log exception // +namespace details { namespace os { + std::string errno_str(int err_num); +}} class spdlog_ex : public std::exception { public: spdlog_ex(const std::string& msg) :_msg(msg) {} + spdlog_ex(const std::string& msg, int last_errno) + { + _msg = msg + ": " + details::os::errno_str(last_errno); + } const char* what() const SPDLOG_NOEXCEPT override { return _msg.c_str(); diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index a465c4d9..4c5fe237 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -17,6 +17,7 @@ #include #include #include +#include namespace spdlog { @@ -58,7 +59,7 @@ public: std::this_thread::sleep_for(std::chrono::milliseconds(open_interval)); } - throw spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing"); + throw spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno); } void reopen(bool truncate) @@ -89,7 +90,7 @@ public: size_t msg_size = msg.formatted.size(); auto data = msg.formatted.data(); if (std::fwrite(data, 1, msg_size, _fd) != msg_size) - throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename)); + throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno); if (_force_flush) std::fflush(_fd); @@ -102,15 +103,15 @@ public: auto pos = ftell(_fd); if (fseek(_fd, 0, SEEK_END) != 0) - throw spdlog_ex("fseek failed on file " + os::filename_to_str(_filename)); + throw spdlog_ex("fseek failed on file " + os::filename_to_str(_filename), errno); auto file_size = ftell(_fd); if(fseek(_fd, pos, SEEK_SET) !=0) - throw spdlog_ex("fseek failed on file " + os::filename_to_str(_filename)); + throw spdlog_ex("fseek failed on file " + os::filename_to_str(_filename), errno); if (file_size == -1) - throw spdlog_ex("ftell failed on file " + os::filename_to_str(_filename)); + throw spdlog_ex("ftell failed on file " + os::filename_to_str(_filename), errno); return file_size; } diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index fda4d0a0..ff9d35ff 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -10,6 +10,7 @@ #include #include #include +#include #ifdef _WIN32 @@ -27,12 +28,16 @@ #endif #elif __linux__ + #include //Use gettid() syscall under linux to get thread id #include #include #include + #else + #include + #endif namespace spdlog @@ -246,6 +251,30 @@ 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]; + constexpr auto buf_size = sizeof(buf); + +#ifdef _WIN32 + if(strerror_s(buf, buf_size, err_num) == 0) + return std::string(buf); + else + return "Unkown error"; + +#elif (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE // posix version + if (strerror_r(err_num, buf, buf_size) == 0) + return std::string(buf); + else + return "Unkown error"; + +#else // gnu version (might not use the given buf, so its retval pointer must be used) + return std::string(strerror_r(err_num, buf, buf_size)); +#endif +} + } //os } //details } //spdlog diff --git a/include/spdlog/sinks/file_sinks.h b/include/spdlog/sinks/file_sinks.h index 14b3cbff..bdc6ec9f 100644 --- a/include/spdlog/sinks/file_sinks.h +++ b/include/spdlog/sinks/file_sinks.h @@ -16,6 +16,7 @@ #include #include #include +#include namespace spdlog { @@ -119,12 +120,12 @@ private: { if (details::os::remove(target) != 0) { - throw spdlog_ex("rotating_file_sink: failed removing " + filename_to_str(target)); + throw spdlog_ex("rotating_file_sink: failed removing " + filename_to_str(target), errno); } } if (details::file_helper::file_exists(src) && details::os::rename(src, target)) { - throw spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target)); + throw spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno); } } _file_helper.reopen(true);