Writing a log entry before crashing?

Started by
19 comments, last by Endurion 17 years, 10 months ago
I'm setting up a better logging system than the one I have now, which was just a plain text file. The new system uses XML in a similar fashion to the article about this here on GameDev. Anyway, the problem is that the XML file needs to be terminated with a </SystemLog> tag, otherwise when I try to load the XML file in say internet explorer I receive an error. If the program runs fine from start to finish, no problem as I can just write the terminating </SystemLog> tag to the end of the XML file when the program closes. However, if the program crashes, then it terminates without shutting down the logging system, which means the </SystemLog> tag is not written to the XML file. Is there some way that I can ensure that this </SystemLog> tag will be written to the end of my log file no matter how the application is terminated?
Advertisement
Are you using exception handling?
Create file with closing tag.

When you want to write to the log. Move the file pointer back the number of character that the closing tag takes. Then write log text and closing tag.

Then when there is a crash the closing log is there. Unless the crash takes place during the log writing.
Marcus SpeightIf at first you don't succeed.
Destroy all evidence that you tried.
I considered the following methods when I had this problem:

  • Write the SytemLog end tag out every time you add an entry, then seek the file back to overwrite it with the next entry
  • Don't write out the SystemLog tags at all - associate a program that writes them then opens the file in IE/Firefox/whatever to the extension you use for your logs
  • Monitor you main program with an external process which writes the tag on exit

int main(...){try{//your main function here}catch(...) //catches all exception{writesyslogendingtag();return EXIT_FAILURE;}return EXIT_SUCCESS;}
http://www.8ung.at/basiror/theironcross.html
In addition to try/catch, iirc you need to use __try / __except / __finally to handle windows-generated exceptions (such as writing to a an invalid memory location). Of course, they're an MSVC extension to C++ and I don't know the equivalents for other compilers.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
You can't rely on exceptions for this - none of them will work if your code manages to blue-screen-of-death your PC.

On the other hand, it may just be easier to manually edit the log file in the event of a crash causing a BSOD, since they're very rare.
In my opinion, XML is the wrong choice for a logging format. As you've managed to discover, it does not cope well with sudden termination - which is one of the main times you'd want to look at a log.
Quote:Original post by MaulingMonkey
In my opinion, XML is the wrong choice for a logging format. As you've managed to discover, it does not cope well with sudden termination - which is one of the main times you'd want to look at a log.


QFE.
rather keep a log of the last 10 important operations that occured, then keep over writing the old one. This can give you some idea of where the crash occured(in a worst case scenario).
----------------------------

http://djoubert.co.uk

This topic is closed to new replies.

Advertisement