# Output delay

**URL:** <https://www.ruby-forum.com/t/output-delay/95639>\
**Category:** Ruby\
**Created:** [June 7, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/output-delay/95639 "2006-06-07T23:08:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [June 7, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/output-delay/95639/1 "2006-06-07T23:08:45Z")

</div>

Howdy,

I am doing some log processing (cross referencing multiple logs, and  
activating triggers based on the results) and printing the results to  
stdout for testing while writing to a file at the same time. The  
symptom that I am seeing is that the output to stdout is immediate,  
while tailing the file with the same output seems to be one entry  
behind. Here is what the code looks like:

```
                logRoot = '/var/log/'
                blockLog = File.open(logRoot + 'block.log', "a+")
                result = searchFW(src,dst,path,time) # does a

```

search for the needed info in the log, and returns the matching lines  
unless result.empty?  
token = parseFW(result) # runs  
the results through a parser that pulls out the relevant bits  
blockLog \<\< date+"+"+token+"+"+url+"\n"  
p date+"+"+token+"+"+url  
end

I am a bit green, so my code bits may look clunky. I am using ruby 1.8  
on FreeBSD, and the above code is in the depths of a loop that tails a  
log file. Actually, the code above is a compressed view with the  
relevant parts. Variables/objects are declared further up in the tree.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [June 8, 2006, 12:28am UTC](https://www.ruby-forum.com/t/output-delay/95639/2 "2006-06-08T00:28:10Z")

</div>

On Jun 7, 2006, at 22:06, Daniel S. wrote:

> Howdy,
> 
> I am doing some log processing (cross referencing multiple logs,  
> and activating triggers based on the results) and printing the  
> results to stdout for testing while writing to a file at the same  
> time. The symptom that I am seeing is that the output to stdout is  
> immediate, while tailing the file with the same output seems to be  
> one entry behind. Here is what the code looks like:

This is the result of buffered output, one simple change ought to fix  
it up:

> ```
> logRoot = '/var/log/'
> blockLog = File.open(logRoot + 'block.log', "a+")
> result = searchFW(src,dst,path,time) # does a  
> 
> ```
> 
> search for the needed info in the log, and returns the matching lines  
> unless result.empty?  
> token = parseFW(result) #  
> runs the results through a parser that pulls out the relevant bits  
> blockLog \<\< date+"+"+token+"+"+url+"\n"  
> blockLog.flush

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [June 8, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/output-delay/95639/3 "2006-06-08T15:02:22Z")

</div>

Thank you, that was exactly what I needed. Last night as I was trying  
to sleep I was thinking about the fact that it was an I/O and would  
probably need to be flushed, and sure enough.

Matthew S. wrote:

> > behind. Here is what the code looks like:  
> > unless result.empty?  
> > token = parseFW(result) #  
> > runs the results through a parser that pulls out the relevant bits  
> > blockLog \<\< date+"+"+token+"+"+url+"\n"  
> > blockLog.flush  
> > p date+"+"+token+"+"+url  
> > end

–  
Daniel S.  
Systems Administrator  
Technology Services  
Spring Arbor University  
517 750-6648

“For even the Son of Man did not come to be served, but to serve, and to  
give His life a ransom for many”  
Mark 10:45
