testbrooklogger.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. unit testbrooklogger;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. BrookLogger, BrookClasses, fpcunit, testregistry, Classes, sysutils;
  6. type
  7. { TLog }
  8. TLog = class
  9. private
  10. FLogFile: TStrings;
  11. FLogInFile: Boolean;
  12. public
  13. constructor Create; virtual;
  14. destructor Destroy; override;
  15. procedure Log(const S: string);
  16. property LogFile: TStrings read FLogFile;
  17. property LogInFile: Boolean read FLogInFile write FLogInFile;
  18. end;
  19. { TBrokerLog }
  20. TBrokerLog = class(TBrookInterfacedObject, IBrookLogger)
  21. private
  22. FLog: TLog;
  23. FOutput: TBrookLogOutput;
  24. procedure SetOutput(const AValue: TBrookLogOutput);
  25. function GetOutput: TBrookLogOutput;
  26. public
  27. constructor Create;
  28. destructor Destroy; override;
  29. function Instance: TObject;
  30. procedure Custom(const S: string; const ACode: Word);
  31. procedure Info(const S: string);
  32. procedure Warn(const S: string);
  33. procedure Debug(const S: string);
  34. procedure Error(const S: string; E: Exception = nil);
  35. property Output: TBrookLogOutput read GetOutput write SetOutput;
  36. end;
  37. { TTestBrookLogger }
  38. TTestBrookLogger = class(TTestCase)
  39. published
  40. procedure TestCustom;
  41. procedure TestInfo;
  42. procedure TestWarn;
  43. procedure TestDebug;
  44. procedure TestError;
  45. procedure TestOutput;
  46. end;
  47. implementation
  48. { TLog }
  49. constructor TLog.Create;
  50. begin
  51. inherited Create;
  52. FLogFile := TStringList.Create;
  53. end;
  54. destructor TLog.Destroy;
  55. begin
  56. FLogFile.Free;
  57. inherited Destroy;
  58. end;
  59. procedure TLog.Log(const S: string);
  60. begin
  61. if FLogInFile then
  62. FLogFile.Add(S);
  63. end;
  64. { TBrokerLog }
  65. constructor TBrokerLog.Create;
  66. begin
  67. inherited Create;
  68. FLog := TLog.Create;
  69. Output := loSystem;
  70. SetOutput(loFile);
  71. end;
  72. destructor TBrokerLog.Destroy;
  73. begin
  74. FLog.Free;
  75. inherited Destroy;
  76. end;
  77. function TBrokerLog.Instance: TObject;
  78. begin
  79. Result := FLog;
  80. end;
  81. procedure TBrokerLog.SetOutput(const AValue: TBrookLogOutput);
  82. begin
  83. FOutput := AValue;
  84. FLog.LogInFile := AValue = loFile;
  85. end;
  86. function TBrokerLog.GetOutput: TBrookLogOutput;
  87. begin
  88. Result := FOutput;
  89. end;
  90. procedure TBrokerLog.Custom(const S: string; const ACode: Word);
  91. begin
  92. FLog.Log(Format('Custom(%d)=%s', [ACode, S]));
  93. end;
  94. procedure TBrokerLog.Info(const S: string);
  95. begin
  96. FLog.Log('Info=' + S);
  97. end;
  98. procedure TBrokerLog.Warn(const S: string);
  99. begin
  100. FLog.Log('Warn=' + S);
  101. end;
  102. procedure TBrokerLog.Debug(const S: string);
  103. begin
  104. FLog.Log('Debug=' + S);
  105. end;
  106. procedure TBrokerLog.Error(const S: string; E: Exception);
  107. begin
  108. FLog.Log('Error=' + S + ': ' + E.Message);
  109. end;
  110. { TTestBrookLogger }
  111. procedure TTestBrookLogger.TestCustom;
  112. begin
  113. BrookLog.Custom('Custom log', 1001);
  114. AssertEquals(TLog(BrookLog.Instance).LogFile.Values['Custom(1001)'],
  115. 'Custom log');
  116. end;
  117. procedure TTestBrookLogger.TestInfo;
  118. begin
  119. BrookLog.Info('Info log');
  120. AssertEquals(TLog(BrookLog.Instance).LogFile.Values['Info'], 'Info log');
  121. end;
  122. procedure TTestBrookLogger.TestWarn;
  123. begin
  124. BrookLog.Warn('Warn log');
  125. AssertEquals(TLog(BrookLog.Instance).LogFile.Values['Warn'], 'Warn log');
  126. end;
  127. procedure TTestBrookLogger.TestDebug;
  128. begin
  129. BrookLog.Debug('Debug log');
  130. AssertEquals(TLog(BrookLog.Instance).LogFile.Values['Debug'], 'Debug log');
  131. end;
  132. procedure TTestBrookLogger.TestError;
  133. var
  134. e: Exception;
  135. begin
  136. e := Exception.Create('Error');
  137. try
  138. BrookLog.Error('Error log', e);
  139. AssertEquals(TLog(BrookLog.Instance).LogFile.Values['Error'],
  140. 'Error log: Error');
  141. finally
  142. e.Free;
  143. end;
  144. end;
  145. procedure TTestBrookLogger.TestOutput;
  146. begin
  147. AssertTrue(BrookLog.Output = loFile);
  148. end;
  149. initialization
  150. BrookRegisterLog(TBrokerLog.Create);
  151. RegisterTest(TTestBrookLogger);
  152. end.