diff --git a/include/libimp/system.h b/include/libimp/system.h index 208f40d..7264682 100644 --- a/include/libimp/system.h +++ b/include/libimp/system.h @@ -24,6 +24,11 @@ LIBIMP_EXPORT void error_code(result_code) noexcept; /** * @brief Gets a text description of the system error */ +LIBIMP_EXPORT std::string error_str(result_code) noexcept; + +/** + * @brief @brief A text description string with an error number attached + */ LIBIMP_EXPORT std::string error_msg(result_code) noexcept; } // namespace sys diff --git a/src/libimp/platform/posix/system.h b/src/libimp/platform/posix/system.h index 21cc55c..b05d76e 100644 --- a/src/libimp/platform/posix/system.h +++ b/src/libimp/platform/posix/system.h @@ -36,7 +36,7 @@ void error_code(result_code code) noexcept { * 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 { +std::string error_str(result_code code) noexcept { char msg_buf[256] {}; #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) LIBIMP_LOG_(); diff --git a/src/libimp/platform/win/system.h b/src/libimp/platform/win/system.h index 5820e03..b50f2bc 100644 --- a/src/libimp/platform/win/system.h +++ b/src/libimp/platform/win/system.h @@ -41,7 +41,7 @@ void error_code(result_code code) noexcept { * @brief Gets a text description of the system error * https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage */ -std::string error_msg(result_code code) noexcept { +std::string error_str(result_code code) noexcept { LIBIMP_LOG_(); try { DWORD err = (DWORD)code.value(); diff --git a/src/libimp/system.cpp b/src/libimp/system.cpp index 54a178f..94e56bc 100644 --- a/src/libimp/system.cpp +++ b/src/libimp/system.cpp @@ -3,4 +3,20 @@ #include "libimp/platform/win/system.h" #else #include "libimp/platform/posix/system.h" -#endif \ No newline at end of file +#endif + +#include "fmt/format.h" + +LIBIMP_NAMESPACE_BEG_ +namespace sys { + +std::string error_msg(result_code code) noexcept { + try { + return ::fmt::format("[{}, \"{}\"]", code.value(), error_str(code)); + } catch (...) { + return error_str(code); + } +} + +} // namespace sys +LIBIMP_NAMESPACE_END_ diff --git a/test/test_imp_system.cpp b/test/test_imp_system.cpp index d7ed936..73c7568 100644 --- a/test/test_imp_system.cpp +++ b/test/test_imp_system.cpp @@ -14,7 +14,7 @@ #endif TEST(system, error_code) { - std::cout << fmt::format("{}\n", imp::sys::error_code()); + std::cout << fmt::format("{}\n", imp::sys::error_msg(imp::sys::error_code())); imp::sys::error_code({false, 111}); auto err = imp::sys::error_code(); @@ -25,7 +25,7 @@ TEST(system, error_code) { EXPECT_TRUE(imp::sys::error_code()); } -TEST(system, error_msg) { +TEST(system, error_str) { #if defined(LIBIMP_OS_WIN) std::u16string u16_ok, u16_err; LANGID lId = ::GetSystemDefaultLangID(); @@ -44,16 +44,16 @@ TEST(system, error_msg) { { std::string s_txt; imp::cvt_sstr(u16_ok, s_txt); - EXPECT_EQ(imp::sys::error_msg({}), s_txt); + EXPECT_EQ(imp::sys::error_str({}), s_txt); } { std::string s_txt; imp::cvt_sstr(u16_err, s_txt); - EXPECT_EQ(imp::sys::error_msg({false, ERROR_INVALID_HANDLE}), s_txt); + EXPECT_EQ(imp::sys::error_str({false, ERROR_INVALID_HANDLE}), s_txt); } #else - 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"); + EXPECT_EQ(imp::sys::error_str({false, 1234}), "Unknown error 1234"); + EXPECT_EQ(imp::sys::error_str({}), "Success"); + EXPECT_EQ(imp::sys::error_str({false, EINVAL}), "Invalid argument"); #endif } \ No newline at end of file