tags:

views:

347

answers:

4

I wondering how in C/C++ you can implement a program (similar to tail -f) that watches for new lines added to a log file and then process them?

A: 

See here: http://stackoverflow.com/questions/18632/how-to-monitor-a-text-file-in-realtime#18635

You could either call out to tail and retrieve the stream back into your app, or as it's open source, maybe try to pull it into your own code.

Also, it is possible in C++ iostream to open a file for viewing only and just read to the end, while buffering the last 10-20 lines, then output that.

Adam Haile
+4  A: 

You can use fseek() to clear the eof condition on the stream. Essentially, read to the end of the file, sleep for a while, fseek() (without changing your position) to clear eof, the read to end of file again. wash, rinse, repeat. man fseek(3) for details.

Here's what it looks like in perl. perl's seek() is essentially a wrapper for fseek(3), so the logic is the same:

wembley 0 /home/jj33/swap >#> cat p
my $f = shift;
open(I, "<$f") || die "Couldn't open $f: $!\n";

while (1) {
  seek(I, 0, 1);
  while (defined(my $l = <I>)) {
    print "Got: $l";
  }
  print "Hit EOF, sleeping\n";
  sleep(10);
}
wembley 0 /home/jj33/swap >#> cat tfile
This is
some
text
in
a file
wembley 0 /home/jj33/swap >#> perl p tfile
Got: This is
Got: some
Got: text
Got: in
Got: a file
Hit EOF, sleeping

Then, in another session:

wembley 0 /home/jj33/swap > echo "another line of text" >> tfile

And back to the original program output:

Hit EOF, sleeping
Got: another line of text
Hit EOF, sleeping
jj33
A: 

I think what you're looking for is the select() call in c/c++. I found a copy of the man page here: http://www.opengroup.org/onlinepubs/007908775/xsh/select.html. Select takes file descriptors as arguments and tells you when one of them has changed and is ready for reading.

Mykroft
A: 

The tail program is open source, so you could reference that. I wondered the same thing and looked at the code a while back, thinking it would be pretty simple, but I was surprised at how complex it was. There are lots of gotchas that have to be taken into account.

Eric Z Beard