Logging Error Messages


Debugging game code can be very complicated, especially if you don’t get any exceptions, but something in your render loop goes wrong. Just setting a few breakpoints might not be enough and especially if you experience errors after running the game for a while, debugging is not the right option. You want to know what is going on each frame, but you don’t want to step through 500 frames to find it out. For these kinds of problems you can just throw some text to the console, but this will only work inside Visual Studio and you will lose all the console content the next time you start your project.

One of the most important classes in all bigger projects I have done is the Log class, which just writes information, warning, error, or debugging texts to a simple text file. The class itself is very short and simple, but if you use it in the right way it will make your debugging and testing sessions much more enjoyable. There are also more advanced logging classes and frameworks available like Log4Net, which you can find at http://logging.apache.org/log4net/. Logging can be more than just writing a few lines to a text file. Logging data from your application can be used to find out about user errors remotely with a web service, you can fire Windows error events, and you can do a lot more things. This is not covered in this book because it is a very complex topic. For the simple games in this book, using Log class should be sufficient.

Take a look at the Log class (a more complex version can be found in the Breakout game):

  public class Log {   #region Variables   private static StreamWriter writer = null;   private const string LogFilename = "Log.txt";   #endregion 

The Log class uses the Log.txt file to store all the log messages with the help of the StreamWriter object stored as a static object to have easy access from your static methods. The first time this class is called it gets instantiated through the static constructor:

  #region Static constructor to create log file static Log() {   // Open file   FileStream file = new FileStream(     LogFilename, FileMode.OpenOrCreate,     FileAccess.Write, FileShare.ReadWrite);   writer = new StreamWriter(file);   // Go to end of file   writer.BaseStream.Seek(0, SeekOrigin.End);   // Enable auto flush (always be up to date when reading!)   writer.AutoFlush = true;   // Add some info about this session   writer.WriteLine("/// Session started at: "+     StringHelper.WriteIsoDateAndTime(DateTime.Now)); } // Log() #endregion 

FileShare.ReadWrite makes sure you can always read and write the file from outside while the game is running. Other than that the writer is set to the end of the file, auto flush is enabled to make sure writing new data is immediately saved to the log file, and finally you add a little text indicating that this session started. For the time stamp you will use a helper method from the StringHelper class you learn about in a second.

And finally, here is the most important method and the only one you will ever call from this class:

  #region Write log entry static public void Write(string message) {   DateTime ct = DateTime.Now;   string s = "[" + ct.Hour.ToString("00") + ":" +     ct.Minute.ToString("00") + ":" +     ct.Second.ToString("00") + "] " +     message;   writer.WriteLine(s); #if DEBUG   // In debug mode write that message to the console as well!   System.Console.WriteLine(s); #endif } // Write(message) #endregion 

First, a simple time stamp is added in front of the message. Then the message is written to your Log.txt file and finally you also add the message to the console if the project is in debug mode. Now you can add a new line to the Log.txt file every time you complete a level in the Breakout game from Chapter 2 by just adding the following line:

  Log.Write("Level " + level + " completed."); 




Professional XNA Game Programming
Professional XNA Programming: Building Games for Xbox 360 and Windows with XNA Game Studio 2.0
ISBN: 0470261285
EAN: 2147483647
Year: 2007
Pages: 138

Similar book on Amazon
XNA Game Studio 4.0 Programming: Developing for Windows Phone 7 and Xbox 360 (Developer's Library)
XNA Game Studio 4.0 Programming: Developing for Windows Phone 7 and Xbox 360 (Developer's Library)
Learning XNA 3.0: XNA 3.0 Game Development for the PC, Xbox 360, and Zune
Learning XNA 3.0: XNA 3.0 Game Development for the PC, Xbox 360, and Zune
Beginning XNA 2.0 Game Programming: From Novice to Professional (Expert's Voice in Game Programming)
Beginning XNA 2.0 Game Programming: From Novice to Professional (Expert's Voice in Game Programming)
Microsoftu00ae XNAu00ae Game Studio 3.0: Learn Programming Now! (Pro - Developer)
Microsoftu00ae XNAu00ae Game Studio 3.0: Learn Programming Now! (Pro - Developer)

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net