Robert Klemme
5/31/2008 3:36:00 PM
On 31.05.2008 13:21, Dave Bass wrote:
> Eric LIn wrote:
>> My suspicion is I'm not instantiating File correctly. Perhaps there
>> is an option for unbuffered output?
>
> File is a subclass of IO, and IO has a flush method, which File
> inherits. So you can do file.flush after each write to bypass Ruby's
> buffering.
>
> Pickaxe p. 509 says:
>
> "io.flush -- Flushes any buffered data within io to the underlying
> operating system (note that this is Ruby internal buffering only; the OS
> may buffer the data as well)."
>
> So you could try flushing the log file after each log.info call. I
> suspect this means subclassing Logger or modifying something within
> Logger; maybe someone more experienced could suggest how to do this.
There is a much better option: create the File with File::SYNC. You
should check on your platform if io.sync returns true afterwards. If
not, you should try io.sync=true. That way all writes go directly to
the file.
> You're still at the mercy of the OS's buffering, which is additional to
> Ruby's.
But this does not matter that much because if you read the log file and
the block was not yet written to disk you see the contents nevertheless.
OS buffering is only an issue if the OS crashes which is unlikely.
Kind regards
robert