From: lx-s Date: Fri, 8 Dec 2023 19:51:08 +0000 (+0100) Subject: Improved logging code. X-Git-Tag: 2.4.9.0~77^2~5 X-Git-Url: https://repos.lx-s.de/gitweb/?a=commitdiff_plain;h=e0b48541ba6c133ea3e0c8880ff5dbf3b28c793e;p=winmute.git Improved logging code. --- diff --git a/WinMute/WMLog.cpp b/WinMute/WMLog.cpp index fc3ef5f..395c6a8 100644 --- a/WinMute/WMLog.cpp +++ b/WinMute/WMLog.cpp @@ -62,7 +62,7 @@ std::wstring WMLog::GetLogFilePath() wchar_t tempPath[MAX_PATH + 1]; if (GetTempPathW(ARRAY_SIZE(tempPath), tempPath)) { std::wstring path{ tempPath }; - path += L"WinMute.log"; + path += LOG_FILE_NAME; return path; } return std::wstring(); @@ -148,25 +148,15 @@ void WMLog::WriteWindowsError(const wchar_t *functionName, DWORD lastError) | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, lastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - reinterpret_cast(&lpMsgBuf), 0, nullptr) != 0) { - size_t displayBufSize = - (wcslen(static_cast(lpMsgBuf)) - + wcslen(static_cast(functionName)) - + 40) * sizeof(wchar_t); - // Display the error message and exit the process - LPVOID lpDisplayBuf = reinterpret_cast( - LocalAlloc(LMEM_ZEROINIT, displayBufSize)); - if (lpDisplayBuf) { - StringCchPrintfW( - reinterpret_cast(lpDisplayBuf), - LocalSize(lpDisplayBuf), - L"%s failed with error %u: %s", - functionName, - lastError, - reinterpret_cast(lpMsgBuf)); - WriteMessage(static_cast(lpMsgBuf)); - LocalFree(lpDisplayBuf); - } - LocalFree(lpMsgBuf); + reinterpret_cast(&lpMsgBuf), 0, nullptr) == 0) { + return; } + const std::wstring errorMsg = std::vformat( + WMi18n::GetInstance().GetTextW(IDS_MAIN_ERROR_WINAPI_FAILURE_TEXT), + std::make_wformat_args( + functionName, + lastError, + reinterpret_cast(lpMsgBuf))); + WriteMessage(static_cast(lpMsgBuf)); + LocalFree(lpMsgBuf); } diff --git a/WinMute/common.h b/WinMute/common.h index 820595c..89a78c9 100644 --- a/WinMute/common.h +++ b/WinMute/common.h @@ -116,6 +116,8 @@ void PrintWindowsError(const wchar_t *functionName, DWORD lastError = -1); bool GetWinMuteVersion(std::wstring &versNumber); static const wchar_t *PROGRAM_NAME = L"WinMute"; +static const wchar_t *LOG_FILE_NAME = L"WinMute.log"; + constexpr int WM_SAVESETTINGS = WM_USER + 300; template