Logging: Add compile-time format checks

This commit is contained in:
SSimco 2026-04-20 14:08:31 +03:00
parent 02383542b2
commit 3fcc88567c
2 changed files with 18 additions and 30 deletions

View File

@ -170,7 +170,7 @@ void padscoreExport_WPADRead(PPCInterpreter_t* hCPU)
{
ppcDefineParamU32(channel, 0);
ppcDefineParamPtr(wpadStatus, WPADStatus_t, 1);
cemuLog_log(LogType::InputAPI, "WPADRead({}, {:x})", channel, fmt::ptr(wpadStatus));
cemuLog_log(LogType::InputAPI, "WPADRead({}, {:x})", channel, memory_getVirtualOffsetFromPointer(wpadStatus));
if (channel < InputManager::kMaxWPADControllers)
{
@ -224,7 +224,7 @@ void padscoreExport_WPADGetInfo(PPCInterpreter_t* hCPU)
{
ppcDefineParamU32(channel, 0);
ppcDefineParamStructPtr(wpadInfo, WPADInfo_t, 1);
cemuLog_log(LogType::InputAPI, "WPADGetInfo({}, 0x{:08x})", channel, fmt::ptr(wpadInfo));
cemuLog_log(LogType::InputAPI, "WPADGetInfo({}, 0x{:08x})", channel, memory_getVirtualOffsetFromPointer(wpadInfo));
if (channel < InputManager::kMaxWPADControllers)
{

View File

@ -78,43 +78,22 @@ bool cemuLog_log(LogType type, std::string_view text);
bool cemuLog_log(LogType type, std::u8string_view text);
void cemuLog_waitForFlush(); // wait until all log lines are written
template<typename T, typename ... TArgs>
bool cemuLog_log(LogType type, std::basic_string<T> formatStr, TArgs&&... args)
template<typename... TArgs>
bool cemuLog_log(LogType type, fmt::format_string<TArgs...> formatStr, TArgs&&... args)
{
if (!cemuLog_isLoggingEnabled(type))
return false;
if constexpr (sizeof...(TArgs) == 0)
{
cemuLog_log(type, std::basic_string_view<T>(formatStr.data(), formatStr.size()));
return true;
}
else
{
const auto format_view = fmt::basic_string_view<T>(formatStr);
#if FMT_VERSION >= 110000
const auto text = fmt::vformat(format_view, fmt::make_format_args<fmt::buffered_context<T>>(args...));
#else
const auto text = fmt::vformat(format_view, fmt::make_format_args<fmt::buffer_context<T>>(args...));
#endif
cemuLog_log(type, std::basic_string_view(text.data(), text.size()));
}
return true;
}
template<typename T, typename ... TArgs>
bool cemuLog_log(LogType type, const T* format, TArgs&&... args)
{
if (!cemuLog_isLoggingEnabled(type))
return false;
auto format_str = std::basic_string<T>(format);
return cemuLog_log(type, format_str, std::forward<TArgs>(args)...);
cemuLog_log(type, fmt::format(formatStr, std::forward<TArgs>(args)...));
return true;
}
#define cemuLog_logOnce(...) { static bool _not_first_call = false; if (!_not_first_call) { _not_first_call = true; cemuLog_log(__VA_ARGS__); } }
// same as cemuLog_log, but only outputs in debug mode
template<typename TFmt, typename ... TArgs>
bool cemuLog_logDebug(LogType type, TFmt format, TArgs&&... args)
template<typename ... TArgs>
bool cemuLog_logDebug(LogType type, fmt::format_string<TArgs...> format, TArgs&&... args)
{
#ifdef CEMU_DEBUG_ASSERT
return cemuLog_log(type, format, std::forward<TArgs>(args)...);
@ -123,6 +102,15 @@ bool cemuLog_logDebug(LogType type, TFmt format, TArgs&&... args)
#endif
}
inline bool cemuLog_logDebug(LogType type, std::string_view message)
{
#ifdef CEMU_DEBUG_ASSERT
return cemuLog_log(type, message);
#else
return false;
#endif
}
#define cemuLog_logDebugOnce(...) { static bool _not_first_call = false; if (!_not_first_call) { _not_first_call = true; cemuLog_logDebug(__VA_ARGS__); } }
// utility function for logging binary data as a hex dump