From ec330aea6930120288939c2663e5dee2d389abae Mon Sep 17 00:00:00 2001 From: CJK_mkp <113243675+CJKmkp@users.noreply.github.com> Date: Sun, 29 Jun 2025 12:40:15 +0800 Subject: [PATCH] =?UTF-8?q?improve:=E6=94=B9=E8=BF=9B=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Helpers/LogHelper.cs | 70 ++++++++++++++++++- Ink Canvas/MainWindow.xaml | 11 ++- Ink Canvas/MainWindow_cs/MW_Settings.cs | 6 ++ Ink Canvas/MainWindow_cs/MW_SettingsToLoad.cs | 29 +++----- Ink Canvas/Resources/Settings.cs | 3 + 5 files changed, 96 insertions(+), 23 deletions(-) diff --git a/Ink Canvas/Helpers/LogHelper.cs b/Ink Canvas/Helpers/LogHelper.cs index a2c6ce66..b365636b 100644 --- a/Ink Canvas/Helpers/LogHelper.cs +++ b/Ink Canvas/Helpers/LogHelper.cs @@ -8,6 +8,9 @@ namespace Ink_Canvas.Helpers class LogHelper { public static string LogFile = "Log.txt"; + private static string LogsFolder = "Logs"; + private static string AppStartTime = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); + private static readonly long MaxLogsFolderSizeBytes = 5 * 1024 * 1024; // 5MB public static void NewLog(string str) { @@ -28,14 +31,40 @@ namespace Ink_Canvas.Helpers public static void WriteLogToFile(string str, LogType logType = LogType.Info) { + // 检查日志是否启用 + if (MainWindow.Settings != null && MainWindow.Settings.Advanced != null && !MainWindow.Settings.Advanced.IsLogEnabled) return; + string strLogType = logType.ToString(); try { - var file = App.RootPath + LogFile; + string file; + + // 检查是否启用了日期保存功能 + if (MainWindow.Settings != null && MainWindow.Settings.Advanced != null && MainWindow.Settings.Advanced.IsSaveLogByDate) + { + // 确保Logs文件夹存在 + string logsPath = Path.Combine(App.RootPath, LogsFolder); + if (!Directory.Exists(logsPath)) + { + Directory.CreateDirectory(logsPath); + } + + // 检查Logs文件夹大小,如果超过5MB则清空 + CheckAndCleanLogsFolder(logsPath); + + // 使用软件启动时间作为日志文件名 + file = Path.Combine(logsPath, $"Log_{AppStartTime}.txt"); + } + else + { + file = App.RootPath + LogFile; + } + if (!Directory.Exists(App.RootPath)) { Directory.CreateDirectory(App.RootPath); } + var threadId = Thread.CurrentThread.ManagedThreadId; var callingMethod = new StackTrace(2, true).GetFrame(0); string callerInfo = ""; @@ -57,6 +86,45 @@ namespace Ink_Canvas.Helpers catch { } } + private static void CheckAndCleanLogsFolder(string logsPath) + { + try + { + long totalSize = 0; + DirectoryInfo dirInfo = new DirectoryInfo(logsPath); + + // 如果目录不存在,直接返回 + if (!dirInfo.Exists) return; + + // 计算文件夹大小 + foreach (FileInfo file in dirInfo.GetFiles()) + { + totalSize += file.Length; + } + + // 如果超过5MB,清空文件夹 + if (totalSize > MaxLogsFolderSizeBytes) + { + foreach (FileInfo file in dirInfo.GetFiles()) + { + try + { + file.Delete(); + } + catch { } + } + + // 记录清理操作 + string cleanupMessage = $"Logs folder exceeded size limit ({totalSize / 1024.0 / 1024.0:F2} MB > {MaxLogsFolderSizeBytes / 1024.0 / 1024.0:F2} MB). Folder cleaned."; + using (StreamWriter sw = new StreamWriter(Path.Combine(logsPath, $"Log_{AppStartTime}.txt"), true)) + { + sw.WriteLine($"{DateTime.Now:O} [Cleanup] {cleanupMessage}"); + } + } + } + catch { } + } + internal static void WriteLogToFile(string v, object warning) { WriteLogToFile($"[Warning] {v}", LogType.Warning); diff --git a/Ink Canvas/MainWindow.xaml b/Ink Canvas/MainWindow.xaml index 437ab176..593d449a 100644 --- a/Ink Canvas/MainWindow.xaml +++ b/Ink Canvas/MainWindow.xaml @@ -1693,7 +1693,14 @@ IsOn="True" FontFamily="Microsoft YaHei UI" FontWeight="Bold" Toggled="ToggleSwitchIsLogEnabled_Toggled" /> - + + + + + @@ -3641,7 +3648,7 @@ TickFrequency="1" TickPlacement="None" ValueChanged="HighlighterWidthSlider_ValueChanged" /> diff --git a/Ink Canvas/MainWindow_cs/MW_Settings.cs b/Ink Canvas/MainWindow_cs/MW_Settings.cs index e19807b1..982e7702 100644 --- a/Ink Canvas/MainWindow_cs/MW_Settings.cs +++ b/Ink Canvas/MainWindow_cs/MW_Settings.cs @@ -1724,6 +1724,12 @@ namespace Ink_Canvas { Settings.Advanced.IsLogEnabled = ToggleSwitchIsLogEnabled.IsOn; SaveSettingsToFile(); } + + private void ToggleSwitchIsSaveLogByDate_Toggled(object sender, RoutedEventArgs e) { + if (!isLoaded) return; + Settings.Advanced.IsSaveLogByDate = ToggleSwitchIsSaveLogByDate.IsOn; + SaveSettingsToFile(); + } private void ToggleSwitchIsSecondConfimeWhenShutdownApp_Toggled(object sender, RoutedEventArgs e) { if (!isLoaded) return; diff --git a/Ink Canvas/MainWindow_cs/MW_SettingsToLoad.cs b/Ink Canvas/MainWindow_cs/MW_SettingsToLoad.cs index 338888d2..77f45dd9 100644 --- a/Ink Canvas/MainWindow_cs/MW_SettingsToLoad.cs +++ b/Ink Canvas/MainWindow_cs/MW_SettingsToLoad.cs @@ -547,41 +547,30 @@ namespace Ink_Canvas { FingerModeBoundsWidthSlider.Value = Settings.Advanced.FingerModeBoundsWidth; NibModeBoundsWidthSlider.Value = Settings.Advanced.NibModeBoundsWidth; ToggleSwitchIsLogEnabled.IsOn = Settings.Advanced.IsLogEnabled; - + ToggleSwitchIsSaveLogByDate.IsOn = Settings.Advanced.IsSaveLogByDate; ToggleSwitchIsSecondConfimeWhenShutdownApp.IsOn = Settings.Advanced.IsSecondConfirmWhenShutdownApp; - - ToggleSwitchEraserBindTouchMultiplier.IsOn = Settings.Advanced.EraserBindTouchMultiplier; - ToggleSwitchIsSpecialScreen.IsOn = Settings.Advanced.IsSpecialScreen; - - TouchMultiplierSlider.Visibility = - ToggleSwitchIsSpecialScreen.IsOn ? Visibility.Visible : Visibility.Collapsed; - ToggleSwitchIsQuadIR.IsOn = Settings.Advanced.IsQuadIR; - + ToggleSwitchEraserBindTouchMultiplier.IsOn = Settings.Advanced.EraserBindTouchMultiplier; ToggleSwitchIsEnableFullScreenHelper.IsOn = Settings.Advanced.IsEnableFullScreenHelper; + ToggleSwitchIsEnableEdgeGestureUtil.IsOn = Settings.Advanced.IsEnableEdgeGestureUtil; + ToggleSwitchIsEnableForceFullScreen.IsOn = Settings.Advanced.IsEnableForceFullScreen; + ToggleSwitchIsEnableResolutionChangeDetection.IsOn = Settings.Advanced.IsEnableResolutionChangeDetection; + ToggleSwitchIsEnableDPIChangeDetection.IsOn = Settings.Advanced.IsEnableDPIChangeDetection; + ToggleSwitchIsEnableAvoidFullScreenHelper.IsOn = Settings.Advanced.IsEnableAvoidFullScreenHelper; if (Settings.Advanced.IsEnableFullScreenHelper) { FullScreenHelper.MarkFullscreenWindowTaskbarList(new WindowInteropHelper(this).Handle, true); } - - ToggleSwitchIsEnableAvoidFullScreenHelper.IsOn = Settings.Advanced.IsEnableAvoidFullScreenHelper; if (Settings.Advanced.IsEnableAvoidFullScreenHelper) { AvoidFullScreenHelper.StartAvoidFullScreen(this); } - - ToggleSwitchIsEnableEdgeGestureUtil.IsOn = Settings.Advanced.IsEnableEdgeGestureUtil; if (Settings.Advanced.IsEnableEdgeGestureUtil) { if (OSVersion.GetOperatingSystem() >= OperatingSystem.Windows10) EdgeGestureUtil.DisableEdgeGestures(new WindowInteropHelper(this).Handle, true); } - - ToggleSwitchIsEnableForceFullScreen.IsOn = Settings.Advanced.IsEnableForceFullScreen; - - ToggleSwitchIsEnableDPIChangeDetection.IsOn = Settings.Advanced.IsEnableDPIChangeDetection; - - ToggleSwitchIsEnableResolutionChangeDetection.IsOn = - Settings.Advanced.IsEnableResolutionChangeDetection; + TouchMultiplierSlider.Visibility = + ToggleSwitchIsSpecialScreen.IsOn ? Visibility.Visible : Visibility.Collapsed; } else { Settings.Advanced = new Advanced(); } diff --git a/Ink Canvas/Resources/Settings.cs b/Ink Canvas/Resources/Settings.cs index 51ebfcd0..522e90f1 100644 --- a/Ink Canvas/Resources/Settings.cs +++ b/Ink Canvas/Resources/Settings.cs @@ -381,6 +381,9 @@ namespace Ink_Canvas [JsonProperty("isLogEnabled")] public bool IsLogEnabled { get; set; } = true; + + [JsonProperty("isSaveLogByDate")] + public bool IsSaveLogByDate { get; set; } = true; [JsonProperty("isEnableFullScreenHelper")] public bool IsEnableFullScreenHelper { get; set; } = false;