Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > new content notification from a log file

Reply
Thread Tools

new content notification from a log file

 
 
Oliver
Guest
Posts: n/a
 
      11-28-2007
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
 
Reply With Quote
 
 
 
 
Gary Wright
Guest
Posts: n/a
 
      11-28-2007

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

 
Reply With Quote
 
 
 
 
Oliver
Guest
Posts: n/a
 
      11-28-2007
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 <(E-Mail Removed)> 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 <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
problem implementing log4j? not inserting data in new userlog.log other then server.log vj Java 0 12-04-2006 08:59 AM
How to log independently of other webapps using log4j? Also, reading from log file from within servlet? unomystEz Java 0 11-19-2006 10:42 AM
is delivery status notification a return email or smtp log line? Dica ASP General 0 10-06-2005 03:29 PM
Urgent Pls: Facing problem in reading Log information from Log file, created by IIS Amratash ASP .Net 0 04-13-2004 09:08 AM



Advertisments