From aa3ad1fc8f599b203d47aa712b168e5baff81634 Mon Sep 17 00:00:00 2001 From: spx Date: Wed, 15 Dec 2010 22:03:36 +0000 Subject: [PATCH] Changed DebugLog and LuaLog more reliably. (Fixed an issue that Rainmeter crashes if format parameter includes '%'.) --- Library/Litestep.cpp | 21 +++++++++++++++++++-- Library/Litestep.h | 2 ++ Library/MeasureTime.cpp | 7 +------ Library/lua/LuaManager.cpp | 16 +++++++++++++--- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Library/Litestep.cpp b/Library/Litestep.cpp index 54934ac2..7b377dca 100644 --- a/Library/Litestep.cpp +++ b/Library/Litestep.cpp @@ -600,12 +600,29 @@ BOOL LSLog(int nLevel, LPCTSTR pszModule, LPCTSTR pszMessage) return TRUE; } +void RmNullCRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved) +{ + // Do nothing. +} + void DebugLog(const WCHAR* format, ... ) { WCHAR buffer[4096]; - va_list args; + va_list args; va_start( args, format ); - _vsnwprintf_s( buffer, 4096, _TRUNCATE, format, args ); + + _invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(RmNullCRTInvalidParameterHandler); + _CrtSetReportMode(_CRT_ASSERT, 0); + + errno = 0; + _vsnwprintf_s( buffer, 4096, _TRUNCATE, format, args ); + if (errno != 0) + { + _snwprintf_s(buffer, 4096, _TRUNCATE, L"DebugLog internal error: %s", format); + } + + _set_invalid_parameter_handler(oldHandler); + LSLog(LOG_DEBUG, L"Rainmeter", buffer); va_end(args); } diff --git a/Library/Litestep.h b/Library/Litestep.h index d37a53fa..c0bed753 100644 --- a/Library/Litestep.h +++ b/Library/Litestep.h @@ -52,6 +52,8 @@ BOOL RemoveBangCommand(LPCSTR command); void TransparentBltLS (HDC dc, int nXDest, int nYDest, int nWidth, int nHeight, HDC tempDC, int nXSrc, int nYSrc, COLORREF colorTransparent); void VarExpansion(LPSTR buffer, LPCSTR value); void LSSetVariable(const BSTR name, const BSTR value); + +void RmNullCRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved); void DebugLog(const WCHAR* message, ... ); void ResetLoggingFlag(); diff --git a/Library/MeasureTime.cpp b/Library/MeasureTime.cpp index 3f353e7b..058f985b 100644 --- a/Library/MeasureTime.cpp +++ b/Library/MeasureTime.cpp @@ -22,11 +22,6 @@ #include "MeasureTime.h" #include "Rainmeter.h" -void MeasureTime_CRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved) -{ - // Do nothing. -} - int GetYearDay(int year, int month, int day) { int yearDay = 0; @@ -86,7 +81,7 @@ void CMeasureTime::TimeToString(WCHAR* buf, size_t bufLen, const WCHAR* format, { if (bufLen > 0) { - _invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(MeasureTime_CRTInvalidParameterHandler); + _invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(RmNullCRTInvalidParameterHandler); _CrtSetReportMode(_CRT_ASSERT, 0); errno = 0; diff --git a/Library/lua/LuaManager.cpp b/Library/lua/LuaManager.cpp index dff9d3fc..aa83f821 100644 --- a/Library/lua/LuaManager.cpp +++ b/Library/lua/LuaManager.cpp @@ -56,10 +56,20 @@ void LuaManager::ReportErrors(lua_State * L) void LuaManager::LuaLog(const char* format, ... ) { char buffer[4096]; - va_list args; - va_start( args, format ); + va_list args; + va_start( args, format ); - _vsnprintf_s(buffer, 4096, _TRUNCATE, format, args ); + _invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(RmNullCRTInvalidParameterHandler); + _CrtSetReportMode(_CRT_ASSERT, 0); + + errno = 0; + _vsnprintf_s( buffer, 4096, _TRUNCATE, format, args ); + if (errno != 0) + { + _snprintf_s(buffer, 4096, _TRUNCATE, "LuaLog internal error: %s", format); + } + + _set_invalid_parameter_handler(oldHandler); #ifndef _DEBUG // Forcing output to the Debug Output window!