Jump to content
  • Advertisement
Sign in to follow this  
sandy

Writing a log entry before crashing?

This topic is 4490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites


int main(...)
{
try
{
//your main function here
}
catch(...) //catches all exception
{
writesyslogendingtag();
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!