Skip to content

Commit

Permalink
[FancyZones Editor] Logger (#13928)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeraphimaZykova committed Oct 25, 2021
1 parent a93dc42 commit 91ed50d
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 206 deletions.
156 changes: 13 additions & 143 deletions src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using FancyZonesEditor.Logs;
using FancyZonesEditor.Utils;
using ManagedCommon;
using Microsoft.PowerToys.Common.UI;
Expand All @@ -25,26 +21,7 @@ namespace FancyZonesEditor
public partial class App : Application, IDisposable
{
// Non-localizable strings
private const string CrashReportLogFile = "FZEditorCrashLog.txt";
private const string ErrorReportLogFile = "FZEditorErrorLog.txt";
private const string PowerToysIssuesURL = "https://aka.ms/powerToysReportBug";

private const string CrashReportExceptionTag = "Exception";
private const string CrashReportSourceTag = "Source: ";
private const string CrashReportTargetAssemblyTag = "TargetAssembly: ";
private const string CrashReportTargetModuleTag = "TargetModule: ";
private const string CrashReportTargetSiteTag = "TargetSite: ";
private const string CrashReportEnvironmentTag = "Environment";
private const string CrashReportCommandLineTag = "* Command Line: ";
private const string CrashReportTimestampTag = "* Timestamp: ";
private const string CrashReportOSVersionTag = "* OS Version: ";
private const string CrashReportIntPtrLengthTag = "* IntPtr Length: ";
private const string CrashReportx64Tag = "* x64: ";
private const string CrashReportCLRVersionTag = "* CLR Version: ";
private const string CrashReportAssembliesTag = "Assemblies - ";
private const string CrashReportDynamicAssemblyTag = "dynamic assembly doesn't have location";
private const string CrashReportLocationNullTag = "location is null or empty";

private const string ParsingErrorReportTag = "Settings parsing error";
private const string ParsingErrorDataTag = "Data: ";

Expand Down Expand Up @@ -89,9 +66,12 @@ public App()
_eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, interop.Constants.FZEExitEvent());
if (_eventHandle.WaitOne())
{
Logger.LogInfo("FancyZones disabled, exit");
Environment.Exit(0);
}
}).Start();

Logger.LogInfo("FancyZones Editor started");
}

private void OnStartup(object sender, StartupEventArgs e)
Expand All @@ -100,6 +80,7 @@ private void OnStartup(object sender, StartupEventArgs e)

RunnerHelper.WaitForPowerToysRunner(PowerToysPID, () =>
{
Logger.LogInfo("Runner exited");
Environment.Exit(0);
});

Expand Down Expand Up @@ -139,15 +120,7 @@ private void OnStartup(object sender, StartupEventArgs e)
// Error message if parsing failed
if (!parseResult.Result)
{
var sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("## " + ParsingErrorReportTag);
sb.AppendLine();
sb.AppendLine(parseResult.Message);
sb.AppendLine();
sb.AppendLine(ParsingErrorDataTag);
sb.AppendLine(parseResult.MalformedData);

Logger.LogError(ParsingErrorReportTag + ": " + parseResult.Message + "; " + ParsingErrorDataTag + ": " + parseResult.MalformedData);
MessageBox.Show(parseResult.Message, FancyZonesEditor.Properties.Resources.Error_Parsing_Zones_Settings_Title, MessageBoxButton.OK);
}

Expand All @@ -163,6 +136,8 @@ private void OnExit(object sender, ExitEventArgs e)
{
_eventHandle.Set();
}

Logger.LogInfo("FancyZones Editor exited");
}

public void App_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
Expand Down Expand Up @@ -197,126 +172,20 @@ public static void ShowExceptionMessageBox(string message, Exception exception =
MessageBox.Show(fullMessage, FancyZonesEditor.Properties.Resources.Error_Exception_Message_Box_Title);
}

public static void ShowExceptionReportMessageBox(string reportData)
{
var fileStream = File.OpenWrite(ErrorReportLogFile);
using (var sw = new StreamWriter(fileStream))
{
sw.Write(reportData);
sw.Flush();
}

fileStream.Close();

ShowReportMessageBox(fileStream.Name);
}

private void OnUnhandledException(object sender, UnhandledExceptionEventArgs args)
{
var fileStream = File.OpenWrite(CrashReportLogFile);
using (var sw = new StreamWriter(fileStream))
{
sw.Write(FormatException((Exception)args.ExceptionObject));
}

fileStream.Close();

ShowReportMessageBox(fileStream.Name);
Logger.LogError("Unhandled exception", (Exception)args.ExceptionObject);
ShowReportMessageBox();
}

private static void ShowReportMessageBox(string fileName)
private static void ShowReportMessageBox()
{
MessageBox.Show(
FancyZonesEditor.Properties.Resources.Crash_Report_Message_Box_Text_Part1 +
Path.GetFullPath(fileName) +
"\n" +
FancyZonesEditor.Properties.Resources.Crash_Report_Message_Box_Text_Part2 +
FancyZonesEditor.Properties.Resources.Crash_Report_Message_Box_Text +
PowerToysIssuesURL,
FancyZonesEditor.Properties.Resources.Fancy_Zones_Editor_App_Title);
}

