Added errno description to sdlog exception strings

This commit is contained in:
gabime
2016-07-15 17:48:02 +03:00
parent e5032c8db5
commit c5c6baad74
4 changed files with 45 additions and 7 deletions

View File

@@ -10,6 +10,7 @@
#include <ctime>
#include <functional>
#include <string>
#include <string.h>
#ifdef _WIN32
@@ -27,12 +28,16 @@
#endif
#elif __linux__
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
#include <sys/stat.h>
#include <unistd.h>
#include <chrono>
#else
#include <thread>
#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