| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- unit testbrooklogger;
- {$mode objfpc}{$H+}
- interface
- uses
- BrookLogger, BrookClasses, fpcunit, testregistry, Classes, sysutils;
- type
- { TLog }
- TLog = class
- private
- FLogFile: TStrings;
- FLogInFile: Boolean;
- public
- constructor Create; virtual;
- destructor Destroy; override;
- procedure Log(const S: string);
- property LogFile: TStrings read FLogFile;
- property LogInFile: Boolean read FLogInFile write FLogInFile;
- end;
- { TBrokerLog }
- TBrokerLog = class(TBrookInterfacedObject, IBrookLogger)
- private
- FLog: TLog;
- FOutput: TBrookLogOutput;
- procedure SetOutput(const AValue: TBrookLogOutput);
- function GetOutput: TBrookLogOutput;
- public
- constructor Create;
- destructor Destroy; override;
- function Instance: TObject;
- procedure Custom(const S: string; const ACode: Word);
- procedure Info(const S: string);
- procedure Warn(const S: string);
- procedure Debug(const S: string);
- procedure Error(const S: string; E: Exception = nil);
- property Output: TBrookLogOutput read GetOutput write SetOutput;
- end;
- { TTestBrookLogger }
- TTestBrookLogger = class(TTestCase)
- published
- procedure TestCustom;
- procedure TestInfo;
- procedure TestWarn;
- procedure TestDebug;
- procedure TestError;
- procedure TestOutput;
- end;
- implementation
- { TLog }
- constructor TLog.Create;
- begin
- inherited Create;
- FLogFile := TStringList.Create;
- end;
- destructor TLog.Destroy;
- begin
- FLogFile.Free;
- inherited Destroy;
- end;
- procedure TLog.Log(const S: string);
- begin
- if FLogInFile then
- FLogFile.Add(S);
- end;
- { TBrokerLog }
- constructor TBrokerLog.Create;
- begin
- inherited Create;
- FLog := TLog.Create;
- Output := loSystem;
- SetOutput(loFile);
- end;
- destructor TBrokerLog.Destroy;
- begin
- FLog.Free;
- inherited Destroy;
- end;
- function TBrokerLog.Instance: TObject;
- begin
- Result := FLog;
- end;
- procedure TBrokerLog.SetOutput(const AValue: TBrookLogOutput);
- begin
- FOutput := AValue;
- FLog.LogInFile := AValue = loFile;
- end;
- function TBrokerLog.GetOutput: TBrookLogOutput;
- begin
- Result := FOutput;
- end;
- procedure TBrokerLog.Custom(const S: string; const ACode: Word);
- begin
- FLog.Log(Format('Custom(%d)=%s', [ACode, S]));
- end;
- procedure TBrokerLog.Info(const S: string);
- begin
- FLog.Log('Info=' + S);
- end;
- procedure TBrokerLog.Warn(const S: string);
- begin
- FLog.Log('Warn=' + S);
- end;
- procedure TBrokerLog.Debug(const S: string);
- begin
- FLog.Log('Debug=' + S);
- end;
- procedure TBrokerLog.Error(const S: string; E: Exception);
- begin
- FLog.Log('Error=' + S + ': ' + E.Message);
- end;
- { TTestBrookLogger }
- procedure TTestBrookLogger.TestCustom;
- begin
- BrookLog.Custom('Custom log', 1001);
- AssertEquals(TLog(BrookLog.Instance).LogFile.Values['Custom(1001)'],
- 'Custom log');
- end;
- procedure TTestBrookLogger.TestInfo;
- begin
- BrookLog.Info('Info log');
- AssertEquals(TLog(BrookLog.Instance).LogFile.Values['Info'], 'Info log');
- end;
- procedure TTestBrookLogger.TestWarn;
- begin
- BrookLog.Warn('Warn log');
- AssertEquals(TLog(BrookLog.Instance).LogFile.Values['Warn'], 'Warn log');
- end;
- procedure TTestBrookLogger.TestDebug;
- begin
- BrookLog.Debug('Debug log');
- AssertEquals(TLog(BrookLog.Instance).LogFile.Values['Debug'], 'Debug log');
- end;
- procedure TTestBrookLogger.TestError;
- var
- e: Exception;
- begin
- e := Exception.Create('Error');
- try
- BrookLog.Error('Error log', e);
- AssertEquals(TLog(BrookLog.Instance).LogFile.Values['Error'],
- 'Error log: Error');
- finally
- e.Free;
- end;
- end;
- procedure TTestBrookLogger.TestOutput;
- begin
- AssertTrue(BrookLog.Output = loFile);
- end;
- initialization
- BrookRegisterLog(TBrokerLog.Create);
- RegisterTest(TTestBrookLogger);
- end.
|