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
#include <exception>
#include <memory>
#include <type_traits>
#include <errno.h>
#include <string.h>
@ -40,8 +36,14 @@ void error_code(result_code code) noexcept {
* https://man7.org/linux/man-pages/man3/strerror_l.3.html
*/
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 msg_buf;
}
} // namespace sys
LIBIMP_NAMESPACE_END_

View File

@ -44,7 +44,7 @@ void error_code(result_code code) noexcept {
std::string error_msg(result_code code) noexcept {
LIBIMP_LOG_();
try {
DWORD err = code ? ERROR_SUCCESS : (DWORD)code.value();
DWORD err = (DWORD)code.value();
LPTSTR lpErrText = NULL;
if (::FormatMessage(
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);
}
#else
imp::sys::error_msg({false, 1234}, "Unknown error nnn");
#endif
}