From: lx-s Date: Wed, 8 Jul 2026 13:09:14 +0000 (+0200) Subject: Replaced printf-format styles with std::format X-Git-Tag: 2.6.0.0~69 X-Git-Url: https://repos.lx-s.de/gitweb/?a=commitdiff_plain;h=0eee2919e08716853269479fb5b4501de03ec527;p=winmute.git Replaced printf-format styles with std::format --- diff --git a/WinMute/BluetoothDetector.cpp b/WinMute/BluetoothDetector.cpp index 460a53a..2dd4586 100644 --- a/WinMute/BluetoothDetector.cpp +++ b/WinMute/BluetoothDetector.cpp @@ -162,15 +162,15 @@ BluetoothDetector::BluetoothStatus BluetoothDetector::GetBluetoothStatus( if (useDeviceList_) { auto pos = std::find(begin(deviceNames_), end(deviceNames_), deviceName); if (pos == end(deviceNames_)) { - log.LogInfo(L"Bluetooth device \"%s\" not in list.", deviceName.c_str()); + log.LogInfo(L"Bluetooth device \"{}\" not in list.", deviceName); return BluetoothStatus::Unknown; } } if ((inRangeInfo->deviceInfo.flags & BDIF_CONNECTED) && !(inRangeInfo->previousDeviceFlags & BDIF_CONNECTED)) { - log.LogInfo(L"Bluetooth Audio device \"%s\" connected.", deviceName.c_str()); + log.LogInfo(L"Bluetooth Audio device \"{}\" connected.", deviceName); return BluetoothStatus::Connected; } else if (!(inRangeInfo->deviceInfo.flags & BDIF_CONNECTED) && (inRangeInfo->previousDeviceFlags & BDIF_CONNECTED)) { - log.LogInfo(L"Bluetooth Audio device \"%s\" disconnected.", deviceName.c_str()); + log.LogInfo(L"Bluetooth Audio device \"{}\" disconnected.", deviceName); return BluetoothStatus::Disconnected; } return BluetoothStatus::Unknown; diff --git a/WinMute/MMNotificationClient.cpp b/WinMute/MMNotificationClient.cpp index f144ce0..bc00b25 100644 --- a/WinMute/MMNotificationClient.cpp +++ b/WinMute/MMNotificationClient.cpp @@ -89,7 +89,7 @@ STDMETHODIMP_(HRESULT) MMNotificationClient::OnDeviceAdded(LPCWSTR pwstrDeviceId { if (notifyParent_ && pwstrDeviceId != nullptr) { const auto deviceName = GetDeviceNameFromId(pwstrDeviceId); - WMLog::GetInstance().LogInfo(L"Device \"%s\" added", deviceName.c_str()); + WMLog::GetInstance().LogInfo(L"Device \"{}\" added", deviceName); notifyParent_->ShouldReInit(); } return S_OK; @@ -99,7 +99,7 @@ STDMETHODIMP_(HRESULT) MMNotificationClient::OnDeviceRemoved(LPCWSTR pwstrDevice { if (notifyParent_ && pwstrDeviceId != nullptr) { const auto deviceName = GetDeviceNameFromId(pwstrDeviceId); - WMLog::GetInstance().LogInfo(L"Device \"%s\" removed", deviceName.c_str()); + WMLog::GetInstance().LogInfo(L"Device \"{}\" removed", deviceName); notifyParent_->ShouldReInit(); } return S_OK; @@ -127,9 +127,9 @@ STDMETHODIMP_(HRESULT) MMNotificationClient::OnDeviceStateChanged( // TODO: Check if output device const auto deviceName = GetDeviceNameFromId(pwstrDeviceId); WMLog::GetInstance().LogInfo( - L"Device \"%s\" status changed to %s", - deviceName.c_str(), - what.c_str()); + L"Device \"{}\" status changed to {}", + deviceName, + what); notifyParent_->ShouldReInit(); } return S_OK; diff --git a/WinMute/MediaController.cpp b/WinMute/MediaController.cpp index 92bcdf1..98a145a 100644 --- a/WinMute/MediaController.cpp +++ b/WinMute/MediaController.cpp @@ -84,8 +84,8 @@ bool MediaPlaybackController::TryPauseCurrentSession(bool &pausedMedia) } } } catch (const hresult_error &e) { - log.LogError(L"MediaPlaybackController: Failed to pause current media session. Error: %s", - e.message().c_str()); + log.LogError(L"MediaPlaybackController: Failed to pause current media session. Error: {}", + e.message()); return false; } return true; @@ -108,8 +108,8 @@ bool MediaPlaybackController::TryPlayCurrentSession() } } } catch (const hresult_error &e) { - log.LogError(L"MediaPlaybackController: Failed to resume current media session. Error: %s", - e.message().c_str()); + log.LogError(L"MediaPlaybackController: Failed to resume current media session. Error: {}", + e.message()); return false; } return true; diff --git a/WinMute/MuteControl.cpp b/WinMute/MuteControl.cpp index 3141e69..48f468c 100644 --- a/WinMute/MuteControl.cpp +++ b/WinMute/MuteControl.cpp @@ -161,7 +161,7 @@ LRESULT CALLBACK MuteControl::WindowProc( void MuteControl::SetMute(bool mute) { - WMLog::GetInstance().LogInfo(L"Manual muting: %s", mute ? L"on" : L"off"); + WMLog::GetInstance().LogInfo(L"Manual muting: {}", mute ? L"on" : L"off"); winAudio_->SetMute(mute); } @@ -438,14 +438,14 @@ void MuteControl::NotifyRestoreCondition(MuteType type, bool active, bool withDe void MuteControl::NotifyWorkstationLock(bool active) { - WMLog::GetInstance().LogInfo(L"Mute Event: Workstation Lock %s", + WMLog::GetInstance().LogInfo(L"Mute Event: Workstation Lock {}", active ? L"start" : L"stop"); NotifyRestoreCondition(MuteTypeWorkstationLock, active); } void MuteControl::NotifyRemoteSession(bool active) { - WMLog::GetInstance().LogInfo(L"Mute Event: Remote Session %s", + WMLog::GetInstance().LogInfo(L"Mute Event: Remote Session {}", active ? L"start" : L"stop"); NotifyRestoreCondition(MuteTypeRemoteSession, active); } @@ -454,7 +454,7 @@ void MuteControl::NotifyDisplayStandby(bool active) { if (displayWasOffOnce_ || active) { - WMLog::GetInstance().LogInfo(L"Mute Event: Display Standby %s", + WMLog::GetInstance().LogInfo(L"Mute Event: Display Standby {}", active ? L"start" : L"stop"); NotifyRestoreCondition(MuteTypeDisplayStandby, active); displayWasOffOnce_ = true; @@ -464,7 +464,7 @@ void MuteControl::NotifyDisplayStandby(bool active) void MuteControl::NotifyBluetoothConnected(bool connected) { WMLog::GetInstance().LogInfo( - L"Mute Event: Bluetooth audio device %s", + L"Mute Event: Bluetooth audio device {}", connected ? L"connected" : L"disconnected"); NotifyRestoreCondition(MuteTypeBluetoothDisconnect, !connected, true); } diff --git a/WinMute/QuietHoursTimer.cpp b/WinMute/QuietHoursTimer.cpp index 56b997c..adc1f32 100644 --- a/WinMute/QuietHoursTimer.cpp +++ b/WinMute/QuietHoursTimer.cpp @@ -153,7 +153,7 @@ bool QuietHoursTimer::SetStart() } WMLog::GetInstance().LogInfo( - L"QuietHours: Next start scheduled in %u s (window %d, %02u:%02u-%02u:%02u)", + L"QuietHours: Next start scheduled in {} s (window {}, {:02}:{:02}-{:02}:{:02})", diffSec, idx, windows_[idx].first / 3600, (windows_[idx].first % 3600) / 60, windows_[idx].second / 3600, (windows_[idx].second % 3600) / 60); @@ -189,7 +189,7 @@ bool QuietHoursTimer::SetEnd() } WMLog::GetInstance().LogInfo( - L"QuietHours: End scheduled in %u s (window %d)", diffSec, activeWindowIdx_); + L"QuietHours: End scheduled in {} s (window {})", diffSec, activeWindowIdx_); return true; } @@ -226,7 +226,7 @@ bool QuietHoursTimer::LoadFromSettings(WMSettings& settings) const int activeIdx = FindActiveWindowIdx(); if (activeIdx != -1) { activeWindowIdx_ = activeIdx; - WMLog::GetInstance().LogInfo(L"QuietHours: Currently inside window %d — muting now", activeIdx); + WMLog::GetInstance().LogInfo(L"QuietHours: Currently inside window {} — muting now", activeIdx); // Sending START will cause WinMute to mute and call SetEnd() SendMessageW(hParent_, WM_WINMUTE_QUIETHOURS_START, 0, 0); } else { diff --git a/WinMute/Settings_GeneralDlg.cpp b/WinMute/Settings_GeneralDlg.cpp index 1e22dde..4d48f67 100644 --- a/WinMute/Settings_GeneralDlg.cpp +++ b/WinMute/Settings_GeneralDlg.cpp @@ -52,7 +52,7 @@ static void FillLanguageList(HWND hLanguageList, const SettingsGeneralData& dlgD for (const auto &lang : dlgData.langModules) { const auto itemId = SendMessage(hLanguageList, CB_ADDSTRING, 0, reinterpret_cast(lang.langName.c_str())); if (itemId == CB_ERR || itemId == CB_ERRSPACE) { - WMLog::GetInstance().LogError(L"Failed to add language %ls to language selector", lang.langName.c_str()); + WMLog::GetInstance().LogError(L"Failed to add language {} to language selector", lang.langName); } else { ComboBox_SetItemData(hLanguageList, itemId, lang.fileName.c_str()); } diff --git a/WinMute/Settings_ManageEndpointsDlg.cpp b/WinMute/Settings_ManageEndpointsDlg.cpp index baef2af..4b654bb 100644 --- a/WinMute/Settings_ManageEndpointsDlg.cpp +++ b/WinMute/Settings_ManageEndpointsDlg.cpp @@ -69,7 +69,7 @@ static bool GetAudioEndpoints(std::vector& endpoints) for (UINT i = 0; i < epCount; ++i) { CComPtr device = nullptr; if (FAILED(audioEndpoints->Item(i, &device))) { - log.LogError(L"Failed to get audio endpoint #%d", i); + log.LogError(L"Failed to get audio endpoint #{}", i); continue; } diff --git a/WinMute/UpdateChecker.cpp b/WinMute/UpdateChecker.cpp index ecf9e28..03dc148 100644 --- a/WinMute/UpdateChecker.cpp +++ b/WinMute/UpdateChecker.cpp @@ -129,13 +129,13 @@ bool UpdateChecker::ParseVersionFile(const std::string& fileContents, } } catch (const nlohmann::json::parse_error& pe) { - log.LogError(L"Failed to parse update: %S", pe.what()); + log.LogError(L"Failed to parse update: {}", ConvertStringToWideString(pe.what())); return false; } catch (const nlohmann::json::type_error& te) { - log.LogError(L"Failed to parse update: %S", te.what()); + log.LogError(L"Failed to parse update: {}", ConvertStringToWideString(te.what())); return false; } catch (const std::exception& e) { - log.LogError(L"Failed to parse update: %S", e.what()); + log.LogError(L"Failed to parse update: {}", ConvertStringToWideString(e.what())); return false; } return true; @@ -168,16 +168,16 @@ std::optional UpdateChecker::IsVersionGreater( std::vector curVersParsed; WMLog& log = WMLog::GetInstance(); if (!ParseVersion(newVers, newVersParsed)) { - log.LogError(L"Failed to parse new version string \"%s\"", - newVers.c_str()); + log.LogError(L"Failed to parse new version string \"{}\"", + newVers); return std::nullopt; } else if (!ParseVersion(curVers, curVersParsed)) { - log.LogError(L"Failed to parse current version string \"%s\"", - curVers.c_str()); + log.LogError(L"Failed to parse current version string \"{}\"", + curVers); return std::nullopt; } else if (newVersParsed.size() != curVersParsed.size()) { - log.LogError(L"Version format mismatch \"%s\" / \"%s\"", - curVers.c_str(), newVers.c_str()); + log.LogError(L"Version format mismatch \"{}\" / \"{}\"", + curVers, newVers); return std::nullopt; } for (size_t i = 0; i < newVersParsed.size(); ++i) { @@ -294,7 +294,7 @@ bool UpdateChecker::GetVersionFile(std::string& fileContents) const GetLastError()); return false; } else if (statusCode != HTTP_STATUS_OK) { - log.LogError(L"Update-Server returned HTTP %d", statusCode); + log.LogError(L"Update-Server returned HTTP {}", statusCode); return false; } diff --git a/WinMute/Utility.cpp b/WinMute/Utility.cpp index eac3f61..d798c38 100644 --- a/WinMute/Utility.cpp +++ b/WinMute/Utility.cpp @@ -197,7 +197,7 @@ bool LaunchBrowser(HWND hParent, const std::wstring &url) }; if (!hasPrefix(L"https://") && !hasPrefix(L"http://")) { WMLog::GetInstance().LogError( - L"Refused to open \"%s\": not a http(s) URL", url.c_str()); + L"Refused to open \"{}\": not a http(s) URL", url); return false; } diff --git a/WinMute/VistaAudio.cpp b/WinMute/VistaAudio.cpp index 7cef244..ef4093b 100644 --- a/WinMute/VistaAudio.cpp +++ b/WinMute/VistaAudio.cpp @@ -86,16 +86,16 @@ bool VistaAudio::LoadAllEndpoints() hr = audioEndpoints->Item(i, &device); if (FAILED(hr)) { - log.LogError(L"Failed to get audio endpoint #%d", i); + log.LogError(L"Failed to get audio endpoint #{}", i); continue; } const auto deviceName = GetAudioDeviceName(device); if (!deviceName) { - log.LogError(L"Failed to get device name for audio endpoint #%d", i); + log.LogError(L"Failed to get device name for audio endpoint #{}", i); continue; } else { - log.LogInfo(L"Found audio endpoint \"%s\"", deviceName->c_str()); + log.LogInfo(L"Found audio endpoint \"{}\"", *deviceName); ep->deviceName = *deviceName; } @@ -103,15 +103,15 @@ bool VistaAudio::LoadAllEndpoints() if (FAILED(device->Activate( __uuidof(IAudioSessionManager2), CLSCTX_INPROC_SERVER, nullptr, reinterpret_cast(&sessionManager2)))) { - log.LogError(L"Failed to retrieve audio session manager for \"%s\"", - ep->deviceName.c_str()); + log.LogError(L"Failed to retrieve audio session manager for \"{}\"", + ep->deviceName); continue; } hr = sessionManager2->GetAudioSessionControl(nullptr, 0, &ep->sessionCtrl); if (FAILED(hr)) { - log.LogError(L"Failed to retrieve audio session manager for \"%s\"", - ep->deviceName.c_str()); + log.LogError(L"Failed to retrieve audio session manager for \"{}\"", + ep->deviceName); continue; } @@ -124,8 +124,8 @@ bool VistaAudio::LoadAllEndpoints() __uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, nullptr, reinterpret_cast(&ep->endpointVolume)); if (FAILED(hr)) { - log.LogError(L"Failed to active endpoint volume for device \"%s\"", - ep->deviceName.c_str()); + log.LogError(L"Failed to active endpoint volume for device \"{}\"", + ep->deviceName); continue; } endpoints_.push_back(std::move(ep)); @@ -199,8 +199,8 @@ bool VistaAudio::AllEndpointsMuted() BOOL isMuted = FALSE; if (FAILED(e->endpointVolume->GetMute(&isMuted))) { log.LogError( - L"Failed to get mute status for \"%s\"", - e->deviceName.c_str()); + L"Failed to get mute status for \"{}\"", + e->deviceName); return false; } else if (isMuted == FALSE) { return false; @@ -219,8 +219,8 @@ bool VistaAudio::SaveMuteStatus() BOOL isMuted = FALSE; if (FAILED(e->endpointVolume->GetMute(&isMuted))) { log.LogError( - L"Failed to get mute status for \"%s\"", - e->deviceName.c_str()); + L"Failed to get mute status for \"{}\"", + e->deviceName); success = false; } else { e->wasMuted = isMuted; @@ -240,17 +240,17 @@ bool VistaAudio::RestoreMuteStatus() } for (auto& e : endpoints_) { if (!IsEndpointManaged(e->deviceName)) { - log.LogInfo(L"Skipping Endpoint %s", e->deviceName.c_str()); + log.LogInfo(L"Skipping Endpoint {}", e->deviceName); continue; } - log.LogInfo(L"Restoring: Mute %s for \"%s\"", + log.LogInfo(L"Restoring: Mute {} for \"{}\"", (e->wasMuted) ? L"true" : L"false", - e->deviceName.c_str()); + e->deviceName); if (e->wasMuted != true) { if (FAILED(e->endpointVolume->SetMute(false, nullptr))) { - log.LogError(L"Failed to restore mute status to %s for \"%s\"", + log.LogError(L"Failed to restore mute status to {} for \"{}\"", (e->wasMuted) ? L"true" : L"false", - e->deviceName.c_str()); + e->deviceName); success = false; } } @@ -266,20 +266,20 @@ void VistaAudio::SetMute(bool mute) for (auto& e : endpoints_) { BOOL isMuted = !mute; if (!IsEndpointManaged(e->deviceName)) { - log.LogInfo(L"Skipping Endpoint %s", e->deviceName.c_str()); + log.LogInfo(L"Skipping Endpoint {}", e->deviceName); continue; } if (FAILED(e->endpointVolume->GetMute(&isMuted))) { log.LogError( - L"Failed to get mute status for \"%s\"", - e->deviceName.c_str()); + L"Failed to get mute status for \"{}\"", + e->deviceName); } if (!!isMuted != mute) { if (FAILED(e->endpointVolume->SetMute(mute, nullptr))) { log.LogError( - L"Failed to set mute status to %s for \"%s\"", + L"Failed to set mute status to {} for \"{}\"", (e->wasMuted) ? L"true" : L"false", - e->deviceName.c_str()); + e->deviceName); } } } diff --git a/WinMute/WMLog.cpp b/WinMute/WMLog.cpp index 7f65dff..1c76be4 100644 --- a/WinMute/WMLog.cpp +++ b/WinMute/WMLog.cpp @@ -205,56 +205,6 @@ void WMLog::UnregisterForLogUpdates(HWND hWnd) } } -void WMLog::LogDebug(_In_z_ _Printf_format_string_ const wchar_t *fmt, ...) -{ - wchar_t buf[1024]; - va_list ap; - va_start(ap, fmt); - - // _TRUNCATE: oversized messages (e.g. containing long external strings) - // must be cut short, not trip the invalid-parameter handler. - _vsnwprintf_s(buf, ARRAY_SIZE(buf), _TRUNCATE, fmt, ap); - StoreMessage(LogLevel::Debug, buf); - - va_end(ap); -} - -void WMLog::LogInfo(_In_z_ _Printf_format_string_ const wchar_t *fmt, ...) -{ - wchar_t buf[1024]; - va_list ap; - va_start(ap, fmt); - - _vsnwprintf_s(buf, ARRAY_SIZE(buf), _TRUNCATE, fmt, ap); - StoreMessage(LogLevel::Info, buf); - - va_end(ap); -} - -void WMLog::LogWarning(_In_z_ _Printf_format_string_ const wchar_t *fmt, ...) -{ - wchar_t buf[1024]; - va_list ap; - va_start(ap, fmt); - - _vsnwprintf_s(buf, ARRAY_SIZE(buf), _TRUNCATE, fmt, ap); - StoreMessage(LogLevel::Warning, buf); - - va_end(ap); -} - -void WMLog::LogError(_In_z_ _Printf_format_string_ const wchar_t *fmt, ...) -{ - wchar_t buf[1024]; - va_list ap; - va_start(ap, fmt); - - _vsnwprintf_s(buf, ARRAY_SIZE(buf), _TRUNCATE, fmt, ap); - StoreMessage(LogLevel::Error, buf); - - va_end(ap); -} - void WMLog::LogWinError(const wchar_t *functionName, DWORD lastError) { // Retrieve the system error message for the last-error code diff --git a/WinMute/WMLog.h b/WinMute/WMLog.h index 0964863..7955159 100644 --- a/WinMute/WMLog.h +++ b/WinMute/WMLog.h @@ -35,6 +35,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "common.h" +#include + enum class LogLevel { Debug, Info, @@ -52,10 +54,31 @@ class WMLog { public: static WMLog& GetInstance(); - void LogDebug(_In_z_ _Printf_format_string_ const wchar_t *fmt, ...); - void LogInfo(_In_z_ _Printf_format_string_ const wchar_t* fmt, ...); - void LogWarning(_In_z_ _Printf_format_string_ const wchar_t *fmt, ...); - void LogError(_In_z_ _Printf_format_string_ const wchar_t *fmt, ...); + // The format string is validated against the arguments at compile time. + template + void LogDebug(std::wformat_string fmt, Args&&... args) + { + StoreMessage(LogLevel::Debug, + std::format(fmt, std::forward(args)...).c_str()); + } + template + void LogInfo(std::wformat_string fmt, Args&&... args) + { + StoreMessage(LogLevel::Info, + std::format(fmt, std::forward(args)...).c_str()); + } + template + void LogWarning(std::wformat_string fmt, Args&&... args) + { + StoreMessage(LogLevel::Warning, + std::format(fmt, std::forward(args)...).c_str()); + } + template + void LogError(std::wformat_string fmt, Args&&... args) + { + StoreMessage(LogLevel::Error, + std::format(fmt, std::forward(args)...).c_str()); + } void LogWinError(const wchar_t *functionName, DWORD errorCode = -1); void EnableLogFile(bool enable); diff --git a/WinMute/WMSettings.cpp b/WinMute/WMSettings.cpp index 27e2223..6a7da02 100644 --- a/WinMute/WMSettings.cpp +++ b/WinMute/WMSettings.cpp @@ -218,7 +218,7 @@ static bool ReadStringFromRegistry(HKEY hKey, const wchar_t* valueName, if (regError != ERROR_SUCCESS) { if (regError != ERROR_FILE_NOT_FOUND) { ShowWindowsError(L"RegGetValueW", regError); - log.LogError(L"[Registry] Failed to read key \"%s\".", valueName); + log.LogError(L"[Registry] Failed to read key \"{}\".", valueName); } } else { std::wstring buf(bufSize / sizeof(wchar_t), L'\0'); @@ -226,7 +226,7 @@ static bool ReadStringFromRegistry(HKEY hKey, const wchar_t* valueName, nullptr, buf.data(), &bufSize); if (regError != ERROR_SUCCESS) { ShowWindowsError(L"RegGetValueW", regError); - log.LogError(L"[Registry] Failed to query value for key \"%s\".", + log.LogError(L"[Registry] Failed to query value for key \"{}\".", valueName); } else { buf.resize(wcslen(buf.c_str())); // Drop trailing '\0's @@ -257,7 +257,7 @@ bool WMSettings::MigrateSettings() DWORD version = QueryValue(SettingsKey::SETTINGS_VERSION); if (version > CURRENT_SETTINGS_VERSION) { WMLog::GetInstance().LogError( - L"Settings version %d is newer than application supports (%d). " + L"Settings version {} is newer than application supports ({}). " L"Some settings may not work correctly.", version, CURRENT_SETTINGS_VERSION); } else if (version == 0) { @@ -421,8 +421,8 @@ bool WMSettings::IsAutostartEnabled() log.LogWarning( L"Registry entry for auto start has wrong executable " L"path." - L"Current path \"%s\". Path in registry \"%s\"", - wmPath, autostartPathFromReg.c_str()); + L"Current path \"{}\". Path in registry \"{}\"", + wmPath, autostartPathFromReg); } else { isEnabled = true; } @@ -480,7 +480,7 @@ DWORD WMSettings::QueryValue(SettingsKey key) const if (regError != ERROR_FILE_NOT_FOUND) { ShowWindowsError(L"RegQueryValueExW", regError); WMLog& log = WMLog::GetInstance(); - log.LogError(L"[Registry] Failed to query value for key \"%s\"", + log.LogError(L"[Registry] Failed to query value for key \"{}\"", keyStr); } return GetDefaultSetting(key); @@ -735,7 +735,7 @@ std::vector> WMSettings::GetQuietHoursTimes() if (numEntries % 2 != 0) { WMLog::GetInstance().LogError( L"Corrupted Quiet Hours times in registry: odd number of entries " - L"(%d). Resetting QuietHours", + L"({}). Resetting QuietHours", numEntries); TaskDialog( nullptr, nullptr, PROGRAM_NAME, @@ -759,9 +759,9 @@ std::vector> WMSettings::GetQuietHoursTimes() reinterpret_cast(&timeEntry.first), &size); if (regError != ERROR_SUCCESS) { WMLog::GetInstance().LogError( - L"Failed to read entry \"%s\" while initializing quiet hours " - L"(%d)", - keyStart.c_str(), regError); + L"Failed to read entry \"{}\" while initializing quiet hours " + L"({})", + keyStart, regError); continue; } @@ -771,9 +771,9 @@ std::vector> WMSettings::GetQuietHoursTimes() reinterpret_cast(&timeEntry.second), &size); if (regError != ERROR_SUCCESS) { WMLog::GetInstance().LogError( - L"Failed to read entry \"%s\" while initializing quiet hours " - L"(%d)", - keyStart.c_str(), regError); + L"Failed to read entry \"{}\" while initializing quiet hours " + L"({})", + keyEnd, regError); continue; } times.push_back(std::move(timeEntry)); diff --git a/WinMute/WMi18n.cpp b/WinMute/WMi18n.cpp index f4aa480..7f79b2e 100644 --- a/WinMute/WMi18n.cpp +++ b/WinMute/WMi18n.cpp @@ -92,7 +92,8 @@ std::vector WMi18n::GetAvailableLanguages() const langDlls.push_back(langMod); } } catch (const nlohmann::json::parse_error &pe) { - WMLog::GetInstance().LogError(L"Failed to parse language file \"%ls\": %S", wfd.cFileName, pe.what()); + WMLog::GetInstance().LogError(L"Failed to parse language file \"{}\": {}", + wfd.cFileName, ConvertStringToWideString(pe.what())); } } while (FindNextFileW(hFindFile, &wfd)); } @@ -136,7 +137,7 @@ bool WMi18n::LoadLanguage(const std::wstring &fileName, TranslationMap &strings) const fs::path loadFilePath{ fileName}; *langFilePath /= loadFilePath.filename(); // Sanitize if (!fs::exists(*langFilePath)) { - log.LogError(L"Language module \"%ls\" does not exist", langFilePath->c_str()); + log.LogError(L"Language module \"{}\" does not exist", langFilePath->wstring()); return false; } try { @@ -145,19 +146,22 @@ bool WMi18n::LoadLanguage(const std::wstring &fileName, TranslationMap &strings) TranslationMap translations_temp; for (auto it = json_data.begin(); it != json_data.end(); ++it) { if (it->is_structured()) { - log.LogError(L"Language module \"%ls\" has nested elements", langFilePath->c_str()); + log.LogError(L"Language module \"{}\" has nested elements", langFilePath->wstring()); return false; } if (it.value() == L"") { - log.LogInfo(L"No translation for \"%S\" found in language file", it.key().c_str()); + log.LogInfo(L"No translation for \"{}\" found in language file", + ConvertStringToWideString(it.key())); } else { const auto value = ConvertStringToWideString(it.value()); if (value == L"") { - log.LogError(L"Unable to convert language element \"%S\"", it.key().c_str()); + log.LogError(L"Unable to convert language element \"{}\"", + ConvertStringToWideString(it.key())); return false; } if (translations_temp.contains(it.key())) { - log.LogError(L"Double entry for language key \"%S\" found.", it.key().c_str()); + log.LogError(L"Double entry for language key \"{}\" found.", + ConvertStringToWideString(it.key())); return false; } translations_temp[it.key()] = value; @@ -166,9 +170,9 @@ bool WMi18n::LoadLanguage(const std::wstring &fileName, TranslationMap &strings) strings = std::move(translations_temp); } catch (const nlohmann::json::parse_error &pe) { log.LogError( - L"Failed to parse language file \"%ls\": %S", - langFilePath->filename().c_str(), - pe.what()); + L"Failed to parse language file \"{}\": {}", + langFilePath->filename().wstring(), + ConvertStringToWideString(pe.what())); return false; } return true; @@ -185,8 +189,8 @@ bool WMi18n::LoadLanguage(const std::wstring &fileName) TranslationMap new_lang; if (!LoadLanguage(fileName, new_lang)) { WMLog::GetInstance().LogError( - L"Failed to load language \"%ls\"", - fileName.c_str()); + L"Failed to load language \"{}\"", + fileName); return false; } else { UnloadLanguage(); diff --git a/WinMute/WinMute.cpp b/WinMute/WinMute.cpp index 154bd04..0c5f414 100644 --- a/WinMute/WinMute.cpp +++ b/WinMute/WinMute.cpp @@ -284,25 +284,25 @@ bool WinMute::LoadSettings() if (log.IsLogFileEnabled()) { std::wstring versionNumber; GetWinMuteVersion(versionNumber); - log.LogInfo(L"Starting WinMute %s", versionNumber.c_str()); + log.LogInfo(L"Starting WinMute {}", versionNumber); log.LogInfo(L"Loading settings:"); - log.LogInfo(L"Check for updates: %s", settings_.QueryValue(SettingsKey::CHECK_FOR_UPDATE) ? L"Yes" : L"No"); - log.LogInfo(L"\tCheck for beta updates: %s", settings_.QueryValue(SettingsKey::CHECK_FOR_BETA_UPDATE) ? L"Yes" : L"No"); - log.LogInfo(L"\tRestore volume: %s", settings_.QueryValue(SettingsKey::RESTORE_AUDIO) ? L"Yes" : L"No"); - log.LogInfo(L"\tMute delay: %d", settings_.QueryValue(SettingsKey::MUTE_DELAY)); - log.LogInfo(L"\tMute on lock: %s", settings_.QueryValue(SettingsKey::MUTE_ON_LOCK) ? L"Yes" : L"No"); - log.LogInfo(L"\tMute on display standby: %s", settings_.QueryValue(SettingsKey::MUTE_ON_DISPLAYSTANDBY) ? L"Yes" : L"No"); - log.LogInfo(L"\tMute on logout: %s", settings_.QueryValue(SettingsKey::MUTE_ON_LOGOUT) ? L"Yes" : L"No"); - log.LogInfo(L"\tMute on suspend: %s", settings_.QueryValue(SettingsKey::MUTE_ON_SUSPEND) ? L"Yes" : L"No"); - log.LogInfo(L"\tMute on shutdown: %s", settings_.QueryValue(SettingsKey::MUTE_ON_SHUTDOWN) ? L"Yes" : L"No"); - log.LogInfo(L"\tShow notifications: %s", settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED) ? L"Yes" : L"No"); - log.LogInfo(L"\tMute on bluetooth: %s", settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH) ? L"Yes" : L"No"); - log.LogInfo(L"\tTry pausing media when muting: %s", settings_.QueryValue(SettingsKey::MUTE_TRY_PAUSE_MEDIA) ? L"Yes" : L"No"); - log.LogInfo(L"\tTry resuming media when unmuting: %s", settings_.QueryValue(SettingsKey::MUTE_TRY_RESUME_MEDIA) ? L"Yes" : L"No"); - log.LogInfo(L"\t\tUse devicelist: %s", settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH_DEVICELIST) ? L"Yes" : L"No"); - log.LogInfo(L"\tMute on WLAN: %s", settings_.QueryValue(SettingsKey::MUTE_ON_WLAN) ? L"Yes" : L"No"); - log.LogInfo(L"\t\tUse allowlist: %s", settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST) ? L"Yes" : L"No"); - log.LogInfo(L"\tMute specific endpoints only: %s", settings_.QueryValue(SettingsKey::MUTE_INDIVIDUAL_ENDPOINTS) ? L"Yes" : L"No"); + log.LogInfo(L"Check for updates: {}", settings_.QueryValue(SettingsKey::CHECK_FOR_UPDATE) ? L"Yes" : L"No"); + log.LogInfo(L"\tCheck for beta updates: {}", settings_.QueryValue(SettingsKey::CHECK_FOR_BETA_UPDATE) ? L"Yes" : L"No"); + log.LogInfo(L"\tRestore volume: {}", settings_.QueryValue(SettingsKey::RESTORE_AUDIO) ? L"Yes" : L"No"); + log.LogInfo(L"\tMute delay: {}", settings_.QueryValue(SettingsKey::MUTE_DELAY)); + log.LogInfo(L"\tMute on lock: {}", settings_.QueryValue(SettingsKey::MUTE_ON_LOCK) ? L"Yes" : L"No"); + log.LogInfo(L"\tMute on display standby: {}", settings_.QueryValue(SettingsKey::MUTE_ON_DISPLAYSTANDBY) ? L"Yes" : L"No"); + log.LogInfo(L"\tMute on logout: {}", settings_.QueryValue(SettingsKey::MUTE_ON_LOGOUT) ? L"Yes" : L"No"); + log.LogInfo(L"\tMute on suspend: {}", settings_.QueryValue(SettingsKey::MUTE_ON_SUSPEND) ? L"Yes" : L"No"); + log.LogInfo(L"\tMute on shutdown: {}", settings_.QueryValue(SettingsKey::MUTE_ON_SHUTDOWN) ? L"Yes" : L"No"); + log.LogInfo(L"\tShow notifications: {}", settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED) ? L"Yes" : L"No"); + log.LogInfo(L"\tMute on bluetooth: {}", settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH) ? L"Yes" : L"No"); + log.LogInfo(L"\tTry pausing media when muting: {}", settings_.QueryValue(SettingsKey::MUTE_TRY_PAUSE_MEDIA) ? L"Yes" : L"No"); + log.LogInfo(L"\tTry resuming media when unmuting: {}", settings_.QueryValue(SettingsKey::MUTE_TRY_RESUME_MEDIA) ? L"Yes" : L"No"); + log.LogInfo(L"\t\tUse devicelist: {}", settings_.QueryValue(SettingsKey::MUTE_ON_BLUETOOTH_DEVICELIST) ? L"Yes" : L"No"); + log.LogInfo(L"\tMute on WLAN: {}", settings_.QueryValue(SettingsKey::MUTE_ON_WLAN) ? L"Yes" : L"No"); + log.LogInfo(L"\t\tUse allowlist: {}", settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST) ? L"Yes" : L"No"); + log.LogInfo(L"\tMute specific endpoints only: {}", settings_.QueryValue(SettingsKey::MUTE_INDIVIDUAL_ENDPOINTS) ? L"Yes" : L"No"); } if (!settings_.QueryValue(SettingsKey::MUTE_INDIVIDUAL_ENDPOINTS)) {