[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

new content notification from a log file

Oliver

11/28/2007 8:04:00 PM

dear all,

I have the following situation that I don't have a good solution:

One process periodically will append its output to a file (which is
not under my control), I want to write a ruby problem to detect and
report only the *new* stuff that went into the output file ... Besides
the brute force way such as opening the file and counting the lines
etc, is there a better solution to it?

Thanks for help

Oliver
4 Answers

Gary Wright

11/28/2007 8:21:00 PM

0


On Nov 28, 2007, at 3:05 PM, Oliver wrote:
> One process periodically will append its output to a file (which is
> not under my control), I want to write a ruby problem to detect and
> report only the *new* stuff that went into the output file ... Besides
> the brute force way such as opening the file and counting the lines
> etc, is there a better solution to it?

You can use File.stat('filename').size to get the number of bytes in
the file or .mtime to find the last modification time. If the bytes
and/or time changes you can then open the file and grab the new lines.

Gary Wright

Christian von Kleist

11/28/2007 8:33:00 PM

0

You can do something like this:

f = File.new('logfile.log')

while true
while(line = f.gets)
puts line
end
sleep 5
end


On Nov 28, 2007 3:21 PM, Gary Wright <gwtmp01@mac.com> wrote:
>
> On Nov 28, 2007, at 3:05 PM, Oliver wrote:
> > One process periodically will append its output to a file (which is
> > not under my control), I want to write a ruby problem to detect and
> > report only the *new* stuff that went into the output file ... Besides
> > the brute force way such as opening the file and counting the lines
> > etc, is there a better solution to it?
>
> You can use File.stat('filename').size to get the number of bytes in
> the file or .mtime to find the last modification time. If the bytes
> and/or time changes you can then open the file and grab the new lines.
>
> Gary Wright
>
>

Oliver

11/28/2007 9:20:00 PM

0

Thanks to both of you for the solution.

The double while loop seems a bit more concise, but it depends on
File#gets method handles the changing file content correctly. I will
give it a spin.

Best,

Oliver

On Nov 28, 3:33 pm, Christian von Kleist <cvonkle...@gmail.com> wrote:
> You can do something like this:
>
> f = File.new('logfile.log')
>
> while true
> while(line = f.gets)
> puts line
> end
> sleep 5
> end
>
> On Nov 28, 2007 3:21 PM, Gary Wright <gwtm...@mac.com> wrote:
>
>
>
> > On Nov 28, 2007, at 3:05 PM, Oliver wrote:
> > > One process periodically will append its output to a file (which is
> > > not under my control), I want to write a ruby problem to detect and
> > > report only the *new* stuff that went into the output file ... Besides
> > > the brute force way such as opening the file and counting the lines
> > > etc, is there a better solution to it?
>
> > You can use File.stat('filename').size to get the number of bytes in
> > the file or .mtime to find the last modification time. If the bytes
> > and/or time changes you can then open the file and grab the new lines.
>
> > Gary Wright

Christian von Kleist

11/28/2007 9:27:00 PM

0

On Nov 28, 2007 4:20 PM, Oliver <fwang2@gmail.com> wrote:
> Thanks to both of you for the solution.
>
> The double while loop seems a bit more concise, but it depends on
> File#gets method handles the changing file content correctly. I will
> give it a spin.
>
> Best,
>
> Oliver
>
> On Nov 28, 3:33 pm, Christian von Kleist <cvonkle...@gmail.com> wrote:
> > You can do something like this:
> >
> > f = File.new('logfile.log')
> >
> > while true
> > while(line = f.gets)
> > puts line
> > end
> > sleep 5
> > end
> >
>
> > On Nov 28, 2007 3:21 PM, Gary Wright <gwtm...@mac.com> wrote:
> >
> >
> >
> > > On Nov 28, 2007, at 3:05 PM, Oliver wrote:
> > > > One process periodically will append its output to a file (which is
> > > > not under my control), I want to write a ruby problem to detect and
> > > > report only the *new* stuff that went into the output file ... Besides
> > > > the brute force way such as opening the file and counting the lines
> > > > etc, is there a better solution to it?
> >
> > > You can use File.stat('filename').size to get the number of bytes in
> > > the file or .mtime to find the last modification time. If the bytes
> > > and/or time changes you can then open the file and grab the new lines.
> >
> > > Gary Wright
>
>
>

You're welcome! That approach has worked for me many times in the
past in many languages, including Ruby. Give it a run!