Merge pull request #10677 from Pokechu22/no-printf-log

Remove printf-style logging
This commit is contained in:
Mai M 2022-05-19 17:55:17 -04:00 committed by GitHub
commit a98d0367d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 79 additions and 134 deletions

View File

@ -96,58 +96,8 @@ void GenericLogFmt(LogLevel level, LogType type, const char* file, int line, con
"too many arguments?"); "too many arguments?");
GenericLogFmtImpl(level, type, file, line, format, fmt::make_format_args(args...)); GenericLogFmtImpl(level, type, file, line, format, fmt::make_format_args(args...));
} }
void GenericLog(LogLevel level, LogType type, const char* file, int line, const char* fmt, ...)
#ifdef __GNUC__
__attribute__((format(printf, 5, 6)))
#endif
;
void GenericLogV(LogLevel level, LogType type, const char* file, int line, const char* fmt,
va_list args);
} // namespace Common::Log } // namespace Common::Log
// Let the compiler optimize this out
#define GENERIC_LOG(t, v, ...) \
do \
{ \
if (v <= Common::Log::MAX_LOGLEVEL) \
Common::Log::GenericLog(v, t, __FILE__, __LINE__, __VA_ARGS__); \
} while (0)
#define ERROR_LOG(t, ...) \
do \
{ \
GENERIC_LOG(Common::Log::LogType::t, Common::Log::LogLevel::LERROR, __VA_ARGS__); \
} while (0)
#define WARN_LOG(t, ...) \
do \
{ \
GENERIC_LOG(Common::Log::LogType::t, Common::Log::LogLevel::LWARNING, __VA_ARGS__); \
} while (0)
#define NOTICE_LOG(t, ...) \
do \
{ \
GENERIC_LOG(Common::Log::LogType::t, Common::Log::LogLevel::LNOTICE, __VA_ARGS__); \
} while (0)
#define INFO_LOG(t, ...) \
do \
{ \
GENERIC_LOG(Common::Log::LogType::t, Common::Log::LogLevel::LINFO, __VA_ARGS__); \
} while (0)
#define DEBUG_LOG(t, ...) \
do \
{ \
GENERIC_LOG(Common::Log::LogType::t, Common::Log::LogLevel::LDEBUG, __VA_ARGS__); \
} while (0)
#define GENERIC_LOG_V(t, v, fmt, args) \
do \
{ \
if (v <= Common::Log::MAX_LOGLEVEL) \
Common::Log::GenericLogV(v, t, __FILE__, __LINE__, fmt, args); \
} while (0)
// fmtlib capable API // fmtlib capable API
#define GENERIC_LOG_FMT(t, v, format, ...) \ #define GENERIC_LOG_FMT(t, v, format, ...) \

View File

