Skip to content

Commit

Permalink
Improved unhandled exception handling (now writer last log entries to…
Browse files Browse the repository at this point in the history
… crash dump file).
  • Loading branch information
LorenzCK committed Jan 31, 2014
1 parent 4bf6548 commit 9d97da1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 29 deletions.
2 changes: 1 addition & 1 deletion OnTopReplica/AspectRatioForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void SetAspectRatio(Size aspectRatioSource, bool forceRefresh) {
AspectRatio = ((double)aspectRatioSource.Width / (double)aspectRatioSource.Height);
_keepAspectRatio = true;

Log.Write("Setting new aspect ratio {0} (for {1})", AspectRatio, aspectRatioSource);
//Log.Write("Setting new aspect ratio {0} (for {1})", AspectRatio, aspectRatioSource);

if (forceRefresh) {
RefreshAspectRatio();
Expand Down
48 changes: 36 additions & 12 deletions OnTopReplica/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,52 @@ public static void WriteException(string message, Exception exception) {
WriteLines(message, exception.ToString());
}
else {
WriteLines(message, "(no last exception)");
WriteLines(message, "(No exception data.)");
}
}

private static void WriteLine(string message) {
if (Writer == null)
return;

var s = string.Format("{0,-8:HH:mm:ss} {1}", DateTime.Now, message);
Writer.WriteLine(s);
AddToQueue(s);

if (Writer != null) {
Writer.WriteLine(s);
}
}

private static void WriteLines(params string[] messages) {
if (Writer == null)
if (messages.Length <= 0)
return;

if (messages.Length > 0)
WriteLine(messages[0]);
if (messages.Length > 1) {
for (int i = 1; i < messages.Length; ++i) {
Writer.WriteLine(" {0}", messages[i]);
}
var sb = new StringBuilder();
sb.AppendFormat("{0,-8:HH:mm:ss} {1}", DateTime.Now, messages[0]);
for (int i = 1; i < messages.Length; ++i) {
sb.AppendLine();
sb.AppendFormat(" {0}", messages[i]);
}

AddToQueue(sb.ToString());

if (Writer != null) {
Writer.WriteLine(sb.ToString());
}
}

const int MaxQueueCapacity = 30;

private static Queue<string> _entriesQueue = new Queue<string>(MaxQueueCapacity);

private static void AddToQueue(string entry){
_entriesQueue.Enqueue(entry);

while(_entriesQueue.Count > MaxQueueCapacity){
_entriesQueue.Dequeue();
}
}

public static IEnumerable<string> Queue {
get {
return _entriesQueue;
}
}

Expand Down
11 changes: 10 additions & 1 deletion OnTopReplica/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public MainForm(Options startupOptions) {

//Set to Key event preview
this.KeyPreview = true;

Log.Write("Main form constructed");
}

#region Event override
Expand All @@ -77,17 +79,24 @@ protected override void OnHandleCreated(EventArgs e){
}

protected override void OnShown(EventArgs e) {
Log.Write("Main form shown");
base.OnShown(e);

//Apply startup options
_startupOptions.Apply(this);
}

protected override void OnClosing(CancelEventArgs e) {
Log.Write("Main form closing");
base.OnClosing(e);

_msgPumpManager.Dispose();
Program.Platform.CloseForm(this);
}

base.OnClosing(e);
protected override void OnClosed(EventArgs e) {
Log.Write("Main form closed");
base.OnClosed(e);
}

protected override void OnMove(EventArgs e) {
Expand Down
28 changes: 14 additions & 14 deletions OnTopReplica/OnTopReplica.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -523,72 +523,72 @@
<ItemGroup>
<PublishFile Include="Assets\icon.ico">
<Visible>False</Visible>
<PublishState>Exclude</PublishState>
<IncludeHash>True</IncludeHash>
<Group>
</Group>
<TargetPath>
</TargetPath>
<PublishState>Exclude</PublishState>
<IncludeHash>True</IncludeHash>
<FileType>File</FileType>
</PublishFile>
<PublishFile Include="Assets\screenshot-icon.ico">
<Visible>False</Visible>
<PublishState>Exclude</PublishState>
<IncludeHash>True</IncludeHash>
<Group>
</Group>
<TargetPath>
</TargetPath>
<PublishState>Exclude</PublishState>
<IncludeHash>True</IncludeHash>
<FileType>File</FileType>
</PublishFile>
<PublishFile Include="Assets\window_multiple16.ico">
<Visible>False</Visible>
<PublishState>Exclude</PublishState>
<IncludeHash>True</IncludeHash>
<Group>
</Group>
<TargetPath>
</TargetPath>
<PublishState>Exclude</PublishState>
<IncludeHash>True</IncludeHash>
<FileType>File</FileType>
</PublishFile>
<PublishFile Include="Assets\xiao_arrow.png">
<Visible>False</Visible>
<PublishState>Exclude</PublishState>
<IncludeHash>True</IncludeHash>
<Group>
</Group>
<TargetPath>
</TargetPath>
<PublishState>Exclude</PublishState>
<IncludeHash>True</IncludeHash>
<FileType>File</FileType>
</PublishFile>
<PublishFile Include="cs\OnTopReplica.resources">
<Visible>False</Visible>
<PublishState>Include</PublishState>
<IncludeHash>True</IncludeHash>
<Group>
</Group>
<TargetPath>
</TargetPath>
<PublishState>Include</PublishState>
<IncludeHash>True</IncludeHash>
<FileType>Satellite</FileType>
</PublishFile>
<PublishFile Include="da\OnTopReplica.resources">
<Visible>False</Visible>
<PublishState>Include</PublishState>
<IncludeHash>True</IncludeHash>
<Group>
</Group>
<TargetPath>
</TargetPath>
<PublishState>Include</PublishState>
<IncludeHash>True</IncludeHash>
<FileType>Satellite</FileType>
</PublishFile>
<PublishFile Include="it\OnTopReplica.resources">
<Visible>False</Visible>
<PublishState>Include</PublishState>
<IncludeHash>True</IncludeHash>
<Group>
</Group>
<TargetPath>
</TargetPath>
<PublishState>Include</PublishState>
<IncludeHash>True</IncludeHash>
<FileType>Satellite</FileType>
</PublishFile>
</ItemGroup>
Expand Down
7 changes: 6 additions & 1 deletion OnTopReplica/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static void Application_Idle(object sender, EventArgs e) {
/// </summary>
static void UpdateManager_CheckCompleted(object sender, UpdateCheckCompletedEventArgs e) {
if (e.Success && e.Information != null) {
Log.Write("Updated check successful (latest version is {0})", e.Information.LatestVersion);
Log.Write("Update check successful (latest version is {0})", e.Information.LatestVersion);

if (e.Information.IsNewVersionAvailable) {
Update.ConfirmAndInstall();
Expand All @@ -139,6 +139,11 @@ static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEv
sw.WriteLine("Last exception:");
sw.WriteLine(e.ExceptionObject.ToString());
sw.WriteLine();
sw.WriteLine("Last log entries:");
foreach (var logEntry in Log.Queue) {
sw.WriteLine(logEntry);
}
sw.WriteLine();
sw.WriteLine("OnTopReplica v.{0}", Application.ProductVersion);
sw.WriteLine("OS: {0}", Environment.OSVersion.ToString());
sw.WriteLine(".NET: {0}", Environment.Version.ToString());
Expand Down

0 comments on commit 9d97da1

Please sign in to comment.