1
0
mirror of synced 2024-12-23 09:43:08 +01:00

Added: ExceptionEx method (to remove slightly ugly hacks in the decorator)

Changed: moved Decorator into it's own unit
This commit is contained in:
Mark van Renswoude 2014-10-20 13:38:28 +00:00
parent 136ca4b5fd
commit c5bb17cb86
5 changed files with 201 additions and 188 deletions

View File

@ -50,7 +50,8 @@ contains
X2Log.Details.Intf in '..\X2Log.Details.Intf.pas',
X2Log.Details.Registry in '..\X2Log.Details.Registry.pas',
X2Log.Util.Stream in '..\X2Log.Util.Stream.pas',
X2Log.Translations.Dutch in '..\X2Log.Translations.Dutch.pas';
X2Log.Translations.Dutch in '..\X2Log.Translations.Dutch.pas',
X2Log.Decorator in '..\X2Log.Decorator.pas';
end.

View File

@ -107,6 +107,7 @@
<DCCReference Include="..\X2Log.Details.Registry.pas"/>
<DCCReference Include="..\X2Log.Util.Stream.pas"/>
<DCCReference Include="..\X2Log.Translations.Dutch.pas"/>
<DCCReference Include="..\X2Log.Decorator.pas"/>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>

191
X2Log.Decorator.pas Normal file
View File

