Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Dec 11, 2024
1 parent cf4d6c7 commit 784c7c9
Showing 1 changed file with 34 additions and 38 deletions.
72 changes: 34 additions & 38 deletions bee/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,49 @@

#if defined(_WIN32)
# include <Windows.h>
# include <bee/win/wtf8.h>
# include <bee/win/cwtf8.h>
#else
# include <errno.h>
#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<HLOCAL>(const_cast<wchar_t*>(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<LPWSTR>(&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<HLOCAL>(const_cast<wchar_t*>(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<LPWSTR>(&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);
Expand All @@ -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 {
Expand All @@ -75,23 +71,23 @@ 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
}

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
}

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
Expand Down

0 comments on commit 784c7c9

Please sign in to comment.