Sign in to follow this  
  • entries
    67
  • comments
    89
  • views
    51536

Errors, meh.

Sign in to follow this  

261 views

Error logging


So I've been plugging in some infrastructure for asset management, etc. In the process, I've improved on my old method of action/error logging. Along with various other customizations (recording various parameters and messages with errors), I wanted to be able to display a message box upon closing the app if there were errors recorded during the run. Also, if a fatal error was encountered, I wanted the app to exit straight away, but cleanly (Ie cleaning everything up and closing without exception boxes, etc).

For the exiting on fatal error business, I just have the app check (in the update() function) for a "isFatal" flag in my error logging class and call the this.Exit() function. Easy.

As for showing message boxes upon closing, that was a little trickier. It appears that any message boxes you create when the XNA app is closing don't get shown as the main window is disappearing. After looking around on the net a bit, I found the following solution. You spawn a seperate thread just for displaying the message boxes. Seems like a bit of overkill, but it works, and since the app is just closing anyway, there's no performance issue.

I created an exiting delegate to be called on closing the XNA game class:


this.Exiting += new EventHandler(Demo_Exiting);



Then I create the seperate thread if there are error messages to show in the delegate:

void Demo_Exiting(object sender, EventArgs e)
{
ThreadStart errorMsg = delegate
{
if (OutputLog.isAppFatal)
{
MessageBox.Show(OutputLog.fatalErrorMessage, "Fatal Error", MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}
else
{
string errorsReport = "Errors were reported.\n";
switch (OutputLog.maxErrorLevel)
{
case ERROR_LEVEL.Minor:
errorsReport += "Maximum error level was MINOR.\n";
break;

case ERROR_LEVEL.Major:
errorsReport += "Maximum error level was MAJOR (not fatal).\n";
break;

case ERROR_LEVEL.FATAL:
errorsReport += "Maximum error level was FATAL!.\n";
break;
}
errorsReport += "See file errors.log for details.";
MessageBox.Show(errorsReport, "Errors reported", MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}
};

if (OutputLog.wereErrorsReported)
{
Thread thread = new Thread(errorMsg);
thread.IsBackground = false;

thread.Start();
}
}



This works like a treat so now it reminds you if there were non-fatal errors encountered upon closing:


And you get some indication as to what happened if a fatal error as encountered, rather than just rudely dumping you out:
Sign in to follow this  


2 Comments


Recommended Comments

Good to see you back in business Lachlan. It's definitely true that a solid logging system often proves to be a huge boon later on in development, so it's always wise to get it out of the way early. [smile]

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now