Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add LoggerPro.FMXMemoAppender.pas #43

Open
ImageProcessingFMX opened this issue Aug 2, 2020 · 2 comments
Open

add LoggerPro.FMXMemoAppender.pas #43

ImageProcessingFMX opened this issue Aug 2, 2020 · 2 comments

Comments

@ImageProcessingFMX
Copy link

can you please add FMX logger support, like below or change by compiler directive

unit LoggerPro.FMXMemoAppender;
{ <@abstract(The unit to include if you want to use the @link(TFMXMemoLogAppender))
@author(Daniele Teti) }

interface

uses
LoggerPro,
System.Classes,
FMX.StdCtrls;

type
{ @abstract(Appends formatted @link(TLogItem) to a TMemo in a VCL application) }
TFMXMemoLogAppender = class(TLoggerProAppenderBase)
private
FMemo: TMemo;
FMaxLogLines: Word;
FClearOnStartup: Boolean;
public
constructor Create(aMemo: TMemo; aMaxLogLines: Word = 100; aClearOnStartup: Boolean = False); reintroduce;
procedure Setup; override;
procedure TearDown; override;
procedure WriteLog(const aLogItem: TLogItem); override;
end;

implementation

uses
System.SysUtils,
Winapi.Windows,
Winapi.Messages;

const
DEFAULT_LOG_FORMAT = '%0:s [TID %1:-8d][%2:-10s] %3:s [%4:s]';

{ TVCLMemoLogAppender }

constructor TFMXMemoLogAppender.Create(aMemo: TMemo; aMaxLogLines: Word; aClearOnStartup: Boolean);
begin
inherited Create;
FMemo := aMemo;
FMaxLogLines := aMaxLogLines;
FClearOnStartup := aClearOnStartup;
end;

procedure TFMXMemoLogAppender.Setup;
begin
if FClearOnStartup then
begin
TThread.Synchronize(nil,
procedure
begin
FMemo.Clear;
end);
end;
end;

procedure TFMXMemoLogAppender.TearDown;
begin
// do nothing
end;

procedure TFMXMemoLogAppender.WriteLog(const aLogItem: TLogItem);
var
lText: string;
begin
lText := Format(DEFAULT_LOG_FORMAT, [datetimetostr(aLogItem.TimeStamp), aLogItem.ThreadID, aLogItem.LogTypeAsString, aLogItem.LogMessage,
aLogItem.LogTag]);
TThread.Queue(nil,
procedure
begin
FMemo.Lines.BeginUpdate;
try
if FMemo.Lines.Count = FMaxLogLines then
FMemo.Lines.Delete(0);
FMemo.Lines.Add(lText)
finally
FMemo.Lines.EndUpdate;
end;
SendMessage(FMemo.Handle, EM_SCROLLCARET, 0, 0);
end);
end;

end.

@danieleteti
Copy link
Owner

Why don't use the "Unit Scope" for this?

@ImageProcessingFMX
Copy link
Author

ImageProcessingFMX commented May 23, 2023

where can I get this unit ?
How to use ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants