From 457fcbaf5e565ba851cbc91202b18d2319fb837c Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Thu, 2 Jun 2022 13:32:30 -0700 Subject: [PATCH] LibusbUtils: Create ErrorWrap --- Source/Core/Core/LibusbUtils.cpp | 20 +++++++++++++++++++- Source/Core/Core/LibusbUtils.h | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Source/Core/Core/LibusbUtils.cpp b/Source/Core/Core/LibusbUtils.cpp index 8ab2f40a55..dadad60a2d 100644 --- a/Source/Core/Core/LibusbUtils.cpp +++ b/Source/Core/Core/LibusbUtils.cpp @@ -23,7 +23,7 @@ public: Impl() { const int ret = libusb_init(&m_context); - ASSERT_MSG(IOS_USB, ret == LIBUSB_SUCCESS, "Failed to init libusb: {}", libusb_error_name(ret)); + ASSERT_MSG(IOS_USB, ret == LIBUSB_SUCCESS, "Failed to init libusb: {}", ErrorWrap(ret)); if (ret != LIBUSB_SUCCESS) return; @@ -118,4 +118,22 @@ ConfigDescriptor MakeConfigDescriptor(libusb_device* device, u8 config_num) #endif return {nullptr, [](auto) {}}; } + +const char* ErrorWrap::GetName() const +{ +#if defined(__LIBUSB__) + return libusb_error_name(m_error); +#else + return "__LIBUSB__ not defined"; +#endif +} + +const char* ErrorWrap::GetStrError() const +{ +#if defined(__LIBUSB__) + return libusb_strerror(static_cast(m_error)); +#else + return "__LIBUSB__ not defined"; +#endif +} } // namespace LibusbUtils diff --git a/Source/Core/Core/LibusbUtils.h b/Source/Core/Core/LibusbUtils.h index 3e15ed0155..aaa77de74b 100644 --- a/Source/Core/Core/LibusbUtils.h +++ b/Source/Core/Core/LibusbUtils.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include @@ -39,4 +40,28 @@ private: using ConfigDescriptor = UniquePtr; ConfigDescriptor MakeConfigDescriptor(libusb_device* device, u8 config_num = 0); + +// Wrapper for libusb_error to be used with fmt. Note that we can't create a fmt::formatter +// directly for libusb_error as it is a plain enum and most libusb functions actually return an +// int instead of a libusb_error. +struct ErrorWrap +{ + constexpr explicit ErrorWrap(int error) : m_error(error) {} + const int m_error; + + const char* GetStrError() const; + const char* GetName() const; +}; } // namespace LibusbUtils + +template <> +struct fmt::formatter +{ + constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); } + template + auto format(const LibusbUtils::ErrorWrap& wrap, FormatContext& ctx) const + { + return fmt::format_to(ctx.out(), "{} ({}: {})", wrap.GetStrError(), wrap.m_error, + wrap.GetName()); + } +};