Add a message to TRichEdit based log. Message will be in a format: [CURRENT_DATE CURRENT_TIME]: MESSAGE_TEXT ... Показать код
function timetolog(reqdate: TdateTime): string;
var
Y, m, d, hh, mm, ss, ms: WORD;
newy, newm, newd, newhh, newmm, newss: string;
begin
DecodeDate(reqdate, Y, m, d);
DecodeTime(reqdate, hh, mm, ss, ms);
newy := inttostr(Y);
newm := inttostr(m);
newd := inttostr(d);
newhh := inttostr(hh);
newmm := inttostr(mm);
newss := inttostr(ss);
if length(newm) = 1 then
newm := '0' + newm;
if length(newd) = 1 then
newd := '0' + newd;
if length(newhh) = 1 then
newhh := '0' + newhh;
if length(newmm) = 1 then
newmm := '0' + newmm;
if length(newss) = 1 then
newss := '0' + newss;
Result := '[' + newd + '.' + newm + '.' + newy + ' ' + newhh + ':' + newmm +
':' + newss + '] ';
end;
procedure AddLog(ARichEdit: TRichEdit; AText: string);
var
Count: Integer;
begin
AText := timetolog(now) + AText;
Count := pos(']', AText) + 1;
if pos('ERROR', AText) > 0 then
begin
ARichEdit.Lines.Add(AText);
ARichEdit.SelStart := length(ARichEdit.Text);
ARichEdit.SelStart := ARichEdit.SelStart - length(AText) - 1;
ARichEdit.SelLength := Count;
ARichEdit.SelAttributes.Style := [fsbold];
ARichEdit.SelAttributes.Color := rgb(180, 10, 10);
ARichEdit.SelStart := ARichEdit.SelStart + Count;
ARichEdit.SelLength := length(AText) - Count;
ARichEdit.SelAttributes.Style := [];
ARichEdit.SelAttributes.Color := rgb(180, 10, 10);
ARichEdit.SelLength := 0;
end
else
begin
ARichEdit.Lines.Add(AText);
ARichEdit.SelStart := length(ARichEdit.Text);
ARichEdit.SelStart := ARichEdit.SelStart - length(AText) - 1;
ARichEdit.SelLength := Count;
ARichEdit.SelAttributes.Style := [fsbold];
ARichEdit.SelStart := ARichEdit.SelStart + Count;
ARichEdit.SelLength := length(AText) - Count;
ARichEdit.SelAttributes.Style := [];
ARichEdit.SelLength := 0;
end;
SendMessage(ARichEdit.Handle, EM_SCROLL, SB_LINEDOWN, 0);
try
ARichEdit.SetFocus;
ARichEdit.SelStart := ARichEdit.GetTextLen;
ARichEdit.Perform(EM_SCROLLCARET, 0, 0);
CreateCaret(ARichEdit.handle, 0, 0, 0);
except
//
end;
application.ProcessMessages;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
AddLog(Form1.RichEdit1, 'ERROR: Something Wrong!'); //Error Message
AddLog(Form1.RichEdit1, 'Some text here'); //Regular Message
end;