From c1c6b904499c0a5a54c9b3ad24f2019c381a8a6b Mon Sep 17 00:00:00 2001 From: Rosalie Wanders Date: Fri, 24 May 2024 12:15:18 +0200 Subject: [PATCH] Use mupen64plus API for logging --- src/Log.cpp | 50 ++++++++++++++++++++++++++++++ src/MupenPlusPluginAPI.cpp | 2 +- src/PluginAPI.h | 2 +- src/mupenplus/GLideN64_mupenplus.h | 4 +++ src/mupenplus/MupenPlusAPIImpl.cpp | 10 ++++-- 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/Log.cpp b/src/Log.cpp index 557fc7982..94235187d 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -13,6 +13,8 @@ #include #include +#ifndef MUPENPLUSAPI // zilmar spec + std::mutex g_logMutex; std::wofstream fileOutput; @@ -113,6 +115,54 @@ void LogDebug(const char* _fileName, int _line, u16 _type, const char* _format, fileOutput.flush(); } +#else // mupen64plus +#include "mupenplus/GLideN64_mupenplus.h" + +void LogDebug(const char* _fileName, int _line, u16 _type, const char* _format, ...) +{ + static const int logLevel[] = { + M64MSG_INFO, + M64MSG_ERROR, + M64MSG_INFO, + M64MSG_WARNING, + M64MSG_VERBOSE, + M64MSG_VERBOSE + }; + + if (CoreDebugCallback == nullptr || + _type > LOG_LEVEL) + { + return; + } + + // initialize use of the variable argument array + va_list vaArgs; + va_start(vaArgs, _format); + + // reliably acquire the size from a copy of + // the variable argument array + // and a functionally reliable call + // to mock the formatting + va_list vaCopy; + va_copy(vaCopy, vaArgs); + const int iLen = std::vsnprintf(NULL, 0, _format, vaCopy); + va_end(vaCopy); + + // return a formatted string without + // risking memory mismanagement + // and without assuming any compiler + // or platform specific behavior + std::vector zc(iLen + 1); + std::vsnprintf(zc.data(), zc.size(), _format, vaArgs); + va_end(vaArgs); + + std::stringstream formatString; + formatString << _fileName << ":" << _line << ", \"" << zc.data() << "\""; + + CoreDebugCallback(CoreDebugCallbackContext, logLevel[_type], formatString.str().c_str()); +} +#endif + #if defined(OS_WINDOWS) && !defined(MINGW) #include "windows/GLideN64_windows.h" void debugPrint(const char * format, ...) { diff --git a/src/MupenPlusPluginAPI.cpp b/src/MupenPlusPluginAPI.cpp index ea777917b..01a099023 100644 --- a/src/MupenPlusPluginAPI.cpp +++ b/src/MupenPlusPluginAPI.cpp @@ -32,7 +32,7 @@ EXPORT m64p_error CALL PluginStartup( void (*DebugCallback)(void *, int, const char *) ) { - return api().PluginStartup(CoreLibHandle); + return api().PluginStartup(CoreLibHandle, Context, DebugCallback); } #ifdef M64P_GLIDENUI diff --git a/src/PluginAPI.h b/src/PluginAPI.h index cc6e222e1..896c5a29c 100644 --- a/src/PluginAPI.h +++ b/src/PluginAPI.h @@ -76,7 +76,7 @@ class PluginAPI void ResizeVideoOutput(int _Width, int _Height); void ReadScreen2(void * _dest, int * _width, int * _height, int _front); - m64p_error PluginStartup(m64p_dynlib_handle _CoreLibHandle); + m64p_error PluginStartup(m64p_dynlib_handle _CoreLibHandle, void * Context, void (*DebugCallback)(void *, int, const char *)); #ifdef M64P_GLIDENUI m64p_error PluginConfig(); #endif // M64P_GLIDENUI diff --git a/src/mupenplus/GLideN64_mupenplus.h b/src/mupenplus/GLideN64_mupenplus.h index c925d1523..ba4bb168c 100644 --- a/src/mupenplus/GLideN64_mupenplus.h +++ b/src/mupenplus/GLideN64_mupenplus.h @@ -4,6 +4,7 @@ #include "m64p_common.h" #include "m64p_config.h" #include "m64p_vidext.h" +#include "m64p_frontend.h" #define PLUGIN_VERSION 0x020000 #define VIDEO_PLUGIN_API_VERSION 0x020200 @@ -53,6 +54,9 @@ extern ptr_VidExt_GL_GetDefaultFramebuffer CoreVideo_GL_GetDefaultFramebuffer; extern ptr_PluginGetVersion CoreGetVersion; +extern void* CoreDebugCallbackContext; +extern ptr_DebugCallback CoreDebugCallback; + extern const unsigned int* rdram_size; extern void(*renderCallback)(int); diff --git a/src/mupenplus/MupenPlusAPIImpl.cpp b/src/mupenplus/MupenPlusAPIImpl.cpp index f814d3894..d1835c9d2 100644 --- a/src/mupenplus/MupenPlusAPIImpl.cpp +++ b/src/mupenplus/MupenPlusAPIImpl.cpp @@ -49,12 +49,18 @@ ptr_VidExt_GL_GetDefaultFramebuffer CoreVideo_GL_GetDefaultFramebuffer = nullptr ptr_PluginGetVersion CoreGetVersion = nullptr; +void* CoreDebugCallbackContext = nullptr; +ptr_DebugCallback CoreDebugCallback = nullptr; + const unsigned int* rdram_size = nullptr; -void(*renderCallback)(int) = nullptr; +void (*renderCallback)(int) = nullptr; -m64p_error PluginAPI::PluginStartup(m64p_dynlib_handle _CoreLibHandle) +m64p_error PluginAPI::PluginStartup(m64p_dynlib_handle _CoreLibHandle, void* Context, void (*DebugCallback)(void *, int, const char *)) { + CoreDebugCallbackContext = Context; + CoreDebugCallback = DebugCallback; + ConfigGetSharedDataFilepath = (ptr_ConfigGetSharedDataFilepath) DLSYM(_CoreLibHandle, "ConfigGetSharedDataFilepath"); ConfigGetUserConfigPath = (ptr_ConfigGetUserConfigPath) DLSYM(_CoreLibHandle, "ConfigGetUserConfigPath"); ConfigGetUserCachePath = (ptr_ConfigGetUserCachePath)DLSYM(_CoreLibHandle, "ConfigGetUserCachePath");