Board index » delphi » 'Tail' command in Delphi

'Tail' command in Delphi

Hello,

  Does anyone know of a way that I can get the same functionality of
the 'tail' command in UNIX?  I need to be able to watch a log file and
parse the lines as they are being added.

Thank you very much,
David

 

Re:'Tail' command in Delphi


Quote
"Dave" <slashdevn...@mac.com> wrote in message news:731e7821.0107301123.84cdce0@posting.google.com...
> Hello,

>   Does anyone know of a way that I can get the same functionality of
> the 'tail' command in UNIX?  I need to be able to watch a log file and
> parse the lines as they are being added.

> Thank you very much,
> David

it depends how the file is held open by the writer
if like below one writer and any number of readers
can access the file concurrently
including the same apps timer function

var stop:boolean;
procedure TForm1.writefileClick(Sender: TObject);
var f:textfile;
begin
   stop:=false;
   assignfile(f,'tailer.txt');
   if fileexists('tailer.txt')
   then
     append(f)
   else
     rewrite(f);
   repeat
       writeln(f,timetostr(time));
       flush(f);
       application.processmessages;
       sleep(1000);
       application.processmessages;
   until stop;
   closefile(f);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var f:file of byte;
var buf:array[1..101] of byte;
var readchars:integer;
begin
   memo1.clear;
   if not fileexists('tailer.txt') then
   begin
     memo1.lines.add('no file');
     exit;
   end;
   assignfile(f,'tailer.txt');
   filemode:=0;
   reset(f);

       memo1.lines.add(inttostr(filesize(f)));
       if filesize(f) > 100 then
           seek(f,filesize(f)-100);
       blockread(f,buf,100,readchars);
       buf[readchars+1]:=0;
       memo1.lines.add(pchar(@buf));

   closefile(f);
end;

procedure TForm1.stopbuttonClick(Sender: TObject);
begin
 stop:=true;
end;

Other Threads