Should log classes open/close a log file stream on each write to the log file or should it keep the log file stream open throughout the application\'s lifetime until all log
I don't see any reason to close it.
On the other hand, closing and reopening takes a little extra time.
As a user of your application I'd prefer it to not hold files open unless it's a real requirement of the app. Just one more thing that can go wrong in the event of a system crash, etc.
It's a tradeoff. Opening and closing the file each time makes it more likely that the file will be updated on disk in the program crashes. On the other hand, there's some overhead involved in opening the file, seeking to the end, and appending data to it.
On Windows, you won't be able to move/rename/delete the file while it's open, so open/write/close might be helpful for a long-running process where you might occasionally want to archive the old log contents without interrupting the writer.
In most of the cases where I've done this sort of logging, I've kept the file open, and used fflush() to make it more likely the file was up-to-date if the program crashed.
If you have frequent read/writes it is more efficient to keep the file open for the lifetime with a single open/close.
You might want to flush periodically or after each write though in case your application crashes you might not have all the data written to your file. Use fflush on Unix based systems and FlushFileBuffers on Windows.
If you are running on Windows as well you can use the CreateFile API with FILE_FLAG_NO_BUFFERING to go direct to file on each write.
It is also better to keep the file open for the lifetime, because each time you open/close you might have a failure if the file is in use. For example you might have a backup application that runs and open/closes your file as it's backing it up. And this might cause your program to not be able to access your own file. Ideally you would want to keep your file open always and specify sharing flags on Windows (FILE_SHARE_READ). On Unix based systems, sharing will be default.
For large intensive applications, what I usually do is I keep the log file open for the duration of the application and have a separate thread that flushes log content in the memory to HDD periodically. File open and close operation require system calls, which is a lot of work if you look into lower level.
Open and close. Can save you from a corrupt file in case of a system crash.