Browse Source

* Add EventFilter, use SafeFormat

Michaël Van Canneyt 1 year ago
parent
commit
d1a5ecd87e
1 changed files with 21 additions and 5 deletions
  1. 21 5
      packages/fcl-base/src/eventlog.pp

+ 21 - 5
packages/fcl-base/src/eventlog.pp

@@ -27,6 +27,10 @@ uses System.SysUtils,System.Classes;
 uses SysUtils,Classes;
 uses SysUtils,Classes;
 {$ENDIF FPC_DOTTEDUNITS}
 {$ENDIF FPC_DOTTEDUNITS}
 
 
+const
+  AllEvents = [Low(TEventType)..High(TEventType)];
+
+
 Type
 Type
   TEventLog = Class;
   TEventLog = Class;
   TLogType = (ltSystem,ltFile,ltStdOut,ltStdErr);
   TLogType = (ltSystem,ltFile,ltStdOut,ltStdErr);
@@ -53,6 +57,7 @@ Type
     FOnGetCustomEvent : TLogCodeEvent;
     FOnGetCustomEvent : TLogCodeEvent;
     FOnLogMessage: TLogMessageEvent;
     FOnLogMessage: TLogMessageEvent;
     FPaused : Boolean;
     FPaused : Boolean;
+    FEventFilter : TEventTypes;
     procedure SetActive(const Value: Boolean);
     procedure SetActive(const Value: Boolean);
     procedure SetIdentification(const Value: String);
     procedure SetIdentification(const Value: String);
     procedure SetlogType(const Value: TLogType);
     procedure SetlogType(const Value: TLogType);
@@ -81,6 +86,7 @@ Type
     Function MapTypeToCategory(EventType : TEventType) : Word;
     Function MapTypeToCategory(EventType : TEventType) : Word;
     Function MapTypeToEventID(EventType : TEventType) : DWord;
     Function MapTypeToEventID(EventType : TEventType) : DWord;
   Public
   Public
+    Constructor Create(aOwner: TComponent); override;
     Destructor Destroy; override;
     Destructor Destroy; override;
     Function EventTypeToString(E : TEventType) : String;
     Function EventTypeToString(E : TEventType) : String;
     Function RegisterMessageFile(AFileName : String) : Boolean; virtual;
     Function RegisterMessageFile(AFileName : String) : Boolean; virtual;
@@ -102,6 +108,7 @@ Type
   Published
   Published
     Property AppendContent : Boolean Read fAppendContent Write fAppendContent;
     Property AppendContent : Boolean Read fAppendContent Write fAppendContent;
     Property Identification : String Read FIdentification Write SetIdentification;
     Property Identification : String Read FIdentification Write SetIdentification;
+    Property EventFilter : TEventTypes Read FEventFilter Write FEventFilter default allevents;
     Property LogType : TLogType Read Flogtype Write SetlogType;
     Property LogType : TLogType Read Flogtype Write SetlogType;
     Property Active : Boolean Read FActive write SetActive;
     Property Active : Boolean Read FActive write SetActive;
     Property RaiseExceptionOnError : Boolean Read FRaiseExceptionOnError Write FRaiseExceptionOnError;
     Property RaiseExceptionOnError : Boolean Read FRaiseExceptionOnError Write FRaiseExceptionOnError;
@@ -141,6 +148,13 @@ implementation
 Resourcestring
 Resourcestring
   SErrOperationNotAllowed = 'Operation not allowed when eventlog is active.';
   SErrOperationNotAllowed = 'Operation not allowed when eventlog is active.';
 
 
+Constructor TEventLog.Create(aOwner: TComponent); 
+
+begin
+  Inherited;
+  FEventFilter:=AllEvents;
+end;
+
 procedure TEventLog.CheckInactive;
 procedure TEventLog.CheckInactive;
 begin
 begin
   If Active then
   If Active then
@@ -205,13 +219,15 @@ end;
 procedure TEventLog.Log(EventType: TEventType; const Fmt: String;
 procedure TEventLog.Log(EventType: TEventType; const Fmt: String;
   Args: array of const);
   Args: array of const);
 begin
 begin
-  Log(EventType,Format(Fmt,Args));
+  Log(EventType,SafeFormat(Fmt,Args));
 end;
 end;
 
 
 procedure TEventLog.Log(EventType: TEventType; const Msg: String);
 procedure TEventLog.Log(EventType: TEventType; const Msg: String);
 begin
 begin
   If Paused then 
   If Paused then 
     exit;
     exit;
+  if not (EventType in EventFilter) then 
+    exit;
   EnsureActive;
   EnsureActive;
   Case FlogType of
   Case FlogType of
     ltFile   : WriteFileLog(EventType,Msg);
     ltFile   : WriteFileLog(EventType,Msg);
@@ -231,7 +247,7 @@ begin
     FTimeStampFormat:='yyyy-mm-dd hh:nn:ss.zzz';
     FTimeStampFormat:='yyyy-mm-dd hh:nn:ss.zzz';
   TS:=FormatDateTime(FTimeStampFormat,Now);
   TS:=FormatDateTime(FTimeStampFormat,Now);
   T:=EventTypeToString(EventType);
   T:=EventTypeToString(EventType);
-  Result:=Format('%s [%s %s] %s',[Identification,TS,T,Msg]);
+  Result:=SafeFormat('%s [%s %s] %s',[Identification,TS,T,Msg]);
 end;
 end;
 
 
 procedure TEventLog.WriteFileLog(EventType : TEventType; const Msg : String);
 procedure TEventLog.WriteFileLog(EventType : TEventType; const Msg : String);
@@ -259,7 +275,7 @@ end;
 
 
 procedure TEventLog.Log(const Fmt: String; Args: array of const);
 procedure TEventLog.Log(const Fmt: String; Args: array of const);
 begin
 begin
-  Log(Format(Fmt,Args));
+  Log(SafeFormat(Fmt,Args));
 end;
 end;
 
 
 procedure TEventLog.SetActive(const Value: Boolean);
 procedure TEventLog.SetActive(const Value: Boolean);
@@ -353,7 +369,7 @@ end;
 
 
 procedure TEventLog.Warning(const Fmt: String; Args: array of const);
 procedure TEventLog.Warning(const Fmt: String; Args: array of const);
 begin
 begin
-  Warning(Format(Fmt,Args));
+  Warning(SafeFormat(Fmt,Args));
 end;
 end;
 
 
 procedure TEventLog.Warning(const Msg: String);
 procedure TEventLog.Warning(const Msg: String);
@@ -382,7 +398,7 @@ begin
     etWarning : Result:=SLogWarning;
     etWarning : Result:=SLogWarning;
     etError   : Result:=SLogError;
     etError   : Result:=SLogError;
     etDebug   : Result:=SLogDebug;
     etDebug   : Result:=SLogDebug;
-    etCustom  : Result:=Format(SLogCustom,[CustomLogType]);
+    etCustom  : Result:=SafeFormat(SLogCustom,[CustomLogType]);
   end;
   end;
 end;
 end;