Link to home
Start Free TrialLog in
Avatar of GoWithTheFlow
GoWithTheFlow

asked on

most efficient way to write a log file?

I need to write text to a log file.  I'm always appending to the end of it.  I'm on an NT system.  What is the most efficient way to do this?  CFile?  CStdIoFile?  ofstream?  I'd like to write to the file possibly a few times per second, and would definitely like to flush it to disk from time to time (every 5-60 seconds).
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GoWithTheFlow
GoWithTheFlow

ASKER

Is it more efficient to flush periodically, or to use the NO_BUFFERING flag?

I'm also wondering if I could avoid closing and reopening the file all the time. To be sure my data is safely on the disk, do I need to close & reopen the file the file? Or can I leave it open?  In other words, will the file be corrupt if the machine crashes while I have it open?
Well, it depends on what is more important for you - if application performance is more important, flushing periodically would be the way to go, but if it is data integrity (e.g. you don't want to loose any data), i'd prefer 'FILE_FLAG_NO_BUFFERING'. (BTW: 'FILE_FLAG_NO_BUFFERING' might also come in handy when you need high-performance direct I/O bypassing the file system cache, but this is an issue of I/O performance, e.g. when handling large files, not log files...)