]> repos.lx-s.de Git - winmute.git/commitdiff
Improved logging code.
authorlx-s <lx-s@users.noreply.github.com>
Fri, 8 Dec 2023 19:51:08 +0000 (20:51 +0100)
committerlx-s <lx-s@users.noreply.github.com>
Fri, 8 Dec 2023 19:51:08 +0000 (20:51 +0100)
WinMute/WMLog.cpp
WinMute/common.h

index fc3ef5ffb3fc3e149fcc30ee2d1d92d98d95b58e..395c6a87926e9e723b9a45d5a600d82bb7b28389 100644 (file)
@@ -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<wchar_t*>(&lpMsgBuf), 0, nullptr) != 0) {
-      size_t displayBufSize =
-         (wcslen(static_cast<const wchar_t*>(lpMsgBuf))
-          + wcslen(static_cast<const wchar_t*>(functionName))
-          + 40) * sizeof(wchar_t);
-      // Display the error message and exit the process
-      LPVOID lpDisplayBuf = reinterpret_cast<LPVOID>(
-         LocalAlloc(LMEM_ZEROINIT, displayBufSize));
-      if (lpDisplayBuf) {
-         StringCchPrintfW(
-            reinterpret_cast<wchar_t*>(lpDisplayBuf),
-            LocalSize(lpDisplayBuf),
-            L"%s failed with error %u: %s",
-            functionName,
-            lastError,
-            reinterpret_cast<wchar_t*>(lpMsgBuf));
-         WriteMessage(static_cast<const wchar_t*>(lpMsgBuf));
-         LocalFree(lpDisplayBuf);
-      }
-      LocalFree(lpMsgBuf);
+         reinterpret_cast<wchar_t*>(&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<wchar_t *>(lpMsgBuf)));
+   WriteMessage(static_cast<const wchar_t*>(lpMsgBuf));
+   LocalFree(lpMsgBuf);
 }
index 820595cbc9ece33e8709bebca49685c7e6c4f23e..89a78c91425347550e5bca32518a6d393f9c4bff 100644 (file)
@@ -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<class Interface>