mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
upd: [system] adjust the output format of the error message
This commit is contained in:
parent
6329d6c66b
commit
64a3ba24e7
@ -24,6 +24,11 @@ LIBIMP_EXPORT void error_code(result_code) noexcept;
|
|||||||
/**
|
/**
|
||||||
* @brief Gets a text description of the system error
|
* @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;
|
LIBIMP_EXPORT std::string error_msg(result_code) noexcept;
|
||||||
|
|
||||||
} // namespace sys
|
} // namespace sys
|
||||||
|
|||||||
@ -36,7 +36,7 @@ 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
|
||||||
* https://manpages.ubuntu.com/manpages/xenial/en/man3/strerror.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] {};
|
char msg_buf[256] {};
|
||||||
#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
|
#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
|
||||||
LIBIMP_LOG_();
|
LIBIMP_LOG_();
|
||||||
|
|||||||
@ -41,7 +41,7 @@ void error_code(result_code code) noexcept {
|
|||||||
* @brief Gets a text description of the system error
|
* @brief Gets a text description of the system error
|
||||||
* https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage
|
* 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_();
|
LIBIMP_LOG_();
|
||||||
try {
|
try {
|
||||||
DWORD err = (DWORD)code.value();
|
DWORD err = (DWORD)code.value();
|
||||||
|
|||||||
@ -4,3 +4,19 @@
|
|||||||
#else
|
#else
|
||||||
#include "libimp/platform/posix/system.h"
|
#include "libimp/platform/posix/system.h"
|
||||||
#endif
|
#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_
|
||||||
|
|||||||
@ -14,7 +14,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEST(system, error_code) {
|
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});
|
imp::sys::error_code({false, 111});
|
||||||
auto err = imp::sys::error_code();
|
auto err = imp::sys::error_code();
|
||||||
@ -25,7 +25,7 @@ TEST(system, error_code) {
|
|||||||
EXPECT_TRUE(imp::sys::error_code());
|
EXPECT_TRUE(imp::sys::error_code());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(system, error_msg) {
|
TEST(system, error_str) {
|
||||||
#if defined(LIBIMP_OS_WIN)
|
#if defined(LIBIMP_OS_WIN)
|
||||||
std::u16string u16_ok, u16_err;
|
std::u16string u16_ok, u16_err;
|
||||||
LANGID lId = ::GetSystemDefaultLangID();
|
LANGID lId = ::GetSystemDefaultLangID();
|
||||||
@ -44,16 +44,16 @@ TEST(system, error_msg) {
|
|||||||
{
|
{
|
||||||
std::string s_txt;
|
std::string s_txt;
|
||||||
imp::cvt_sstr(u16_ok, 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;
|
std::string s_txt;
|
||||||
imp::cvt_sstr(u16_err, 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
|
#else
|
||||||
EXPECT_EQ(imp::sys::error_msg({false, 1234}), "Unknown error 1234");
|
EXPECT_EQ(imp::sys::error_str({false, 1234}), "Unknown error 1234");
|
||||||
EXPECT_EQ(imp::sys::error_msg({}), "Success");
|
EXPECT_EQ(imp::sys::error_str({}), "Success");
|
||||||
EXPECT_EQ(imp::sys::error_msg({false, EINVAL}), "Invalid argument");
|
EXPECT_EQ(imp::sys::error_str({false, EINVAL}), "Invalid argument");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user