diff --git a/bee/error.cpp b/bee/error.cpp index 7242b83..3402a2e 100644 --- a/bee/error.cpp +++ b/bee/error.cpp @@ -3,51 +3,49 @@ #if defined(_WIN32) # include -# include +# include #else # include #endif namespace bee::error { #if defined(_WIN32) - struct errormsg : public std::wstring_view { - using mybase = std::wstring_view; - errormsg(wchar_t* str) noexcept - : mybase(str) {} - ~errormsg() noexcept { - ::LocalFree(reinterpret_cast(const_cast(mybase::data()))); - } - }; - - static std::wstring error_message(int error_code) { - wchar_t* message = 0; - const unsigned long result = ::FormatMessageW( - FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - error_code, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - reinterpret_cast(&message), - 0, - NULL - ); - if ((result == 0) || !message) { - return std::format(L"Unable to get an error message for error code: {}.", error_code); - } - errormsg str(message); - while (str.size() && ((str.back() == L'\n') || (str.back() == L'\r'))) { - str.remove_suffix(1); - } - return std::wstring(str); - } - - class winCategory : public std::error_category { + class windows_category : public std::error_category { public: + struct errormsg : public std::wstring_view { + using mybase = std::wstring_view; + errormsg(wchar_t* str) noexcept + : mybase(str) {} + ~errormsg() noexcept { + ::LocalFree(reinterpret_cast(const_cast(mybase::data()))); + } + }; const char* name() const noexcept override { return "Windows"; } std::string message(int error_code) const override { - return wtf8::w2u(error_message(error_code)); + wchar_t* message = 0; + const unsigned long result = ::FormatMessageW( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + error_code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast(&message), + 0, + NULL + ); + if ((result == 0) || !message) { + return std::format("Unable to get an error message for error code: {}.", error_code); + } + errormsg wstr(message); + while (wstr.size() && ((wstr.back() == L'\n') || (wstr.back() == L'\r'))) { + wstr.remove_suffix(1); + } + size_t len = wtf8_from_utf16_length(wstr.data(), wstr.size()); + std::string ret(len, '\0'); + wtf8_from_utf16(wstr.data(), wstr.size(), ret.data(), len); + return ret; } std::error_condition default_error_condition(int error_code) const noexcept override { const std::error_condition cond = std::system_category().default_error_condition(error_code); @@ -57,8 +55,6 @@ namespace bee::error { return std::error_condition(error_code, *this); } }; - - static winCategory g_windows_category; #endif std::string errmsg(std::string_view msg, std::error_code ec) noexcept { @@ -75,7 +71,7 @@ namespace bee::error { std::string sys_errmsg(std::string_view msg) noexcept { #if defined(_WIN32) - return errmsg(msg, std::error_code(::WSAGetLastError(), g_windows_category)); + return errmsg(msg, std::error_code(::GetLastError(), windows_category())); #else return errmsg(msg, std::error_code(errno, std::system_category())); #endif @@ -83,7 +79,7 @@ namespace bee::error { std::string net_errmsg(std::string_view msg, int err) noexcept { #if defined(_WIN32) - return errmsg(msg, std::error_code(err, g_windows_category)); + return errmsg(msg, std::error_code(err, windows_category())); #else return errmsg(msg, std::error_code(err, std::system_category())); #endif @@ -91,7 +87,7 @@ namespace bee::error { std::string net_errmsg(std::string_view msg) noexcept { #if defined(_WIN32) - return net_errmsg(msg, ::GetLastError()); + return net_errmsg(msg, ::WSAGetLastError()); #else return net_errmsg(msg, errno); #endif