Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Errors, meh.

Errors, meh.

LachlanL
LachlanL
Journal of a Shaven Ape · · 2 min read
1,145 2

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:

Discussion

Loading comments...