How to save pending log information when your application crashes

Do have an application that never crashes? Then you are very lucky and don’t need this tip!

 

For all others: Even when your program crashes you normally can save (or try to save) all pending logmessages to disk. This even includes the logmessages of the crashing thread that were not sent at the moment of the crash. To achieve this, your program must call

 GetITLogLib().Deinitialize();

even when it crashes.

 

How you can achieve this usually depends on the compiler you use. Microsoft Visual C++, for instance, provides the keywords __try and __except for that purpose, which build on the "Structured Exception Handling" (SEH) of the Windows API. So in Visual C++ you would write:

 

__try ()

{

 MainFunction();

// - This is a function that produces logmessages

// and might crash at a certain point.

}

__except(1)

{

 ITLogLibDeinitialize();

 // - writes out all pending output

}

 

Then, in the case of a crash ITLogLib would be deinitialized in the __except clause. On this occasion all pending logmessages will be written to disk -- if the crash has not destroyed too much.

 

Note that you should deinitialize ITLogLib also in the normal case when no exception occurred. Here we assumed that the initialization and deinitialization of ITLogLib is done in MainFunction().