From 6329d6c66b76f514eecf468ad69f8e17e405ad39 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 14 Aug 2022 13:25:21 +0800 Subject: [PATCH] add: [system] obtained the string describing error number in Linux --- src/libimp/platform/posix/system.h | 7 ++++++- test/test_imp_system.cpp | 5 +++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/libimp/platform/posix/system.h b/src/libimp/platform/posix/system.h index 1529643..21cc55c 100644 --- a/src/libimp/platform/posix/system.h +++ b/src/libimp/platform/posix/system.h @@ -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 diff --git a/test/test_imp_system.cpp b/test/test_imp_system.cpp index 711e3ea..d7ed936 100644 --- a/test/test_imp_system.cpp +++ b/test/test_imp_system.cpp @@ -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 } \ No newline at end of file