Commit c5c6baad authored by gabime's avatar gabime

Added errno description to sdlog exception strings

parent e5032c8d
...@@ -100,10 +100,17 @@ enum class async_overflow_policy ...@@ -100,10 +100,17 @@ enum class async_overflow_policy
// //
// Log exception // Log exception
// //
namespace details { namespace os {
std::string errno_str(int err_num);
}}
class spdlog_ex : public std::exception class spdlog_ex : public std::exception
{ {
public: public:
spdlog_ex(const std::string& msg) :_msg(msg) {} 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 const char* what() const SPDLOG_NOEXCEPT override
{ {
return _msg.c_str(); return _msg.c_str();
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <cstdio> #include <cstdio>
#include <string> #include <string>
#include <thread> #include <thread>
#include <cerrno>
namespace spdlog namespace spdlog
{ {
...@@ -58,7 +59,7 @@ public: ...@@ -58,7 +59,7 @@ public:
std::this_thread::sleep_for(std::chrono::milliseconds(open_interval)); 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) void reopen(bool truncate)
...@@ -89,7 +90,7 @@ public: ...@@ -89,7 +90,7 @@ public:
size_t msg_size = msg.formatted.size(); size_t msg_size = msg.formatted.size();
auto data = msg.formatted.data(); auto data = msg.formatted.data();
if (std::fwrite(data, 1, msg_size, _fd) != msg_size) 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) if (_force_flush)
std::fflush(_fd); std::fflush(_fd);
...@@ -102,15 +103,15 @@ public: ...@@ -102,15 +103,15 @@ public:
auto pos = ftell(_fd); auto pos = ftell(_fd);
if (fseek(_fd, 0, SEEK_END) != 0) 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); auto file_size = ftell(_fd);
if(fseek(_fd, pos, SEEK_SET) !=0) 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) 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; return file_size;
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <ctime> #include <ctime>
#include <functional> #include <functional>
#include <string> #include <string>
#include <string.h>
#ifdef _WIN32 #ifdef _WIN32
...@@ -27,12 +28,16 @@ ...@@ -27,12 +28,16 @@
#endif #endif
#elif __linux__ #elif __linux__
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id #include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <chrono> #include <chrono>
#else #else
#include <thread> #include <thread>
#endif #endif
namespace spdlog namespace spdlog
...@@ -246,6 +251,30 @@ inline std::string filename_to_str(const filename_t& filename) ...@@ -246,6 +251,30 @@ inline std::string filename_to_str(const filename_t& filename)
} }
#endif #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 } //os
} //details } //details
} //spdlog } //spdlog
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <ctime> #include <ctime>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <cerrno>
namespace spdlog namespace spdlog
{ {
...@@ -119,12 +120,12 @@ private: ...@@ -119,12 +120,12 @@ private:
{ {
if (details::os::remove(target) != 0) 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)) 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); _file_helper.reopen(true);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment