How to protect the log from application failure?

I created a simple logger that, well, writes all the values ​​to a text file. I use std::ofstream, but there is a problem - when the program does not close the file (call std::ofstream::close()) for any reason (for example, a failure), the created log is actually empty (size 0). And since the magazine is most useful in exceptional situations (when something goes wrong) - you see the problem.

Is there a way to protect my journal from this? I could try to close the file after writing each pair of lines and using append, but this still does not protect me from the situation when the program crashes out of the system in the middle of logging / before closing the file. Is there any solution, or am I just doomed?

+2
source share
2 answers

You should use the flush method , it should accurately solve problems like the ones you are facing.

, , . , (IPC). , , (). ( , ). , , , .

, . , , flush() .

+3

. .

Vectored Exception Handler. , (, , ). , ++ ( C). fflush .

, . . malloc. crashfile, , , .. , .

+1

All Articles