@ -62,40 +62,6 @@ private:
bool m_enable; bool m_enable;
}; };
void GenericLog(LogLevel level, LogType type, const char* file, int line, const char* fmt, ...)
{
auto* instance = LogManager::GetInstance();
if (instance == nullptr)
return;
if (!instance->IsEnabled(type, level))
return;
va_list args;
va_start(args, fmt);
char message[MAX_MSGLEN];
CharArrayFromFormatV(message, MAX_MSGLEN, fmt, args);
va_end(args);
instance->Log(level, type, file, line, message);
}
void GenericLogV(LogLevel level, LogType type, const char* file, int line, const char* fmt,
va_list args)
{
auto* instance = LogManager::GetInstance();
if (instance == nullptr)
return;
if (!instance->IsEnabled(type, level))
return;
char message[MAX_MSGLEN];
CharArrayFromFormatV(message, MAX_MSGLEN, fmt, args);
instance->Log(level, type, file, line, message);
}
void GenericLogFmtImpl(LogLevel level, LogType type, const char* file, int line, void GenericLogFmtImpl(LogLevel level, LogType type, const char* file, int line,
fmt::string_view format, const fmt::format_args& args) fmt::string_view format, const fmt::format_args& args)
{ {

View File

@ -8,6 +8,7 @@
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include "Common/CommonFuncs.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
#include "Core/HW/EXI/EXI_Device.h" #include "Core/HW/EXI/EXI_Device.h"
@ -29,7 +30,7 @@ bool CEXIETHERNET::TAPServerNetworkInterface::Activate()
sockaddr_un sun = {}; sockaddr_un sun = {};
if (sizeof(socket_path) > sizeof(sun.sun_path)) if (sizeof(socket_path) > sizeof(sun.sun_path))
{ {
ERROR_LOG(SP1, "Socket path is too long, unable to init BBA"); ERROR_LOG_FMT(SP1, "Socket path is too long, unable to init BBA");
return false; return false;
} }
sun.sun_family = AF_UNIX; sun.sun_family = AF_UNIX;
@ -38,19 +39,19 @@ bool CEXIETHERNET::TAPServerNetworkInterface::Activate()
fd = socket(AF_UNIX, SOCK_STREAM, 0); fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) if (fd == -1)
{ {
ERROR_LOG(SP1, "Couldn't create socket, unable to init BBA"); ERROR_LOG_FMT(SP1, "Couldn't create socket, unable to init BBA");
return false; return false;
} }
if (connect(fd, reinterpret_cast<sockaddr*>(&sun), sizeof(sun)) == -1) if (connect(fd, reinterpret_cast<sockaddr*>(&sun), sizeof(sun)) == -1)
{ {
ERROR_LOG(SP1, "Couldn't connect socket (%d), unable to init BBA", errno); ERROR_LOG_FMT(SP1, "Couldn't connect socket ({}), unable to init BBA", LastStrerrorString());
close(fd); close(fd);
fd = -1; fd = -1;
return false; return false;
} }
INFO_LOG(SP1, "BBA initialized.\n"); INFO_LOG_FMT(SP1, "BBA initialized.");
return RecvInit(); return RecvInit();
} }
@ -58,20 +59,20 @@ bool CEXIETHERNET::TAPServerNetworkInterface::SendFrame(const u8* frame, u32 siz
{ {
{ {
const std::string s = ArrayToString(frame, size, 0x10); const std::string s = ArrayToString(frame, size, 0x10);
INFO_LOG(SP1, "SendFrame %x\n%s\n", size, s.c_str()); INFO_LOG_FMT(SP1, "SendFrame {}\n{}", size, s);
} }
auto size16 = u16(size); auto size16 = u16(size);
if (write(fd, &size16, 2) != 2) if (write(fd, &size16, 2) != 2)
{ {
ERROR_LOG(SP1, "SendFrame(): could not write size field\n"); ERROR_LOG_FMT(SP1, "SendFrame(): could not write size field");
return false; return false;
} }
int written_bytes = write(fd, frame, size); int written_bytes = write(fd, frame, size);
if (u32(written_bytes) != size) if (u32(written_bytes) != size)
{ {
ERROR_LOG(SP1, "SendFrame(): expected to write %d bytes, instead wrote %d", size, ERROR_LOG_FMT(SP1, "SendFrame(): expected to write {} bytes, instead wrote {}", size,
written_bytes); written_bytes);
return false; return false;
} }
else else
@ -98,19 +99,19 @@ void CEXIETHERNET::TAPServerNetworkInterface::ReadThreadHandler()
u16 size; u16 size;
if (read(fd, &size, 2) != 2) if (read(fd, &size, 2) != 2)
{ {
ERROR_LOG(SP1, "Failed to read size field from BBA, err=%d", errno); ERROR_LOG_FMT(SP1, "Failed to read size field from BBA: {}", LastStrerrorString());
} }
else else
{ {
int read_bytes = read(fd, m_eth_ref->mRecvBuffer.get(), size); int read_bytes = read(fd, m_eth_ref->mRecvBuffer.get(), size);
if (read_bytes < 0) if (read_bytes < 0)
{ {
ERROR_LOG(SP1, "Failed to read packet data from BBA, err=%d", errno); ERROR_LOG_FMT(SP1, "Failed to read packet data from BBA: {}", LastStrerrorString());
} }
else if (readEnabled.IsSet()) else if (readEnabled.IsSet())
{ {
std::string data_string = ArrayToString(m_eth_ref->mRecvBuffer.get(), read_bytes, 0x10); std::string data_string = ArrayToString(m_eth_ref->mRecvBuffer.get(), read_bytes, 0x10);
INFO_LOG(SP1, "Read data: %s", data_string.c_str()); INFO_LOG_FMT(SP1, "Read data: {}", data_string);
m_eth_ref->mRecvBufferLength = read_bytes; m_eth_ref->mRecvBufferLength = read_bytes;
m_eth_ref->RecvHandlePacket(); m_eth_ref->RecvHandlePacket();
} }

View File

@ -50,7 +50,7 @@ CEXIETHERNET::CEXIETHERNET(BBADeviceType type)
#if defined(__APPLE__) #if defined(__APPLE__)
case BBADeviceType::TAPSERVER: case BBADeviceType::TAPSERVER:
m_network_interface = std::make_unique<TAPServerNetworkInterface>(this); m_network_interface = std::make_unique<TAPServerNetworkInterface>(this);
INFO_LOG(SP1, "Created tapserver physical network interface."); INFO_LOG_FMT(SP1, "Created tapserver physical network interface.");
break; break;
#endif #endif
case BBADeviceType::XLINK: case BBADeviceType::XLINK:

View File

@ -709,7 +709,7 @@ void JitArm64::Jit(u32 em_address, bool clear_cache_and_retry_on_failure)
{ {
// Code generation failed due to not enough free space in either the near or far code regions. // Code generation failed due to not enough free space in either the near or far code regions.
// Clear the entire JIT cache and retry. // Clear the entire JIT cache and retry.
WARN_LOG(POWERPC, "flushing code caches, please report if this happens a lot"); WARN_LOG_FMT(POWERPC, "flushing code caches, please report if this happens a lot");
ClearCache(); ClearCache();
Jit(em_address, false); Jit(em_address, false);
return; return;
@ -728,7 +728,7 @@ bool JitArm64::SetEmitterStateToFreeCodeRegion()
auto free_near = m_free_ranges_near.by_size_begin(); auto free_near = m_free_ranges_near.by_size_begin();
if (free_near == m_free_ranges_near.by_size_end()) if (free_near == m_free_ranges_near.by_size_end())
{ {
WARN_LOG(POWERPC, "Failed to find free memory region in near code region."); WARN_LOG_FMT(POWERPC, "Failed to find free memory region in near code region.");
return false; return false;
} }
SetCodePtr(free_near.from(), free_near.to()); SetCodePtr(free_near.from(), free_near.to());
@ -736,7 +736,7 @@ bool JitArm64::SetEmitterStateToFreeCodeRegion()
auto free_far = m_free_ranges_far.by_size_begin(); auto free_far = m_free_ranges_far.by_size_begin();
if (free_far == m_free_ranges_far.by_size_end()) if (free_far == m_free_ranges_far.by_size_end())
{ {
WARN_LOG(POWERPC, "Failed to find free memory region in far code region."); WARN_LOG_FMT(POWERPC, "Failed to find free memory region in far code region.");
return false; return false;
} }
m_far_code.SetCodePtr(free_far.from(), free_far.to()); m_far_code.SetCodePtr(free_far.from(), free_far.to());
@ -995,9 +995,9 @@ bool JitArm64::DoJit(u32 em_address, JitBlock* b, u32 nextPC)
if (HasWriteFailed() || m_far_code.HasWriteFailed()) if (HasWriteFailed() || m_far_code.HasWriteFailed())
{ {
if (HasWriteFailed()) if (HasWriteFailed())
WARN_LOG(POWERPC, "JIT ran out of space in near code region during code generation."); WARN_LOG_FMT(POWERPC, "JIT ran out of space in near code region during code generation.");
if (m_far_code.HasWriteFailed()) if (m_far_code.HasWriteFailed())
WARN_LOG(POWERPC, "JIT ran out of space in far code region during code generation."); WARN_LOG_FMT(POWERPC, "JIT ran out of space in far code region during code generation.");
return false; return false;
} }

View File

@ -325,9 +325,9 @@ void MainWindow::InitControllers()
if (!g_controller_interface.HasDefaultDevice()) if (!g_controller_interface.HasDefaultDevice())
{ {
// Note that the CI default device could be still temporarily removed at any time // Note that the CI default device could be still temporarily removed at any time
WARN_LOG(CONTROLLERINTERFACE, WARN_LOG_FMT(CONTROLLERINTERFACE,
"No default device has been added in time. EmulatedController(s) defaulting adds" "No default device has been added in time. EmulatedController(s) defaulting adds"
" input mappings made for a specific default device depending on the platform"); " input mappings made for a specific default device depending on the platform");
} }
GCAdapter::Init(); GCAdapter::Init();
Pad::Initialize(); Pad::Initialize();

View File

@ -13,6 +13,7 @@
#include <wrl.h> #include <wrl.h>
#include "Common/DynamicLibrary.h" #include "Common/DynamicLibrary.h"
#include "Common/HRWrap.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/ControllerInterface.h"
@ -481,8 +482,8 @@ private:
if (!waveform_name.data()) if (!waveform_name.data())
{ {
WARN_LOG(CONTROLLERINTERFACE, "WGInput: Unknown haptics feedback waveform: %d.", WARN_LOG_FMT(CONTROLLERINTERFACE, "WGInput: Unknown haptics feedback waveform: 0x{:04x}.",
waveform); waveform);
continue; continue;
} }
@ -502,23 +503,31 @@ private:
const auto switch_count = UINT32(m_switches.size()); const auto switch_count = UINT32(m_switches.size());
const auto axis_count = UINT32(m_axes.size()); const auto axis_count = UINT32(m_axes.size());
UINT64 timestamp = 0; UINT64 timestamp = 0;
if (FAILED(m_raw_controller->GetCurrentReading(button_count, m_buttons.data(), switch_count, const HRESULT raw_result = m_raw_controller->GetCurrentReading(
m_switches.data(), axis_count, m_axes.data(), button_count, m_buttons.data(), switch_count, m_switches.data(), axis_count, m_axes.data(),
&timestamp))) &timestamp);
if (FAILED(raw_result))
{ {
ERROR_LOG(CONTROLLERINTERFACE, "WGInput: IRawGameController::GetCurrentReading failed."); ERROR_LOG_FMT(CONTROLLERINTERFACE,
"WGInput: IRawGameController::GetCurrentReading failed: {}",
Common::HRWrap(raw_result));
} }
// IGamepad: // IGamepad:
if (m_gamepad && FAILED(m_gamepad->GetCurrentReading(&m_gamepad_reading))) if (m_gamepad)
{ {
ERROR_LOG(CONTROLLERINTERFACE, "WGInput: IGamepad::GetCurrentReading failed."); const HRESULT gamepad_result = m_gamepad->GetCurrentReading(&m_gamepad_reading);
if (FAILED(gamepad_result))
{
ERROR_LOG_FMT(CONTROLLERINTERFACE, "WGInput: IGamepad::GetCurrentReading failed: {}",
Common::HRWrap(gamepad_result));
}
} }
// IGameControllerBatteryInfo: // IGameControllerBatteryInfo:
if (m_controller_battery && !UpdateBatteryLevel()) if (m_controller_battery && !UpdateBatteryLevel())
{ {
DEBUG_LOG(CONTROLLERINTERFACE, "WGInput: UpdateBatteryLevel failed."); DEBUG_LOG_FMT(CONTROLLERINTERFACE, "WGInput: UpdateBatteryLevel failed.");
} }
} }
@ -648,7 +657,7 @@ void Init()
if (!LoadFunctionPointers()) if (!LoadFunctionPointers())
{ {
ERROR_LOG(CONTROLLERINTERFACE, "WGInput: System lacks support, skipping init."); ERROR_LOG_FMT(CONTROLLERINTERFACE, "WGInput: System lacks support, skipping init.");
return; return;
} }
@ -666,7 +675,7 @@ void Init()
break; break;
default: default:
ERROR_LOG(CONTROLLERINTERFACE, "WGInput: RoInitialize failed."); ERROR_LOG_FMT(CONTROLLERINTERFACE, "WGInput: RoInitialize failed: {}", Common::HRWrap(hr));
break; break;
} }
} }
@ -712,43 +721,49 @@ void PopulateDevices()
HSTRING_HEADER header_raw_game_controller; HSTRING_HEADER header_raw_game_controller;
HSTRING hstr_raw_game_controller; HSTRING hstr_raw_game_controller;
if (FAILED(WindowsCreateStringReferenceAutoSizeWrapper(L"Windows.Gaming.Input.RawGameController", HRESULT hr;
&header_raw_game_controller, if (FAILED(hr = WindowsCreateStringReferenceAutoSizeWrapper(
&hstr_raw_game_controller))) L"Windows.Gaming.Input.RawGameController", &header_raw_game_controller,
&hstr_raw_game_controller)))
{ {
ERROR_LOG(CONTROLLERINTERFACE, "WGInput: Failed to create string reference."); ERROR_LOG_FMT(CONTROLLERINTERFACE, "WGInput: Failed to create string reference: {}",
Common::HRWrap(hr));
return; return;
} }
HSTRING_HEADER header_gamepad; HSTRING_HEADER header_gamepad;
HSTRING hstr_gamepad; HSTRING hstr_gamepad;
if (FAILED(WindowsCreateStringReferenceAutoSizeWrapper(L"Windows.Gaming.Input.Gamepad", if (FAILED(hr = WindowsCreateStringReferenceAutoSizeWrapper(L"Windows.Gaming.Input.Gamepad",
&header_gamepad, &hstr_gamepad))) &header_gamepad, &hstr_gamepad)))
{ {
ERROR_LOG(CONTROLLERINTERFACE, "WGInput: Failed to create string reference."); ERROR_LOG_FMT(CONTROLLERINTERFACE, "WGInput: Failed to create string reference: {}",
Common::HRWrap(hr));
return; return;
} }
ComPtr<WGI::IRawGameControllerStatics> raw_stats; ComPtr<WGI::IRawGameControllerStatics> raw_stats;
if (FAILED(g_RoGetActivationFactory_address( if (FAILED(hr = g_RoGetActivationFactory_address(
hstr_raw_game_controller, __uuidof(WGI::IRawGameControllerStatics), &raw_stats))) hstr_raw_game_controller, __uuidof(WGI::IRawGameControllerStatics), &raw_stats)))
{ {
ERROR_LOG(CONTROLLERINTERFACE, "WGInput: Failed to get IRawGameControllerStatics."); ERROR_LOG_FMT(CONTROLLERINTERFACE, "WGInput: Failed to get IRawGameControllerStatics: {}",
Common::HRWrap(hr));
return; return;
} }
ComPtr<WGI::IGamepadStatics2> gamepad_stats; ComPtr<WGI::IGamepadStatics2> gamepad_stats;
if (FAILED(g_RoGetActivationFactory_address(hstr_gamepad, __uuidof(WGI::IGamepadStatics2), if (FAILED(hr = g_RoGetActivationFactory_address(hstr_gamepad, __uuidof(WGI::IGamepadStatics2),
&gamepad_stats))) &gamepad_stats)))
{ {
ERROR_LOG(CONTROLLERINTERFACE, "WGInput: Failed to get IGamepadStatics2."); ERROR_LOG_FMT(CONTROLLERINTERFACE, "WGInput: Failed to get IGamepadStatics2: {}",
Common::HRWrap(hr));
return; return;
} }
IVectorView<WGI::RawGameController*>* raw_controllers; IVectorView<WGI::RawGameController*>* raw_controllers;
if (FAILED(raw_stats->get_RawGameControllers(&raw_controllers))) if (FAILED(hr = raw_stats->get_RawGameControllers(&raw_controllers)))
{ {
ERROR_LOG(CONTROLLERINTERFACE, "WGInput: get_RawGameControllers failed."); ERROR_LOG_FMT(CONTROLLERINTERFACE, "WGInput: get_RawGameControllers failed: {}",
Common::HRWrap(hr));
return; return;
} }
@ -763,10 +778,10 @@ void PopulateDevices()
// Attempt to get the controller's name. // Attempt to get the controller's name.
WGI::IRawGameController2* rgc2 = nullptr; WGI::IRawGameController2* rgc2 = nullptr;
if (SUCCEEDED(raw_controller->QueryInterface(&rgc2)) && rgc2) if (SUCCEEDED(hr = raw_controller->QueryInterface(&rgc2)) && rgc2)
{ {
HSTRING hstr = {}; HSTRING hstr = {};
if (SUCCEEDED(rgc2->get_DisplayName(&hstr)) && hstr) if (SUCCEEDED(hr = rgc2->get_DisplayName(&hstr)) && hstr)
{ {
device_name = device_name =
StripSpaces(WStringToUTF8(g_WindowsGetStringRawBuffer_address(hstr, nullptr))); StripSpaces(WStringToUTF8(g_WindowsGetStringRawBuffer_address(hstr, nullptr)));
@ -775,7 +790,8 @@ void PopulateDevices()
if (device_name.empty()) if (device_name.empty())
{ {
ERROR_LOG(CONTROLLERINTERFACE, "WGInput: Failed to get device name."); ERROR_LOG_FMT(CONTROLLERINTERFACE, "WGInput: Failed to get device name: {}",
Common::HRWrap(hr));
// Set a default name if we couldn't query the name or it was empty. // Set a default name if we couldn't query the name or it was empty.
device_name = "Device"; device_name = "Device";
} }

View File

@ -28,7 +28,9 @@ extern "C" {
#include "Common/ChunkFile.h" #include "Common/ChunkFile.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/Logging/LogManager.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
#include "Common/StringUtil.h"
#include "Core/Config/MainSettings.h" #include "Core/Config/MainSettings.h"
#include "Core/ConfigManager.h" #include "Core/ConfigManager.h"
@ -95,7 +97,17 @@ void InitAVCodec()
// keep libav debug messages visible in release build of dolphin // keep libav debug messages visible in release build of dolphin
log_level = Common::Log::LogLevel::LINFO; log_level = Common::Log::LogLevel::LINFO;
GENERIC_LOG_V(Common::Log::LogType::FRAMEDUMP, log_level, fmt, vl); // Don't perform this formatting if the log level is disabled
auto* log_manager = Common::Log::LogManager::GetInstance();
if (log_manager != nullptr &&
log_manager->IsEnabled(Common::Log::LogType::FRAMEDUMP, log_level))
{
constexpr size_t MAX_MSGLEN = 1024;
char message[MAX_MSGLEN];
CharArrayFromFormatV(message, MAX_MSGLEN, fmt, vl);
GENERIC_LOG_FMT(Common::Log::LogType::FRAMEDUMP, log_level, "{}", message);
}
}); });
// TODO: We never call avformat_network_deinit. // TODO: We never call avformat_network_deinit.