Improve error_code (new formatting constructor)

Minor cleanup in formatting utilities.
This commit is contained in:
Nekotekina 2021-05-19 10:12:57 +03:00
parent 16620f6835
commit 1d0f6eebdc
5 changed files with 22 additions and 27 deletions

View File

@ -248,15 +248,6 @@ namespace fmt
} }
}; };
template <typename... Args>
FORCE_INLINE SAFE_BUFFERS(const fmt_type_info*) get_type_info()
{
// Constantly initialized null-terminated list of type-specific information
static constexpr fmt_type_info result[sizeof...(Args) + 1]{fmt_type_info::make<fmt_unveil_t<Args>>()...};
return result;
}
template <typename... Args> template <typename... Args>
constexpr const fmt_type_info type_info_v[sizeof...(Args) + 1]{fmt_type_info::make<fmt_unveil_t<Args>>()...}; constexpr const fmt_type_info type_info_v[sizeof...(Args) + 1]{fmt_type_info::make<fmt_unveil_t<Args>>()...};
@ -267,8 +258,7 @@ namespace fmt
template <typename CharT, usz N, typename... Args> template <typename CharT, usz N, typename... Args>
FORCE_INLINE SAFE_BUFFERS(void) append(std::string& out, const CharT(&fmt)[N], const Args&... args) FORCE_INLINE SAFE_BUFFERS(void) append(std::string& out, const CharT(&fmt)[N], const Args&... args)
{ {
static constexpr fmt_type_info type_list[sizeof...(Args) + 1]{fmt_type_info::make<fmt_unveil_t<Args>>()...}; raw_append(out, reinterpret_cast<const char*>(fmt), type_info_v<Args...>, fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...});
raw_append(out, reinterpret_cast<const char*>(fmt), type_list, fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...});
} }
// Formatting function // Formatting function
@ -293,8 +283,7 @@ namespace fmt
const char* file = __builtin_FILE(), const char* file = __builtin_FILE(),
const char* func = __builtin_FUNCTION()) const char* func = __builtin_FUNCTION())
{ {
static constexpr fmt_type_info type_list[sizeof...(Args) + 1]{fmt_type_info::make<fmt_unveil_t<Args>>()...}; raw_throw_exception({line, col, file, func}, reinterpret_cast<const char*>(fmt), type_info_v<Args...>, fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...});
raw_throw_exception({line, col, file, func}, reinterpret_cast<const char*>(fmt), type_list, fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...});
} }
#ifndef _MSC_VER #ifndef _MSC_VER

View File

@ -12,20 +12,26 @@ public:
error_code() = default; error_code() = default;
// Implementation must be provided independently // Implementation must be provided independently
static s32 error_report(const fmt_type_info* sup, u64 arg, const fmt_type_info* sup2, u64 arg2); static s32 error_report(s32 result, const char* fmt, const fmt_type_info* sup, const u64* args);
// Common constructor // Common constructor
template<typename ET> template<typename ET>
error_code(const ET& value) error_code(const ET& value)
: value(static_cast<s32>(value)) : value(error_report(static_cast<s32>(value), " : %s", fmt::type_info_v<ET>, fmt_args_t<ET>{fmt_unveil<ET>::get(value)}))
{ {
this->value = error_report(fmt::get_type_info<fmt_unveil_t<ET>>(), fmt_unveil<ET>::get(value), nullptr, 0);
} }
// Error constructor (2 args) // Error constructor (2 args)
template<typename ET, typename T2> template<typename ET, typename T>
error_code(const ET& value, const T2& value2) error_code(const ET& value, const T& arg)
: value(error_report(fmt::get_type_info<fmt_unveil_t<ET>>(), fmt_unveil<ET>::get(value), fmt::get_type_info<fmt_unveil_t<T2>>(), fmt_unveil<T2>::get(value2))) : value(error_report(static_cast<s32>(value), " : %s, %s", fmt::type_info_v<ET, T>, fmt_args_t<ET, T>{fmt_unveil<ET>::get(value), fmt_unveil<T>::get(arg)}))
{
}
// Formatting constructor (error, format string, variadic list)
template <typename ET, typename... Args> requires (sizeof...(Args) > 0)
error_code(const ET& value, const const_str& fmt, const Args&... args)
: value(error_report(static_cast<s32>(value), fmt, fmt::type_info_v<Args...>, fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...}))
{ {
} }