private static string FormatException(Exception ex)
{
var sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("## " + CrashReportExceptionTag);
sb.AppendLine();
sb.AppendLine("```");

var exlist = new List<StringBuilder>();

while (ex != null)
{
var exsb = new StringBuilder();
exsb.Append(ex.GetType().FullName);
exsb.Append(": ");
exsb.AppendLine(ex.Message);
if (ex.Source != null)
{
exsb.Append(" " + CrashReportSourceTag);
exsb.AppendLine(ex.Source);
}

if (ex.TargetSite != null)
{
exsb.Append(" " + CrashReportTargetAssemblyTag);
exsb.AppendLine(ex.TargetSite.Module.Assembly.ToString());
exsb.Append(" " + CrashReportTargetModuleTag);
exsb.AppendLine(ex.TargetSite.Module.ToString());
exsb.Append(" " + CrashReportTargetSiteTag);
exsb.AppendLine(ex.TargetSite.ToString());
}

exsb.AppendLine(ex.StackTrace);
exlist.Add(exsb);

ex = ex.InnerException;
}

foreach (var result in exlist.Select(o => o.ToString()).Reverse())
{
sb.AppendLine(result);
}

sb.AppendLine("```");
sb.AppendLine();

sb.AppendLine("## " + CrashReportEnvironmentTag);
sb.AppendLine(CrashReportCommandLineTag + Environment.CommandLine);

// Using InvariantCulture since this is used for a timestamp internally
sb.AppendLine(CrashReportTimestampTag + DateTime.Now.ToString(CultureInfo.InvariantCulture));
sb.AppendLine(CrashReportOSVersionTag + Environment.OSVersion.VersionString);
sb.AppendLine(CrashReportIntPtrLengthTag + IntPtr.Size);
sb.AppendLine(CrashReportx64Tag + Environment.Is64BitOperatingSystem);
sb.AppendLine(CrashReportCLRVersionTag + Environment.Version);
sb.AppendLine("## " + CrashReportAssembliesTag + AppDomain.CurrentDomain.FriendlyName);
sb.AppendLine();
foreach (var ass in AppDomain.CurrentDomain.GetAssemblies().OrderBy(o => o.GlobalAssemblyCache ? 50 : 0))
{
sb.Append("* ");
sb.Append(ass.FullName);
sb.Append(" (");

if (ass.IsDynamic)
{
sb.Append(CrashReportDynamicAssemblyTag);
}
else if (string.IsNullOrEmpty(ass.Location))
{
sb.Append(CrashReportLocationNullTag);
}
else
{
sb.Append(ass.Location);
}

sb.AppendLine(")");
}

return sb.ToString();
}

protected virtual void Dispose(bool disposing)
{
if (!_isDisposed)
Expand All @@ -329,6 +198,7 @@ protected virtual void Dispose(bool disposing)
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
_isDisposed = true;
Logger.LogInfo("FancyZones Editor disposed");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Windows;
using System.Windows.Input;
using FancyZonesEditor.Logs;
using FancyZonesEditor.Models;

namespace FancyZonesEditor
Expand Down Expand Up @@ -34,11 +35,13 @@ public LayoutModel Model

private void OnAddZone(object sender, RoutedEventArgs e)
{
Logger.LogInfo("Add zone");
_model.AddZone();
}

protected new void OnCancel(object sender, RoutedEventArgs e)
{
Logger.LogInfo("Cancel changes");
base.OnCancel(sender, e);
_stashedModel.RestoreTo(_model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Windows;
using FancyZonesEditor.Logs;
using FancyZonesEditor.Models;

namespace FancyZonesEditor
Expand All @@ -12,6 +13,7 @@ public class EditorWindow : Window
{
protected void OnSaveApplyTemplate(object sender, RoutedEventArgs e)
{
Logger.LogTrace();
var mainEditor = App.Overlay;
if (mainEditor.CurrentDataContext is LayoutModel model)
{
Expand Down
6 changes: 6 additions & 0 deletions src/modules/fancyzones/editor/FancyZonesEditor/GridData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using FancyZonesEditor.Logs;
using FancyZonesEditor.Models;

namespace FancyZonesEditor
Expand Down Expand Up @@ -417,6 +418,8 @@ public List<int> MergeClosureIndices(List<int> indices)

public void DoMerge(List<int> indices)
{
Logger.LogTrace();

if (indices.Count == 0)
{
return;
Expand Down Expand Up @@ -455,6 +458,7 @@ public bool CanSplit(int zoneIndex, int position, Orientation orientation)

public void Split(int zoneIndex, int position, Orientation orientation)
{
Logger.LogTrace();
if (!CanSplit(zoneIndex, position, orientation))
{
return;
Expand Down Expand Up @@ -519,6 +523,8 @@ public bool CanDrag(int resizerIndex, int delta)

public void Drag(int resizerIndex, int delta)
{
Logger.LogTrace();

if (!CanDrag(resizerIndex, delta))
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using FancyZonesEditor.Logs;
using FancyZonesEditor.Models;

namespace FancyZonesEditor
Expand Down Expand Up @@ -316,6 +317,8 @@ private void SetupUI()

private void OnSplit(object sender, SplitEventArgs args)
{
Logger.LogTrace();

MergeCancelClick(null, null);

var zonePanel = sender as GridZone;
Expand Down Expand Up @@ -488,6 +491,7 @@ private void Resizer_DragCompleted(object sender, System.Windows.Controls.Primit

private void OnMergeComplete(object o, MouseButtonEventArgs e)
{
Logger.LogTrace();
_inMergeDrag = false;

var selectedIndices = new List<int>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using FancyZonesEditor.Logs;
using FancyZonesEditor.Models;

namespace FancyZonesEditor
Expand Down Expand Up @@ -83,6 +84,11 @@ private void OnLoaded(object sender, RoutedEventArgs e)
{
_model = (LayoutModel)DataContext;

if (_model != null)
{
Logger.LogInfo("Loaded " + _model.Name);
}

RenderPreview();
}

Expand Down

0 comments on commit 91ed50d

Please sign in to comment.