On 2007-07-04 11:26, Jrgen Exner <> wrote:
> Tim Southerwood wrote:
>> Opening and closing the file on every call to Log() is expensive.
>
> True.
>
>> One usually opens it once and then keeps the file handle around.
>
> Depends. If you want to capture the last breath of your program before it
> dies an untimely death then you don't want to risk loosing its last words in
> an unsaved buffer.
You should generally turn autoflush on on log files. Apart from the
reason you just mentioned there are two others:
1) If your program only prints log entries only infrequently, it may
take a long time until the (typically 4kB or 8kB) buffer fills up.
Normally you want every log message in the log immediately, not a
bunch of messages every few hours.
2) Buffers usually have a fixed size, so the blocks written to the log
file will not start and end at line boundaries. If you have several
processes writing to the same log file lines will be mixed. If you
flush after every line that can't happen (at least on Unix, the
kernel makes sure that a single write on a file in append mode is
written atomically). For the same reason you should write the whole
message with a single print call.
hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | |
|
__/ |
http://www.hjp.at/ | -- Sam in "Freefall"