Delphi - Reading from a log file that changes every second -
i need read .log file changing application. (more data being added frequently)
so have begin with:
var logfile: tstrings; stream: tstream; begin logfile := tstringlist.create; try stream := tfilestream.create(log, fmopenread or fmsharedenynone); try logfile.loadfromstream(stream); stream.free; end; while logfile.count > memo1.lines.count memo1.lines.add(logfile[memo1.lines.count]); logfile.free; end; end;
this works fine. updates memo @ real time data being added. of data being added don't want see in memo. wish not add these lines, still have memo updated @ real time without junk lines.
what best way go this?
you'd need check see if line has content want include, , add if has content (or not add if don't want include it, whichever case). more efficient keep track of last line in logfile
processed previously, skip lines each time - if make variable private member of form itself, automatically initialized 0 when application starts:
type tform1 = class(tform) //... other stuff added ide private lastline: integer; end; // @ point need add logfile memo := lastline logfile.count - 1 begin if contentwanted(logfile[i]) memo1.lines.append(logfile[i]); inc(lastline); end;
so handle based on code:
type tform1 = class(tform) //... ide stuff here private flastlogline: integer; procedure processlogfile; public // other stuff end; procedure tform1.processlogfile; var log: tstringlist; logstream: tfilestream; i: integer; begin log := tstringlist.create; try logstream := tfilestream.create(...); try log.loadfromstream(logstream); logstream.free; end; := flastlogline log.count - 1 if pos('[globals] []', log[i]) <>0 memo1.lines.append(log[i]); // we've processed lines in log. save // last line processed starting point // next pass. flastlogline := log.count - 1; log.free; end; end; procedure tform1.timer1timer(sender: tobject); begin timer1.enabled := false; try processlogfile; timer1.enabled := true; end; end; end;
Comments
Post a Comment