From ad9bd455eb6e75f5aa64f6d246eb98026e1a7757 Mon Sep 17 00:00:00 2001 From: lx-s Date: Tue, 28 Jan 2025 08:54:28 +0100 Subject: [PATCH] Prepare for new log facilities --- Dist/bin/changelog.txt | 1 + WinMute/LogDialog.cpp | 118 ++++++++++++++++++++++++++++++++ WinMute/Settings_GeneralDlg.cpp | 2 +- WinMute/WMLog.cpp | 8 ++- WinMute/WMLog.h | 19 ++++- WinMute/WinMute.cpp | 4 +- WinMute/WinMute.rc | 22 ++++++ WinMute/WinMute.vcxproj | 9 +++ WinMute/WinMute.vcxproj.filters | 3 + WinMute/resource.h | 7 +- 10 files changed, 185 insertions(+), 8 deletions(-) create mode 100644 WinMute/LogDialog.cpp diff --git a/Dist/bin/changelog.txt b/Dist/bin/changelog.txt index eda95dd..bdc7b1e 100644 --- a/Dist/bin/changelog.txt +++ b/Dist/bin/changelog.txt @@ -1,5 +1,6 @@ Version 2.5.1 (2024-05-03) * Added new language: Simplified Chinese (thanks to https://translate.codeberg.org/user/richzhl/) + * Updated translations (thanks to all contributors!) Version 2.5.0.1 (2024-02-26) * Fixed update-check file for chocolatey package manager diff --git a/WinMute/LogDialog.cpp b/WinMute/LogDialog.cpp new file mode 100644 index 0000000..3a77cbf --- /dev/null +++ b/WinMute/LogDialog.cpp @@ -0,0 +1,118 @@ +/* + WinMute + Copyright (c) 2024, Alexander Steinhoefer + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + +#include "common.h" + +static void TranslateLogDlgProc(HWND hDlg) +{ + WMi18n &i18n = WMi18n::GetInstance(); + SetWindowText(hDlg, i18n.GetTranslationW("log.title").c_str()); +} + +INT_PTR CALLBACK AboutDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) +{ + AboutDlgData *dlgData = + reinterpret_cast(GetWindowLongPtr(hDlg, GWLP_USERDATA)); + switch (msg) { + case WM_INITDIALOG: + { + dlgData = new AboutDlgData(); + SetWindowLongPtr(hDlg, GWLP_USERDATA, reinterpret_cast(dlgData)); + + dlgData->hTabCtrl = GetDlgItem(hDlg, IDC_ABOUT_TAB); + + WMi18n &i18n = WMi18n::GetInstance(); + InsertTabItem(dlgData->hTabCtrl, ABOUT_TAB_GENERAL, i18n.GetTranslationW("about.tab.winmute")); + InsertTabItem(dlgData->hTabCtrl, ABOUT_TAB_LICENSE, i18n.GetTranslationW("about.tab.license")); + + TranslateAboutDlgProc(hDlg); + + dlgData->hTabs[ABOUT_TAB_GENERAL] = CreateDialog( + nullptr, + MAKEINTRESOURCE(IDD_ABOUT_WINMUTE), + hDlg, + About_GeneralDlgProc); + dlgData->hTabs[ABOUT_TAB_LICENSE] = CreateDialog( + nullptr, + MAKEINTRESOURCE(IDD_ABOUT_LICENSE), + hDlg, + About_LicenseDlgProc); + + + // Set title font + LOGFONT font; + font.lfHeight = 32; + font.lfWidth = 0; + font.lfEscapement = 0; + font.lfOrientation = 0; + font.lfWeight = FW_BOLD; + font.lfItalic = false; + font.lfUnderline = false; + font.lfStrikeOut = false; + font.lfEscapement = 0; + font.lfOrientation = 0; + font.lfOutPrecision = OUT_DEFAULT_PRECIS; + font.lfClipPrecision = CLIP_STROKE_PRECIS | CLIP_MASK | CLIP_TT_ALWAYS | CLIP_LH_ANGLES; + font.lfQuality = CLEARTYPE_QUALITY; + font.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN; + wcscpy_s(font.lfFaceName, L"Segoe UI"); + + dlgData->hTitleFont = CreateFontIndirect(&font); + SendMessage( + hTitle, + WM_SETFONT, + reinterpret_cast(dlgData->hTitleFont), + TRUE); + + HICON hIcon = LoadIcon( + GetModuleHandle(nullptr), + MAKEINTRESOURCE(IDI_TRAY_DARK)); + SendMessageW(hDlg, WM_SETICON, ICON_BIG, reinterpret_cast(hIcon)); + + return TRUE; + } + case WM_COMMAND: + return 0; + case WM_DESTROY: + DeleteObject(dlgData->hTitleFont); + delete dlgData; + SetWindowLongPtrW(hDlg, GWLP_USERDATA, 0); + return 0; + case WM_CLOSE: + EndDialog(hDlg, 0); + return TRUE; + default: + break; + } + return FALSE; +} diff --git a/WinMute/Settings_GeneralDlg.cpp b/WinMute/Settings_GeneralDlg.cpp index 0957f59..bceb468 100644 --- a/WinMute/Settings_GeneralDlg.cpp +++ b/WinMute/Settings_GeneralDlg.cpp @@ -199,7 +199,7 @@ INT_PTR CALLBACK Settings_GeneralDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPA const int enableLog = Button_GetCheck(hLogging) == BST_CHECKED; dlgData->settings->SetValue(SettingsKey::LOGGING_ENABLED, enableLog); - WMLog::GetInstance().SetEnabled(enableLog); + WMLog::GetInstance().EnableLogFile(enableLog); HWND hLanguageSelector = GetDlgItem(hDlg, IDC_LANGUAGE); const auto curLangSel = ComboBox_GetCurSel(hLanguageSelector); diff --git a/WinMute/WMLog.cpp b/WinMute/WMLog.cpp index da21380..4184567 100644 --- a/WinMute/WMLog.cpp +++ b/WinMute/WMLog.cpp @@ -77,7 +77,7 @@ void WMLog::DeleteLogFile() DeleteFileW(logFilePath.c_str()); } -void WMLog::SetEnabled(bool enable) +void WMLog::EnableLogFile(bool enable) { const std::lock_guard lock(logMutex_); if (enable == enabled_) { @@ -127,6 +127,12 @@ void WMLog::WriteMessage(const wchar_t* level, const wchar_t* msg) logFile_.flush(); } +std::vector WMLog::GetLogMessages() const +{ + const std::scoped_lock lock(logMutex_); + return logMessages_; +} + void WMLog::LogDebug(_In_z_ _Printf_format_string_ const wchar_t *fmt, ...) { if (!enabled_) { diff --git a/WinMute/WMLog.h b/WinMute/WMLog.h index 13c4d22..c59b79c 100644 --- a/WinMute/WMLog.h +++ b/WinMute/WMLog.h @@ -35,6 +35,18 @@ POSSIBILITY OF SUCH DAMAGE. #include "common.h" +enum class LogLevel { + Debug, + Info, + Error, +}; + +struct LogMessage { + LogLevel level; + std::string time; + std::string message; +}; + class WMLog { public: static WMLog& GetInstance(); @@ -44,10 +56,12 @@ public: void LogError(_In_z_ _Printf_format_string_ const wchar_t *fmt, ...); void LogWinError(const wchar_t *functionName, DWORD errorCode = -1); - void SetEnabled(bool enable); + void EnableLogFile(bool enable); bool IsEnabled() const; std::wstring GetLogFilePath(); + std::vector GetLogMessages() const; + private: WMLog(); ~WMLog(); @@ -57,7 +71,8 @@ private: void WriteMessage(const wchar_t *level, const wchar_t *msg); void DeleteLogFile(); - std::mutex logMutex_; + mutable std::mutex logMutex_; bool enabled_; std::wofstream logFile_; + std::vector logMessages_; }; diff --git a/WinMute/WinMute.cpp b/WinMute/WinMute.cpp index 1bdd332..f5f2479 100644 --- a/WinMute/WinMute.cpp +++ b/WinMute/WinMute.cpp @@ -205,9 +205,9 @@ bool WinMute::Init() hAppIcon_ = LoadIconW(hglobInstance, MAKEINTRESOURCE(IDI_APP)); #ifdef _DEBUG - WMLog::GetInstance().SetEnabled(true); + WMLog::GetInstance().EnableLogFile(true); #else - WMLog::GetInstance().SetEnabled(settings_.QueryValue(SettingsKey::LOGGING_ENABLED)); + WMLog::GetInstance().EnableLogFile(settings_.QueryValue(SettingsKey::LOGGING_ENABLED)); #endif log.LogInfo(L"Starting new session..."); diff --git a/WinMute/WinMute.rc b/WinMute/WinMute.rc index d6b7063..ffa859d 100644 --- a/WinMute/WinMute.rc +++ b/WinMute/WinMute.rc @@ -245,6 +245,14 @@ BEGIN PUSHBUTTON "Cancel",IDCANCEL,144,41,50,14 END +IDD_LOG DIALOGEX 0, 0, 307, 176 +STYLE DS_SETFONT | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "WinMute Log" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + EDITTEXT IDC_LOG_CONTENT,0,0,306,174,ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | NOT WS_BORDER | WS_VSCROLL +END + ///////////////////////////////////////////////////////////////////////////// // @@ -357,6 +365,14 @@ BEGIN TOPMARGIN, 7 BOTTOMMARGIN, 55 END + + IDD_LOG, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 300 + TOPMARGIN, 7 + BOTTOMMARGIN, 168 + END END #endif // APSTUDIO_INVOKED @@ -432,6 +448,12 @@ BEGIN 0 END +IDD_LOG AFX_DIALOG_LAYOUT +BEGIN + 0, + 0, 0, 100, 100 +END + ///////////////////////////////////////////////////////////////////////////// // diff --git a/WinMute/WinMute.vcxproj b/WinMute/WinMute.vcxproj index 7e19b6e..6e9c400 100644 --- a/WinMute/WinMute.vcxproj +++ b/WinMute/WinMute.vcxproj @@ -250,6 +250,7 @@ Create Create + @@ -384,6 +385,14 @@ copy /Y %(FullPath) $(OutputPath)lang Copy translation "%(Filename)" to build directory $(OutputPath)lang\%(Filename)%(Extension) $(OutputPath)lang\%(Filename)%(Extension) + mkdir $(OutputPath)lang >nul 2>&1 +copy /Y %(FullPath) $(OutputPath)lang + mkdir $(OutputPath)lang >nul 2>&1 +copy /Y %(FullPath) $(OutputPath)lang + Copy translation "%(Filename)" to build directory + Copy translation "%(Filename)" to build directory + $(OutputPath)lang\%(Filename)%(Extension) + $(OutputPath)lang\%(Filename)%(Extension) diff --git a/WinMute/WinMute.vcxproj.filters b/WinMute/WinMute.vcxproj.filters index 83e56e3..a5b7dbf 100644 --- a/WinMute/WinMute.vcxproj.filters +++ b/WinMute/WinMute.vcxproj.filters @@ -207,6 +207,9 @@ Source Files\UI\Dialogs\Settings + + Source Files\UI\Dialogs + diff --git a/WinMute/resource.h b/WinMute/resource.h index a616167..bbffead 100644 --- a/WinMute/resource.h +++ b/WinMute/resource.h @@ -25,6 +25,7 @@ #define IDD_MANAGE_ENDPOINTS_ADD 137 #define IDI_APP_UPDATE 141 #define IDI_APPUPDATE 141 +#define IDD_LOG 142 #define IDC_LINK_HOMEPAGE 1001 #define IDC_LINK_SUPPORT 1002 #define IDC_LINK_PROJECT 1002 @@ -113,6 +114,8 @@ #define IDC_UPDATES_VIA_PACKAGE_MANAGER_NOTICE 1072 #define IDC_UPDATE_OPTIONS_DISABLED 1072 #define IDC_LINK_HELP_TRANSLATING 1073 +#define IDC_EDIT1 1075 +#define IDC_LOG_CONTENT 1075 #define ID_TRAYMENU_INFO 40001 #define ID_TRAYMENU_ 40002 #define ID_TRAYMENU_MUTEON 40003 @@ -158,9 +161,9 @@ // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 142 +#define _APS_NEXT_RESOURCE_VALUE 144 #define _APS_NEXT_COMMAND_VALUE 40041 -#define _APS_NEXT_CONTROL_VALUE 1074 +#define _APS_NEXT_CONTROL_VALUE 1076 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif -- 2.43.0