add: [system] obtained the string describing error number in Linux

This commit is contained in:
mutouyun 2022-08-14 13:25:21 +08:00
parent c51395c033
commit 6329d6c66b
2 changed files with 9 additions and 3 deletions

View File

@ -34,15 +34,20 @@ void error_code(result_code code) noexcept {
/**
* @brief Gets a text description of the system error
* https://man7.org/linux/man-pages/man3/strerror_l.3.html
* https://manpages.ubuntu.com/manpages/xenial/en/man3/strerror.3.html
*/
std::string error_msg(result_code code) noexcept {
LIBIMP_LOG_();
char msg_buf[256] {};
#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
LIBIMP_LOG_();
if (::strerror_r((int)code.value(), msg_buf, sizeof(msg_buf)) != 0) {
log.error("strerror_r fails. return = {}", error_code());
return {};
}
return msg_buf;
#else
return ::strerror_r((int)code.value(), msg_buf, sizeof(msg_buf));
#endif
}
} // namespace sys

View File

@ -52,7 +52,8 @@ TEST(system, error_msg) {
EXPECT_EQ(imp::sys::error_msg({false, ERROR_INVALID_HANDLE}), s_txt);
}
#else
EXPECT_EQ(imp::sys::error_msg({}), "");
EXPECT_EQ(imp::sys::error_msg({false, EINVAL}), "");
EXPECT_EQ(imp::sys::error_msg({false, 1234}), "Unknown error 1234");
EXPECT_EQ(imp::sys::error_msg({}), "Success");
EXPECT_EQ(imp::sys::error_msg({false, EINVAL}), "Invalid argument");
#endif
}