Skip to content

Commit

Permalink
add log of exceptions to help tracking the rare crash.
Browse files Browse the repository at this point in the history
fix incorrect snapshot area when dpi setting is not 100%.
  • Loading branch information
geovens committed Jul 20, 2016
1 parent 3c57849 commit 36d9f93
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
build
release
bin/crash.txt

*.pdb
*.exp
Expand Down
7 changes: 6 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@

v0.3.4 (ongoing)
=================
- Log unhandled exceptions to a file to help tracking rare crashes.
- Fix incorrect snapshot area when the DPI setting is not 100%. (not fully tested)

v0.3.3 (Jul. 10, 2016)
=================
- Change system tray icon size.
- Do not quit drawing after snapshot if there are inks.

v0.3.2 (Feb. 1, 2016)
=================
- Click throung mode has a dedicated button now.
- Click through mode has a dedicated button now.
- Add two new pens, to a total of five.
- Add an entry to pen settings.

Expand Down
12 changes: 12 additions & 0 deletions src/FormDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ public void SnapShot(Rectangle rect)
//tempbmp.Dispose();

IntPtr screenDc = GetDC(IntPtr.Zero);

const int VERTRES = 10;
const int DESKTOPVERTRES = 117;
int LogicalScreenHeight = GetDeviceCaps(screenDc, VERTRES);
int PhysicalScreenHeight = GetDeviceCaps(screenDc, DESKTOPVERTRES);
float ScreenScalingFactor = (float)PhysicalScreenHeight / (float)LogicalScreenHeight;

rect.X = (int)(rect.X * ScreenScalingFactor);
rect.Y = (int)(rect.Y * ScreenScalingFactor);
rect.Width = (int)(rect.Width * ScreenScalingFactor);
rect.Height = (int)(rect.Height * ScreenScalingFactor);

IntPtr hDest = CreateCompatibleDC(screenDc);
Bitmap tempbmp = new Bitmap(rect.Width, rect.Height);
IntPtr hBmp = tempbmp.GetHbitmap();
Expand Down
89 changes: 89 additions & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.IO;

namespace gInk
{
Expand All @@ -12,11 +15,97 @@ static class Program
[STAThread]
static void Main()
{
Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

new Root();
Application.Run();
}

private static void UIThreadException(object sender, ThreadExceptionEventArgs t)
{
DialogResult result = DialogResult.Cancel;
try
{
Exception ex = (Exception)t.Exception;

string errorMsg = "UIThreadException\r\n\r\n";
errorMsg += "Oops, gInk crashed! Please include the following information if you plan to contact the developers (a copy of the following information is stored in crash.txt in the application folder):\r\n\r\n";
errorMsg += ex.Message + "\r\n\r\n";
errorMsg += "Stack Trace:\r\n" + ex.StackTrace + "\r\n\r\n";
WriteErrorLog(errorMsg);

errorMsg += "!!! PLEASE PRESS ESC KEY TO EXIT IF YOU FEEL YOUR MOUSE CLICK IS BLOCKED BY SOMETHING";
ShowErrorDialog("UIThreadException", errorMsg);
}
catch
{
try
{
MessageBox.Show("Fatal Windows Forms Error", "Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}

// Exits the program when the user clicks Abort.
if (result == DialogResult.Abort)
Application.Exit();
}

private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;

string errorMsg = "UnhandledException\r\n\r\n";
errorMsg += "Oops, gInk crashed! Please include the following information if you plan to contact the developers:\r\n\r\n";
errorMsg += ex.Message + "\r\n\r\n";
errorMsg += "Stack Trace:\r\n" + ex.StackTrace + "\r\n\r\n";
WriteErrorLog(errorMsg);

ShowErrorDialog("UnhandledException", errorMsg);

if (!EventLog.SourceExists("UnhandledException"))
{
EventLog.CreateEventSource("UnhandledException", "Application");
}
EventLog myLog = new EventLog();
myLog.Source = "UnhandledException";
myLog.WriteEntry(errorMsg);
}
catch (Exception exc)
{
try
{
MessageBox.Show("Fatal Non-UI Error", "Fatal Non-UI Error. Could not write the error to the event log. Reason: " + exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
}

private static DialogResult ShowErrorDialog(string title, string errormsg)
{
return MessageBox.Show(errormsg, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}

private static void WriteErrorLog(string errormsg)
{
FileStream fs = new FileStream("crash.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(errormsg);
sw.Close();
fs.Close();
}
}
}
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.3.3.0")]
[assembly: AssemblyFileVersion("0.3.3.0")]
[assembly: AssemblyVersion("0.3.4.0")]
[assembly: AssemblyFileVersion("0.3.4.0")]

0 comments on commit 36d9f93

Please sign in to comment.