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 {