add: [system] error_msg for linux

This commit is contained in:
mutouyun 2022-08-13 18:35:29 +08:00
parent bd198cb6e2
commit cbda80e4b8
3 changed files with 9 additions and 6 deletions

View File

@ -4,10 +4,6 @@
*/ */
#pragma once #pragma once
#include <exception>
#include <memory>
#include <type_traits>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
@ -40,7 +36,13 @@ void error_code(result_code code) noexcept {
* https://man7.org/linux/man-pages/man3/strerror_l.3.html * https://man7.org/linux/man-pages/man3/strerror_l.3.html
*/ */
std::string error_msg(result_code code) noexcept { std::string error_msg(result_code code) noexcept {
LIBIMP_LOG_();
char msg_buf[256] {};
if (::strerror_r((int)code.value(), msg_buf, sizeof(msg_buf)) != 0) {
log.error("strerror_r fails. return = {}", error_code());
return {}; return {};
}
return msg_buf;
} }
} // namespace sys } // namespace sys

View File

@ -44,7 +44,7 @@ void error_code(result_code code) noexcept {
std::string error_msg(result_code code) noexcept { std::string error_msg(result_code code) noexcept {
LIBIMP_LOG_(); LIBIMP_LOG_();
try { try {
DWORD err = code ? ERROR_SUCCESS : (DWORD)code.value(); DWORD err = (DWORD)code.value();
LPTSTR lpErrText = NULL; LPTSTR lpErrText = NULL;
if (::FormatMessage( if (::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ALLOCATE_BUFFER |

View File

@ -52,5 +52,6 @@ TEST(system, error_msg) {
EXPECT_EQ(imp::sys::error_msg({false, ERROR_INVALID_HANDLE}), s_txt); EXPECT_EQ(imp::sys::error_msg({false, ERROR_INVALID_HANDLE}), s_txt);
} }
#else #else
imp::sys::error_msg({false, 1234}, "Unknown error nnn");
#endif #endif
} }