From d21b7fac7b3500d7af338641cfd9d7550cae3d11 Mon Sep 17 00:00:00 2001
From: Kai Tao <69313318+vanzue@users.noreply.github.com>
Date: Wed, 28 May 2025 18:53:35 +0800
Subject: [PATCH] Log: Clear mwb module interface old version log and clear
dotnet old version log (#39652)
* Initial plan for issue
* Add cleanup of old version log folders for Mouse Without Borders
Co-authored-by: vanzue <69313318+vanzue@users.noreply.github.com>
* Add try-catch block to cleanup_old_logs function
Co-authored-by: vanzue <69313318+vanzue@users.noreply.github.com>
* Refactor Mouse Without Borders log cleanup to use common mechanism
Co-authored-by: vanzue <69313318+vanzue@users.noreply.github.com>
* Investigate .NET logger cleanup mechanism for old version logs
Co-authored-by: vanzue <69313318+vanzue@users.noreply.github.com>
* delete in other thread
* clear mwb previous folder
* slow down the deletion
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
---
src/common/ManagedCommon/Logger.cs | 60 +++++++++++++++++--
.../ModuleInterface/dllmain.cpp | 10 +++-
2 files changed, 61 insertions(+), 9 deletions(-)
diff --git a/src/common/ManagedCommon/Logger.cs b/src/common/ManagedCommon/Logger.cs
index cd85d500e1..c034abb6e6 100644
--- a/src/common/ManagedCommon/Logger.cs
+++ b/src/common/ManagedCommon/Logger.cs
@@ -6,9 +6,10 @@ using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
+using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
-
+using System.Threading.Tasks;
using PowerToys.Interop;
namespace ManagedCommon
@@ -40,25 +41,72 @@ namespace ManagedCommon
/// If the process using Logger is a low-privilege process.
public static void InitializeLogger(string applicationLogPath, bool isLocalLow = false)
{
+ string basePath;
if (isLocalLow)
{
- applicationLogPath = Environment.GetEnvironmentVariable("userprofile") + "\\appdata\\LocalLow\\Microsoft\\PowerToys" + applicationLogPath + "\\" + Version;
+ basePath = Environment.GetEnvironmentVariable("userprofile") + "\\appdata\\LocalLow\\Microsoft\\PowerToys" + applicationLogPath;
}
else
{
- applicationLogPath = Constants.AppDataPath() + applicationLogPath + "\\" + Version;
+ basePath = Constants.AppDataPath() + applicationLogPath;
}
- if (!Directory.Exists(applicationLogPath))
+ string versionedPath = Path.Combine(basePath, Version);
+
+ if (!Directory.Exists(versionedPath))
{
- Directory.CreateDirectory(applicationLogPath);
+ Directory.CreateDirectory(versionedPath);
}
- var logFilePath = Path.Combine(applicationLogPath, "Log_" + DateTime.Now.ToString(@"yyyy-MM-dd", CultureInfo.InvariantCulture) + ".log");
+ var logFilePath = Path.Combine(versionedPath, "Log_" + DateTime.Now.ToString(@"yyyy-MM-dd", CultureInfo.InvariantCulture) + ".log");
Trace.Listeners.Add(new TextWriterTraceListener(logFilePath));
Trace.AutoFlush = true;
+
+ // Clean up old version log folders
+ Task.Run(() => DeleteOldVersionLogFolders(basePath, versionedPath));
+ }
+
+ ///
+ /// Deletes old version log folders, keeping only the current version's folder.
+ ///
+ /// The base path to the log files folder.
+ /// The path to the current version's log folder.
+ private static void DeleteOldVersionLogFolders(string basePath, string currentVersionPath)
+ {
+ try
+ {
+ if (!Directory.Exists(basePath))
+ {
+ return;
+ }
+
+ var dirs = Directory.GetDirectories(basePath)
+ .Select(d => new DirectoryInfo(d))
+ .OrderBy(d => d.CreationTime)
+ .Where(d => !string.Equals(d.FullName, currentVersionPath, StringComparison.OrdinalIgnoreCase))
+ .Take(3)
+ .ToList();
+
+ foreach (var directory in dirs)
+ {
+ try
+ {
+ Directory.Delete(directory.FullName, true);
+ LogInfo($"Deleted old log directory: {directory.FullName}");
+ Task.Delay(500).Wait();
+ }
+ catch (Exception ex)
+ {
+ LogError($"Failed to delete old log directory: {directory.FullName}", ex);
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ LogError("Error cleaning up old log folders", ex);
+ }
}
public static void LogError(string message, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
diff --git a/src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp b/src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp
index 6bb823453f..33030fbdfb 100644
--- a/src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp
+++ b/src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp
@@ -13,6 +13,7 @@
#include
#include
#include
+#include
HINSTANCE g_hInst_MouseWithoutBorders = 0;
@@ -409,9 +410,12 @@ public:
{
app_name = L"MouseWithoutBorders";
app_key = app_name;
- std::filesystem::path logFilePath(PTSettingsHelper::get_module_save_folder_location(app_key));
- logFilePath.append(LogSettings::mouseWithoutBordersLogPath);
- Logger::init(LogSettings::mouseWithoutBordersLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location());
+
+ LoggerHelpers::init_logger(app_key, L"ModuleInterface", LogSettings::mouseWithoutBordersLoggerName);
+
+ std::filesystem::path oldLogPath(PTSettingsHelper::get_module_save_folder_location(app_key));
+ oldLogPath.append("LogsModuleInterface");
+ LoggerHelpers::delete_old_log_folder(oldLogPath);
try
{