View File

@ -574,7 +574,7 @@ static NEVER_INLINE error_code savedata_op(ppu_thread& ppu, u32 operation, u32 v
if (const auto ecode = savedata_check_args(operation, version, dirName, errDialog, setList, setBuf, funcList, funcFixed, funcStat, if (const auto ecode = savedata_check_args(operation, version, dirName, errDialog, setList, setBuf, funcList, funcFixed, funcStat,
funcFile, container, unk_op_flags, userdata, userId, funcDone)) funcFile, container, unk_op_flags, userdata, userId, funcDone))
{ {
return {CELL_SAVEDATA_ERROR_PARAM, std::to_string(ecode)}; return {CELL_SAVEDATA_ERROR_PARAM, " (error %d)", ecode};
} }
std::unique_lock lock(g_fxo->get<savedata_manager>().mutex, std::try_to_lock); std::unique_lock lock(g_fxo->get<savedata_manager>().mutex, std::try_to_lock);

View File

@ -1640,14 +1640,14 @@ std::string Emulator::GetFormattedTitle(double fps) const
return rpcs3::get_formatted_title(title_data); return rpcs3::get_formatted_title(title_data);
} }
s32 error_code::error_report(const fmt_type_info* sup, u64 arg, const fmt_type_info* sup2, u64 arg2) s32 error_code::error_report(s32 result, const char* fmt, const fmt_type_info* sup, const u64* args)
{ {
static thread_local std::unordered_map<std::string, usz> g_tls_error_stats; static thread_local std::unordered_map<std::string, usz> g_tls_error_stats;
static thread_local std::string g_tls_error_str; static thread_local std::string g_tls_error_str;
if (!sup) if (!sup && !args)
{ {
if (!sup2) if (!fmt)
{ {
// Report and clean error state // Report and clean error state
for (auto&& pair : g_tls_error_stats) for (auto&& pair : g_tls_error_stats)
@ -1681,7 +1681,8 @@ s32 error_code::error_report(const fmt_type_info* sup, u64 arg, const fmt_type_i
// Format log message (use preallocated buffer) // Format log message (use preallocated buffer)
g_tls_error_str.clear(); g_tls_error_str.clear();
fmt::append(g_tls_error_str, "'%s' failed with 0x%08x%s%s%s%s", func, arg, sup ? " : " : "", std::make_pair(sup, arg), sup2 ? ", " : "", std::make_pair(sup2, arg2)); fmt::append(g_tls_error_str, "'%s' failed with 0x%08x", func, result);
fmt::raw_append(g_tls_error_str, fmt, sup, args);
// Update stats and check log threshold // Update stats and check log threshold
const auto stat = ++g_tls_error_stats[g_tls_error_str]; const auto stat = ++g_tls_error_stats[g_tls_error_str];
@ -1691,7 +1692,7 @@ s32 error_code::error_report(const fmt_type_info* sup, u64 arg, const fmt_type_i
channel->error("%s [%u]", g_tls_error_str, stat); channel->error("%s [%u]", g_tls_error_str, stat);
} }
return static_cast<s32>(arg); return result;
} }
void Emulator::ConfigurePPUCache() const void Emulator::ConfigurePPUCache() const

View File

@ -91,8 +91,7 @@ namespace logs
{\ {\
if constexpr (sizeof...(Args) > 0)\ if constexpr (sizeof...(Args) > 0)\
{\ {\
static constexpr fmt_type_info type_list[sizeof...(Args) + 1]{fmt_type_info::make<fmt_unveil_t<Args>>()...};\ msg_##_sev.broadcast(fmt, fmt::type_info_v<Args...>, u64{fmt_unveil<Args>::get(args)}...);\
msg_##_sev.broadcast(fmt, type_list, u64{fmt_unveil<Args>::get(args)}...);\
}\ }\
else\ else\
{\ {\