I have a form program in Delphi. I've made a log for different actions in the program. I'm pretty sure I need to save it in rtf file with different colors depending on the line I'm entering but don't know how to change the color. I'm also opening the file with richedit, so I can see the different colors when done.
This is the procedure I use to enter the lines previously inserted in a stringlist(logText), whenever I want the actions added to the log file(logfil). Usually I do it when there's a new login and when a session of an account is ended. At the moment I use .txt file but I'm pretty sure I need rtf?
procedure AddToLogFile();
var
i : integer;
begin
with frmMain do
begin
i := 0;
AssignFile(logFile, 'C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\LogFile.txt');
Append(logFile);
while i < logText.Count do
begin
Writeln(logFile, logText.Strings[i]);
i := i + 1;
end;
CloseFile(logFile);
end;
end;
This is how I add text to the stringlist(logText)
dateTimeNow := Now;
logText.Add('<' + DateTimeToStr(dateTimeNow) + '> A new flight was added');
And this is how I call the procedure LogFileUse.AddToLogFile;
Yes, your could use TRichEdit like this:
You may also create your log file directly in the RTF format. RTF file format is actually plain text format. It contains "tags" which select all text attribute, a bit like HTML does.
The easiest way writing your RTF file directly is to generate one sample (Like the text.rtf file generated above) and open it using notepad. You'll then easily see which tags you have to use and reproduce that in your log file.
The code below will probably help you:
Of course, you can load the generated RTF file into a TRichEdit to see it. In your real code, you should probably encapsulate all that in a nice object to make using it easier.
Another similar option is to format your log file as an HTML document.