1
0
mirror of synced 2024-11-14 08:29:15 +00:00
x2log/X2Log.Observer.Event.pas
2019-10-30 15:01:19 +01:00

76 lines
1.8 KiB
ObjectPascal

unit X2Log.Observer.Event;
interface
uses
X2Log.Intf,
X2Log.Observer.Custom;
type
TX2LogEvent = procedure(Sender: TObject; Level: TX2LogLevel; DateTime: TDateTime; const Msg, Category: string; Details: IX2LogDetails) of object;
TX2LogEventObserver = class(TX2LogCustomObserver)
private
FOnLog: TX2LogEvent;
FRunInMainThread: Boolean;
protected
procedure DoLog(ALevel: TX2LogLevel; ADateTime: TDateTime; const AMessage, ACategory: string; ADetails: IX2LogDetails); override;
public
constructor Create(ALogLevels: TX2LogLevels = X2LogLevelsDefault); overload;
constructor Create(AOnLog: TX2LogEvent; ALogLevels: TX2LogLevels = X2LogLevelsDefault); overload;
property RunInMainThread: Boolean read FRunInMainThread write FRunInMainThread default True;
property OnLog: TX2LogEvent read FOnLog write FOnLog;
end;
implementation
uses
System.Classes;
{ TX2LogEventObserver }
constructor TX2LogEventObserver.Create(ALogLevels: TX2LogLevels);
begin
inherited Create(ALogLevels);
FRunInMainThread := True;
end;
constructor TX2LogEventObserver.Create(AOnLog: TX2LogEvent; ALogLevels: TX2LogLevels);
begin
Create(ALogLevels);
FOnLog := AOnLog;
end;
procedure TX2LogEventObserver.DoLog(ALevel: TX2LogLevel; ADateTime: TDateTime; const AMessage, ACategory: string; ADetails: IX2LogDetails);
var
details: IX2LogDetails;
begin
details := nil;
if Assigned(ADetails) then
details := ADetails.Clone;
if Assigned(FOnLog) then
begin
if RunInMainThread then
begin
TThread.Queue(nil,
procedure
begin
if Assigned(FOnLog) then
FOnLog(Self, ALevel, ADateTime, AMessage, ACategory, details);
end);
end else
FOnLog(Self, ALevel, ADateTime, AMessage, ACategory, details);
end;
end;
end.