I've created a simple logger which, well, logs everything of importance into a text file. I'm using std::ofstream
but there's a problem - when the program doesn't close the file (call std::ofstream::close()
) for whatever reason (like crash), the created log is actually empty (0 size). And since the log is most useful in exceptional situations (when something goes wrong) - you see the problem.
Is there any way to protect my log from that? I could try closing the file after writing every couple of lines and using append - but that still doesn't protect me from the situation when the program crashes in the middle of logging/before closing the file. Is there any solution or am I just doomed?