From edae13d9e491fbc3870a45f907b1efdec5cc944b Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 5 Nov 2023 13:32:25 +1000 Subject: [PATCH] CDROM: Log command parameters --- src/common/log.cpp | 16 +++++++++++++++- src/common/log.h | 22 +++++++++++++++++++++- src/common/small_string.cpp | 12 ++++++++++++ src/common/small_string.h | 3 +++ src/core/cdrom.cpp | 17 ++++++++++++----- src/core/settings.cpp | 2 +- 6 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/common/log.cpp b/src/common/log.cpp index 4112de451..f830f05fb 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -445,6 +445,20 @@ void Log::SetFileOutputParams(bool enabled, const char* filename, bool timestamp s_file_output_timestamp = timestamps; } +LOGLEVEL Log::GetLogLevel() +{ + return s_log_level; +} + +bool Log::IsLogVisible(LOGLEVEL level, const char* channelName) +{ + if (level > s_log_level) + return false; + + std::unique_lock lock(s_callback_mutex); + return FilterTest(level, channelName, lock); +} + void Log::SetLogLevel(LOGLEVEL level) { std::unique_lock lock(s_callback_mutex); @@ -452,7 +466,7 @@ void Log::SetLogLevel(LOGLEVEL level) s_log_level = level; } -void Log::SetLogfilter(std::string_view filter) +void Log::SetLogFilter(std::string_view filter) { std::unique_lock lock(s_callback_mutex); if (s_log_filter != filter) diff --git a/src/common/log.h b/src/common/log.h index 5daf909a6..f693c1368 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -52,11 +52,17 @@ void SetDebugOutputParams(bool enabled); // adds a file output void SetFileOutputParams(bool enabled, const char* filename, bool timestamps = true); +// Returns the current global filtering level. +LOGLEVEL GetLogLevel(); + +// Returns true if log messages for the specified log level/filter would not be filtered (and visible). +bool IsLogVisible(LOGLEVEL level, const char* channelName); + // Sets global filtering level, messages below this level won't be sent to any of the logging sinks. void SetLogLevel(LOGLEVEL level); // Sets global filter, any messages from these channels won't be sent to any of the logging sinks. -void SetLogfilter(std::string_view filter); +void SetLogFilter(std::string_view filter); // writes a message to the log void Write(const char* channelName, const char* functionName, LOGLEVEL level, std::string_view message); @@ -98,6 +104,14 @@ ALWAYS_INLINE static void WriteFmt(const char* channelName, const char* function #define Log_ProfilePrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_PROFILE, __VA_ARGS__) #define Log_ProfileFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_PROFILE, __VA_ARGS__) +#define Log_ErrorVisible() Log::IsLogVisible(LOGLEVEL_ERROR, ___LogChannel___) +#define Log_WarningVisible() Log::IsLogVisible(LOGLEVEL_WARNING, ___LogChannel___) +#define Log_PerfVisible() Log::IsLogVisible(LOGLEVEL_PERF, ___LogChannel___) +#define Log_InfoVisible() Log::IsLogVisible(LOGLEVEL_INFO, ___LogChannel___) +#define Log_VerboseVisible() Log::IsLogVisible(LOGLEVEL_VERBOSE, ___LogChannel___) +#define Log_DevVisible() Log::IsLogVisible(LOGLEVEL_DEV, ___LogChannel___) +#define Log_ProfileVisible() Log::IsLogVisible(LOGLEVEL_PROFILE, ___LogChannel___) + #ifdef _DEBUG #define Log_DebugPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_DEBUG, msg) #define Log_DebugPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_DEBUG, __VA_ARGS__) @@ -105,6 +119,9 @@ ALWAYS_INLINE static void WriteFmt(const char* channelName, const char* function #define Log_TracePrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_TRACE, msg) #define Log_TracePrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_TRACE, __VA_ARGS__) #define Log_TraceFmt(...) Log::WriteFmt(___LogChannel___, __func__, LOGLEVEL_TRACE, __VA_ARGS__) + +#define Log_DebugVisible() Log::IsLogVisible(LOGLEVEL_DEBUG, ___LogChannel___) +#define Log_TraceVisible() Log::IsLogVisible(LOGLEVEL_TRACE, ___LogChannel___) #else #define Log_DebugPrint(msg) \ do \ @@ -130,4 +147,7 @@ ALWAYS_INLINE static void WriteFmt(const char* channelName, const char* function do \ { \ } while (0) + +#define Log_DebugVisible() false +#define Log_TraceVisible() false #endif diff --git a/src/common/small_string.cpp b/src/common/small_string.cpp index 8273751e4..bc9338fb4 100644 --- a/src/common/small_string.cpp +++ b/src/common/small_string.cpp @@ -172,6 +172,18 @@ void SmallStringBase::append(const char* str, u32 length) m_buffer[m_length] = 0; } +void SmallStringBase::append_hex(const void* data, size_t len) +{ + if (len == 0) + return; + + make_room_for(static_cast(len) * 4); + const u8* bytes = static_cast(data); + append_fmt("{:02X}", bytes[0]); + for (size_t i = 1; i < len; i++) + append_fmt(", {:02X}", bytes[i]); +} + void SmallStringBase::prepend(const char* str, u32 length) { if (length == 0) diff --git a/src/common/small_string.h b/src/common/small_string.h index 15ae54ddc..6f6b03282 100644 --- a/src/common/small_string.h +++ b/src/common/small_string.h @@ -64,6 +64,9 @@ public: template void append_fmt(fmt::format_string fmt, T&&... args); + // append hex string + void append_hex(const void* data, size_t len); + // append a single character to this string void prepend(char c); diff --git a/src/core/cdrom.cpp b/src/core/cdrom.cpp index 8c599b624..11f3e7a51 100644 --- a/src/core/cdrom.cpp +++ b/src/core/cdrom.cpp @@ -1397,12 +1397,19 @@ void CDROM::EndCommand() void CDROM::ExecuteCommand(void*, TickCount ticks, TickCount ticks_late) { const CommandInfo& ci = s_command_info[static_cast(s_command)]; - Log_DevPrintf("CDROM executing command 0x%02X (%s), stat = 0x%02X", static_cast(s_command), ci.name, - s_secondary_status.bits); - if (s_param_fifo.GetSize() < ci.expected_parameters) + if (Log_DevVisible()) [[unlikely]] { - Log_WarningPrintf("Too few parameters for command 0x%02X (%s), expecting %u got %u", static_cast(s_command), - ci.name, ci.expected_parameters, s_param_fifo.GetSize()); + SmallString params; + for (u32 i = 0; i < s_param_fifo.GetSize(); i++) + params.append_fmt("{}0x{:02X}", (i == 0) ? "" : ", ", s_param_fifo.Peek(i)); + Log_DevFmt("CDROM executing command 0x{:02X} ({}), stat = 0x{:02X}, params = [{}]", static_cast(s_command), + ci.name, s_secondary_status.bits, params); + } + + if (s_param_fifo.GetSize() < ci.expected_parameters) [[unlikely]] + { + Log_WarningFmt("Too few parameters for command 0x{:02X} ({}), expecting {} got {}", static_cast(s_command), + ci.name, ci.expected_parameters, s_param_fifo.GetSize()); SendErrorResponse(STAT_ERROR, ERROR_REASON_INCORRECT_NUMBER_OF_PARAMETERS); EndCommand(); return; diff --git a/src/core/settings.cpp b/src/core/settings.cpp index f2fc31797..8aafd57d2 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -704,7 +704,7 @@ void Settings::FixIncompatibleSettings(bool display_osd_messages) void Settings::UpdateLogSettings() { Log::SetLogLevel(log_level); - Log::SetLogfilter(log_filter); + Log::SetLogFilter(log_filter); Log::SetConsoleOutputParams(log_to_console, log_timestamps); Log::SetDebugOutputParams(log_to_debug);