@ -0,0 +1,191 @@
unit X2Log.Decorator;
interface
uses
System.SysUtils,
X2Log.Intf;
type
TX2LogCategoryDecorator = class(TInterfacedObject, IX2Log)
private
FCategoryName: string;
FDecoratedLog: IX2Log;
protected
function GetCategory(const ACategory: string = ''): string;
property CategoryName: string read FCategoryName write FCategoryName;
property DecoratedLog: IX2Log read FDecoratedLog;
public
constructor Create(ADecoratedLog: IX2Log; const ACategory: string);
{ IX2LogBase }
procedure Log(ALevel: TX2LogLevel; const AMessage: string; const ACategory: string = ''; ADetails: IX2LogDetails = nil); overload;
procedure Log(ALevel: TX2LogLevel; ADateTime: TDateTime; const AMessage: string; const ACategory: string = ''; ADetails: IX2LogDetails = nil); overload;
{ IX2LogObservable }
procedure Attach(AObserver: IX2LogObserver);
procedure Detach(AObserver: IX2LogObserver);
{ IX2Log }
procedure SetExceptionStrategy(AStrategy: IX2LogExceptionStrategy);
function Category(const ACategory: string): IX2Log;
procedure Verbose(const AMessage: string; const ADetails: string = '');
procedure VerboseEx(const AMessage: string; ADetails: IX2LogDetails = nil);
procedure Info(const AMessage: string; const ADetails: string = '');
procedure InfoEx(const AMessage: string; ADetails: IX2LogDetails = nil);
procedure Warning(const AMessage: string; const ADetails: string = '');
procedure WarningEx(const AMessage: string; ADetails: IX2LogDetails = nil);
procedure Error(const AMessage: string; const ADetails: string = '');
procedure ErrorEx(const AMessage: string; ADetails: IX2LogDetails = nil);
procedure Exception(AException: Exception; const AMessage: string = '');
procedure ExceptionEx(AException: Exception; const AMessage: string = ''; const ACategory: string = '');
end;
implementation
uses
X2Log.Constants,
X2Log.Details.Default,
X2Log.Exception.Default;
{ TX2LogCategoryDecorator }
constructor TX2LogCategoryDecorator.Create(ADecoratedLog: IX2Log; const ACategory: string);
begin
inherited Create;
FDecoratedLog := ADecoratedLog;
FCategoryName := ACategory;
end;
procedure TX2LogCategoryDecorator.Log(ALevel: TX2LogLevel; const AMessage, ACategory: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(ALevel, AMessage, GetCategory(ACategory), ADetails);
end;
procedure TX2LogCategoryDecorator.Log(ALevel: TX2LogLevel; ADateTime: TDateTime; const AMessage, ACategory: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(ALevel, AMessage, GetCategory(ACategory), ADetails);
end;
procedure TX2LogCategoryDecorator.Attach(AObserver: IX2LogObserver);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Attach(AObserver);
end;
procedure TX2LogCategoryDecorator.Detach(AObserver: IX2LogObserver);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Detach(AObserver);
end;
procedure TX2LogCategoryDecorator.SetExceptionStrategy(AStrategy: IX2LogExceptionStrategy);
begin
if Assigned(DecoratedLog) then
DecoratedLog.SetExceptionStrategy(AStrategy);
end;
function TX2LogCategoryDecorator.Category(const ACategory: string): IX2Log;
begin
if Assigned(DecoratedLog) then
Result := Self.Create(DecoratedLog, GetCategory(ACategory));
end;
procedure TX2LogCategoryDecorator.Verbose(const AMessage, ADetails: string);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Verbose, AMessage, CategoryName, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
end;
procedure TX2LogCategoryDecorator.VerboseEx(const AMessage: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Verbose, AMessage, CategoryName, ADetails);
end;
procedure TX2LogCategoryDecorator.Info(const AMessage, ADetails: string);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Info, AMessage, CategoryName, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
end;
procedure TX2LogCategoryDecorator.InfoEx(const AMessage: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Info, AMessage, CategoryName, ADetails);
end;
procedure TX2LogCategoryDecorator.Warning(const AMessage, ADetails: string);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Warning, AMessage, CategoryName, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
end;
procedure TX2LogCategoryDecorator.WarningEx(const AMessage: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Warning, AMessage, CategoryName, ADetails);
end;
procedure TX2LogCategoryDecorator.Error(const AMessage, ADetails: string);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Error, AMessage, CategoryName, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
end;
procedure TX2LogCategoryDecorator.ErrorEx(const AMessage: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Error, AMessage, CategoryName, ADetails);
end;
procedure TX2LogCategoryDecorator.Exception(AException: Exception; const AMessage: string);
begin
if Assigned(DecoratedLog) then
DecoratedLog.ExceptionEx(AException, AMessage, CategoryName);
end;
procedure TX2LogCategoryDecorator.ExceptionEx(AException: Exception; const AMessage, ACategory: string);
begin
if Assigned(DecoratedLog) then
DecoratedLog.ExceptionEx(AException, AMessage, GetCategory(ACategory));
end;
function TX2LogCategoryDecorator.GetCategory(const ACategory: string): string;
begin
Result := CategoryName;
if Length(ACategory) > 0 then
Result := Result + LogCategorySeparator + ACategory;
end;
end.

View File

@ -90,6 +90,7 @@ type
procedure ErrorEx(const AMessage: string; ADetails: IX2LogDetails = nil);
procedure Exception(AException: Exception; const AMessage: string = '');
procedure ExceptionEx(AException: Exception; const AMessage: string = ''; const ACategory: string = '');
end;

193
X2Log.pas
View File

@ -11,19 +11,10 @@ uses
type
IX2LogExceptionCategory = interface
['{ED076237-65AA-4B0C-8A25-4A6B28D2F0CB}']
procedure Exception(AException: Exception; const AMessage: string = ''; const ACategory: string = '');
end;
TX2Log = class(TX2LogBaseClient, IX2Log, IX2LogExceptionCategory)
TX2Log = class(TX2LogBaseClient, IX2Log)
private
FExceptionStrategy: IX2LogExceptionStrategy;
protected
{ IX2LogExceptionCategory }
procedure Exception(AException: Exception; const AMessage: string = ''; const ACategory: string = ''); overload; virtual;
property ExceptionStrategy: IX2LogExceptionStrategy read FExceptionStrategy;
public
constructor Create;
@ -45,55 +36,15 @@ type
procedure Error(const AMessage: string; const ADetails: string = '');
procedure ErrorEx(const AMessage: string; ADetails: IX2LogDetails = nil);
procedure Exception(AException: Exception; const AMessage: string = ''); overload;
end;
TX2LogCategoryDecorator = class(TInterfacedObject, IX2Log)
private
FCategoryName: string;
FDecoratedLog: IX2Log;
protected
function GetCategory(const ACategory: string = ''): string;
property CategoryName: string read FCategoryName write FCategoryName;
property DecoratedLog: IX2Log read FDecoratedLog;
public
constructor Create(ADecoratedLog: IX2Log; const ACategory: string);
{ IX2LogBase }
procedure Log(ALevel: TX2LogLevel; const AMessage: string; const ACategory: string = ''; ADetails: IX2LogDetails = nil); overload;
procedure Log(ALevel: TX2LogLevel; ADateTime: TDateTime; const AMessage: string; const ACategory: string = ''; ADetails: IX2LogDetails = nil); overload;
{ IX2LogObservable }
procedure Attach(AObserver: IX2LogObserver);
procedure Detach(AObserver: IX2LogObserver);
{ IX2Log }
procedure SetExceptionStrategy(AStrategy: IX2LogExceptionStrategy);
function Category(const ACategory: string): IX2Log;
procedure Verbose(const AMessage: string; const ADetails: string = '');
procedure VerboseEx(const AMessage: string; ADetails: IX2LogDetails = nil);
procedure Info(const AMessage: string; const ADetails: string = '');
procedure InfoEx(const AMessage: string; ADetails: IX2LogDetails = nil);
procedure Warning(const AMessage: string; const ADetails: string = '');
procedure WarningEx(const AMessage: string; ADetails: IX2LogDetails = nil);
procedure Error(const AMessage: string; const ADetails: string = '');
procedure ErrorEx(const AMessage: string; ADetails: IX2LogDetails = nil);
procedure Exception(AException: Exception; const AMessage: string = '');
procedure ExceptionEx(AException: Exception; const AMessage: string = ''; const ACategory: string = '');
end;
implementation
uses
X2Log.Constants,
X2Log.Decorator,
X2Log.Details.Default,
X2Log.Exception.Default;
@ -172,11 +123,11 @@ end;
procedure TX2Log.Exception(AException: Exception; const AMessage: string);
begin
Exception(AException, AMessage, LogCategoryDefault);
ExceptionEx(AException, AMessage, LogCategoryDefault);
end;
procedure TX2Log.Exception(AException: Exception; const AMessage, ACategory: string);
procedure TX2Log.ExceptionEx(AException: Exception; const AMessage, ACategory: string);
var
msg: string;
details: IX2LogDetails;
@ -186,139 +137,7 @@ begin
details := nil;
ExceptionStrategy.Execute(AException, msg, details);
Log(TX2LogLevel.Error, msg, LogCategoryDefault, details);
end;
{ TX2LogCategoryDecorator }
constructor TX2LogCategoryDecorator.Create(ADecoratedLog: IX2Log; const ACategory: string);
begin
inherited Create;
FDecoratedLog := ADecoratedLog;
FCategoryName := ACategory;
end;
procedure TX2LogCategoryDecorator.Log(ALevel: TX2LogLevel; const AMessage, ACategory: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(ALevel, AMessage, GetCategory(ACategory), ADetails);
end;
procedure TX2LogCategoryDecorator.Log(ALevel: TX2LogLevel; ADateTime: TDateTime; const AMessage, ACategory: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(ALevel, AMessage, GetCategory(ACategory), ADetails);
end;
procedure TX2LogCategoryDecorator.Attach(AObserver: IX2LogObserver);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Attach(AObserver);
end;
procedure TX2LogCategoryDecorator.Detach(AObserver: IX2LogObserver);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Detach(AObserver);
end;
procedure TX2LogCategoryDecorator.SetExceptionStrategy(AStrategy: IX2LogExceptionStrategy);
begin
if Assigned(DecoratedLog) then
DecoratedLog.SetExceptionStrategy(AStrategy);
end;
function TX2LogCategoryDecorator.Category(const ACategory: string): IX2Log;
begin
if Assigned(DecoratedLog) then
Result := Self.Create(DecoratedLog, GetCategory(ACategory));
end;
procedure TX2LogCategoryDecorator.Verbose(const AMessage, ADetails: string);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Verbose, AMessage, CategoryName, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
end;
procedure TX2LogCategoryDecorator.VerboseEx(const AMessage: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Verbose, AMessage, CategoryName, ADetails);
end;
procedure TX2LogCategoryDecorator.Info(const AMessage, ADetails: string);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Info, AMessage, CategoryName, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
end;
procedure TX2LogCategoryDecorator.InfoEx(const AMessage: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Info, AMessage, CategoryName, ADetails);
end;
procedure TX2LogCategoryDecorator.Warning(const AMessage, ADetails: string);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Warning, AMessage, CategoryName, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
end;
procedure TX2LogCategoryDecorator.WarningEx(const AMessage: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Warning, AMessage, CategoryName, ADetails);
end;
procedure TX2LogCategoryDecorator.Error(const AMessage, ADetails: string);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Error, AMessage, CategoryName, TX2LogStringDetails.CreateIfNotEmpty(ADetails));
end;
procedure TX2LogCategoryDecorator.ErrorEx(const AMessage: string; ADetails: IX2LogDetails);
begin
if Assigned(DecoratedLog) then
DecoratedLog.Log(TX2LogLevel.Error, AMessage, CategoryName, ADetails);
end;
procedure TX2LogCategoryDecorator.Exception(AException: Exception; const AMessage: string);
var
exceptionCategory: IX2LogExceptionCategory;
begin
if not Assigned(DecoratedLog) then
exit;
if Supports(DecoratedLog, IX2LogExceptionCategory, exceptionCategory) then
exceptionCategory.Exception(AException, AMessage, CategoryName)
else
DecoratedLog.Log(TX2LogLevel.Error, AMessage, CategoryName);
end;
function TX2LogCategoryDecorator.GetCategory(const ACategory: string): string;
begin
Result := CategoryName;
if Length(ACategory) > 0 then
Result := Result + LogCategorySeparator + ACategory;
Log(TX2LogLevel.Error, msg, ACategory, details);
end;
end.