mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-23 19:49:43 +01:00
## Summary of the Pull Request This PR introduces a small ribbon to the CmdPal for app developers. The dev ribbon is dynamically added to the main window in local (non-CI) builds. It shows the number of logged errors and warnings, the current build configuration (Debug or Release), and whether it’s built with AOT. The flyout shows the latest errors and warnings and lets you quickly access the logs. ## Pictures? Pictures! <img width="985" height="589" alt="image" src="https://github.com/user-attachments/assets/6528b02b-b4b4-4968-91bf-e67a29f86415" /> <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #43318 <!-- - [ ] Closes: #yyy (add separate lines for additional resolved issues) --> - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed
222 lines
9.0 KiB
C#
222 lines
9.0 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
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
|
|
{
|
|
public static class Logger
|
|
{
|
|
private static readonly string Error = "Error";
|
|
private static readonly string Warning = "Warning";
|
|
private static readonly string Info = "Info";
|
|
#if DEBUG
|
|
private static readonly string Debug = "Debug";
|
|
#endif
|
|
private static readonly string TraceFlag = "Trace";
|
|
|
|
private static readonly string Version = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version ?? "Unknown";
|
|
|
|
/// <summary>
|
|
/// Gets the path to the log directory for the current version of the app.
|
|
/// </summary>
|
|
public static string CurrentVersionLogDirectoryPath { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the path to the current log file.
|
|
/// </summary>
|
|
public static string CurrentLogFile { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the path to the log directory for the app.
|
|
/// </summary>
|
|
public static string AppLogDirectoryPath { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Initializes the logger and sets the path for logging.
|
|
/// </summary>
|
|
/// <example>InitializeLogger("\\FancyZones\\Editor\\Logs")</example>
|
|
/// <param name="applicationLogPath">The path to the log files folder.</param>
|
|
/// <param name="isLocalLow">If the process using Logger is a low-privilege process.</param>
|
|
public static void InitializeLogger(string applicationLogPath, bool isLocalLow = false)
|
|
{
|
|
string versionedPath = LogDirectoryPath(applicationLogPath, isLocalLow);
|
|
string basePath = Path.GetDirectoryName(versionedPath);
|
|
|
|
if (!Directory.Exists(versionedPath))
|
|
{
|
|
Directory.CreateDirectory(versionedPath);
|
|
}
|
|
|
|
AppLogDirectoryPath = basePath;
|
|
CurrentVersionLogDirectoryPath = versionedPath;
|
|
|
|
var logFile = "Log_" + DateTime.Now.ToString(@"yyyy-MM-dd", CultureInfo.InvariantCulture) + ".log";
|
|
var logFilePath = Path.Combine(versionedPath, logFile);
|
|
CurrentLogFile = logFilePath;
|
|
|
|
Trace.Listeners.Add(new TextWriterTraceListener(logFilePath));
|
|
|
|
Trace.AutoFlush = true;
|
|
|
|
// Clean up old version log folders
|
|
Task.Run(() => DeleteOldVersionLogFolders(basePath, versionedPath));
|
|
}
|
|
|
|
public static string LogDirectoryPath(string applicationLogPath, bool isLocalLow = false)
|
|
{
|
|
string basePath;
|
|
if (isLocalLow)
|
|
{
|
|
basePath = Environment.GetEnvironmentVariable("userprofile") + "\\appdata\\LocalLow\\Microsoft\\PowerToys" + applicationLogPath;
|
|
}
|
|
else
|
|
{
|
|
basePath = Constants.AppDataPath() + applicationLogPath;
|
|
}
|
|
|
|
string versionedPath = Path.Combine(basePath, Version);
|
|
return versionedPath;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes old version log folders, keeping only the current version's folder.
|
|
/// </summary>
|
|
/// <param name="basePath">The base path to the log files folder.</param>
|
|
/// <param name="currentVersionPath">The path to the current version's log folder.</param>
|
|
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)
|
|
{
|
|
Log(message, Error, memberName, sourceFilePath, sourceLineNumber);
|
|
}
|
|
|
|
public static void LogError(string message, Exception ex, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
|
|
{
|
|
if (ex == null)
|
|
{
|
|
Log(message, Error, memberName, sourceFilePath, sourceLineNumber);
|
|
}
|
|
else
|
|
{
|
|
var exMessage =
|
|
message + Environment.NewLine +
|
|
ex.GetType() + " (" + ex.HResult + "): " + ex.Message + Environment.NewLine;
|
|
|
|
if (ex.InnerException != null)
|
|
{
|
|
exMessage +=
|
|
"Inner exception: " + Environment.NewLine +
|
|
ex.InnerException.GetType() + " (" + ex.InnerException.HResult + "): " + ex.InnerException.Message + Environment.NewLine;
|
|
}
|
|
|
|
exMessage +=
|
|
"Stack trace: " + Environment.NewLine +
|
|
ex.StackTrace;
|
|
|
|
Log(exMessage, Error, memberName, sourceFilePath, sourceLineNumber);
|
|
}
|
|
}
|
|
|
|
public static void LogWarning(string message, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
|
|
{
|
|
Log(message, Warning, memberName, sourceFilePath, sourceLineNumber);
|
|
}
|
|
|
|
public static void LogInfo(string message, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
|
|
{
|
|
Log(message, Info, memberName, sourceFilePath, sourceLineNumber);
|
|
}
|
|
|
|
public static void LogDebug(string message, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
|
|
{
|
|
#if DEBUG
|
|
Log(message, Debug, memberName, sourceFilePath, sourceLineNumber);
|
|
#endif
|
|
}
|
|
|
|
public static void LogTrace([System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
|
|
{
|
|
Log(string.Empty, TraceFlag, memberName, sourceFilePath, sourceLineNumber);
|
|
}
|
|
|
|
private static void Log(string message, string type, string memberName, string sourceFilePath, int sourceLineNumber)
|
|
{
|
|
Trace.WriteLine("[" + DateTime.Now.TimeOfDay + "] [" + type + "] " + GetCallerInfo(memberName, sourceFilePath, sourceLineNumber));
|
|
Trace.Indent();
|
|
if (message != string.Empty)
|
|
{
|
|
Trace.WriteLine(message);
|
|
}
|
|
|
|
Trace.Unindent();
|
|
}
|
|
|
|
private static string GetCallerInfo(string memberName, string sourceFilePath, int sourceLineNumber)
|
|
{
|
|
string callerFileName = "Unknown";
|
|
|
|
try
|
|
{
|
|
string fileName = Path.GetFileName(sourceFilePath);
|
|
if (!string.IsNullOrEmpty(fileName))
|
|
{
|
|
callerFileName = fileName;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
callerFileName = "Unknown";
|
|
#if DEBUG
|
|
throw;
|
|
#endif
|
|
}
|
|
|
|
return $"{callerFileName}::{memberName}::{sourceLineNumber}";
|
|
}
|
|
}
|
|
}
|