So I started from the beginning:
var
LogFile: TStrings;
Stream: TStream;
begin
LogFile := TStringList.Create;
try
Stream := TFileStream.Create(Log, fmOpenRead or fmShareDenyNone);
try
LogFile.LoadFromStream(Stream);
finally
Stream.Free;< br /> end;
while LogFile.Count> Memo1.Lines.Count do
Memo1.Lines.Add(LogFile[Memo1.Lines.Count]);
finally< br /> LogFile.Free;
end;
end;
This is completely ok. It will update the memo in real time while adding data. However, I want to put it in the memo I see some added data. I hope not to add these lines, but I can still update the memo in real time without the garbage line.
What is the best way?
type
TForm1 = class(TForm )
//... other stuff added by IDE
private
LastLine: Integer;
end;
// At the point you need to add the logfile to the memo
for i := LastLine to LogFile.Count-1 do
begin
if ContentWanted(LogFile[i]) then
Memo1. Lines.Append(LogFile[i]);
Inc(LastLine);
end;
So handle this problem completely according to your 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
Lo g := TStringList.Create;
try
LogStream := TFileStream.Create(...);
try
Log.LoadFromStream(LogStream);
finally< br /> LogStream.Free;
end;
for i := FLastLogLine to Log.Count-1 do
if Pos('[Globals] []', Log[ i]) <>0 then
Memo1.Lines.Append(Log[i]);
// We've now processed all the lines in Log. Save
/ / the last line we processed as the starting point
// for the next pass.
FLastLogLine := Log.Count-1;
finally
Log.Free;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
try
ProcessLogFile;
finally
Timer1.Enabled := True;
end;
end;
end;
I need to read from a .log file that is constantly changing by another application. (Add more data often)
So I start from the beginning:
< /p>
var
LogFile: TStrings;
Stream: TStream;
begin
LogFi le := TStringList.Create;
try
Stream := TFileStream.Create(Log, fmOpenRead or fmShareDenyNone);
try
LogFile.LoadFromStream(Stream);
finally
Stream.Free;
end;
while LogFile.Count> Memo1.Lines.Count do
Memo1.Lines.Add(LogFile[Memo1.Lines. Count]);
finally
LogFile.Free;
end;
end;
This is completely fine. It will update in real time while adding data Memo. However, I want to see some added data in the memo. I want to not add these lines, but still can update the memo in real time without the garbage line.
The best way is What?
You obviously need to check if the line contains what you want to include, and only add it when you have it (if you don’t want to include it, Then don’t add it, in either case). It will also be more efficient to track the last line in the LogFile processed before, so you can skip these lines every time-if you set the variable as a private member of the form itself, it will Automatically initialize to 0 when your application starts:
type
TForm1 = class(TForm)
//... other stuff added by IDE
private
LastLine: Integer;
end;
// At the point you need to add the logfile to the memo
for i := LastLine to LogFile.Count-1 do
begin
if ContentWanted(LogFile[i]) then
Memo1.Lines.Append(LogFile[i]);
Inc(LastLine);
end;
So handle this problem completely according to your 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;< br />begin
Log := TStringList.Create;
try
LogStream: = TFileStream.Create(...);
try
Log.LoadFromStream(LogStream);
finally
LogStream.Free;
end;
< br /> for i := FLastLogLine to Log.Count-1 do
if Pos('[Globals] []', Log[i]) <>0 then
Memo1.Lines.Append(Log [i]);
// We've now processed all the lines in Log. Save
// the last line we processed as the starting point
// for the next pass.
FLastLogLine := Log.Count-1;
finally
Log.Free;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
try
ProcessLogFile;
finally
Timer1.Enabled := True;
end;
end;
end;