I have a Delphi Form program, where I enter data with different accounts, and do stuff like create a new account, a new client, a new flight, and so on.
I've created a log file where different things get added to it. I want to be able to change the color of the different messages. For example: New Login: Admin (should be green); Admin logged out (should be red).
I try to do it by entering the text to a TRichEdit variable, then I try to set the color and I give it to a TMemo variable, so I can then give the text with tags to a WordPad file (RTF). After that, I try to load the file, but unsuccessfully.
I tried loading it directly to a TRichEdit, which results in getting the text with tags instead of it being formatted.
I've also tried giving it to a TMemo first then to the RichEdit, but I get an empty text.
This is the code I use to give the text from the TRichEdit to the TMemo in order to have the tags in the file later:
procedure GetLogFormatting(richEditLog : TRichEdit; textColor : TColor);
var
sStream : TStringStream;
begin
with frmMain do
begin
logText.Clear;
memLog.Clear;
sStream := TStringStream.Create;
richEditLog.Lines.SaveToStream(sStream);
richEditLog.SelAttributes.Color := textColor;
memLog.Lines.Add(sStream.DataString);
FreeAndNil(sStream);
end;
end;
This is the code I use to make the log line and then give it to the TMemo with the procedure above:
dateTimeNow := Now;
logText.Lines.Add('<' + DateTimeToStr(dateTimeNow) + '> A client was deleted');
GetLogFormatting(logText, clRed);
This is how I give the log lines to the file (I call the procedure whenever there is a change in accounts, or when the program is closed):
procedure AddToLogFile();
var
i : integer;
s : string;
begin
with frmMain do
begin
AssignFile(logFile, 'C:\LogFileColor.rtf');
Append(logFile);
for i := 0 to memLog.Lines.Count do
begin
s := memLog.Lines.Text;
Writeln(logFile, memLog.Lines[i]);
end;
CloseFile(logFile);
end;
end;
This is how I give the text from the file to the TRichEdit in the form:
procedure OpenLogForm(dateFilter : Boolean; searchDate : tDate);
var
sLine, sTime : string;
dateExists : Boolean;
newMemo : TMemo;
sStream : TStringStream;
begin
with frmMain do
begin
newMemo := TMemo.Create(frmLogSearch);
newMemo.Parent := frmLogSearch;
newMemo.Width := frmLogSearch.tLogRichEdit.Width;
newMemo.Visible := False;
sStream := TStringStream.Create;
dateExists := false;
frmLogSearch.tLogRichEdit.Clear;
AssignFile(logFile, 'C:\LogFileColor.rtf');
Reset(logFile);
while not Eof(logFile) do
begin
newMemo.Clear;
Readln(logFile, sLine);
if dateFilter then
begin
sTime := DateTimeToStr(searchDate);
if (Pos(sTime, sLine) <> 0)then
begin
newMemo.Lines.Add(sLine);
sStream.WriteString(newMemo.Lines.Text);
frmLogSearch.tLogRichEdit.Lines.LoadFromStream(sStream);
dateExists := true;
end;
end
else
begin
newMemo.Lines.Add(sLine);
sStream.WriteString(newMemo.Lines.Text);
frmLogSearch.tLogRichEdit.Lines.LoadFromStream(sStream);
dateExists := True;
end;
end;
if dateExists = false then
begin
ShowMessage('There is no such text from that date! Try another text');
end;
CloseFile(logFile);
newMemo.Destroy;
end;
end;
Can someone help me understand what I'm doing wrong, and how to fix it? I can't seem to find a lot on the Internet.