brookfcleventlogbroker.pas 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. (*
  2. Brook for Free Pascal
  3. Copyright (C) 2014-2019 Silvio Clecio
  4. See the file LICENSE.txt, included in this distribution,
  5. for details about the copyright.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. *)
  10. { FCL EventLog broker. }
  11. unit BrookFCLEventLogBroker;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. BrookLog, BrookUtils, EventLog, SysUtils, Classes;
  16. type
  17. { TBrookFCLEventLog }
  18. TBrookFCLEventLog = class(TBrookLogger)
  19. private
  20. FLogger: TEventLog;
  21. public
  22. constructor Create(AOwner: TComponent); override;
  23. destructor Destroy; override;
  24. procedure Prepare; override;
  25. procedure Log(const AType: TBrookLogType; const S: string;
  26. const ACode: Word; const E: Exception = nil); override;
  27. end;
  28. implementation
  29. { TBrookFCLEventLog }
  30. constructor TBrookFCLEventLog.Create(AOwner: TComponent);
  31. begin
  32. inherited Create(AOwner);
  33. FLogger := TEventLog.Create(nil);
  34. FLogger.Identification := ApplicationName;
  35. end;
  36. destructor TBrookFCLEventLog.Destroy;
  37. begin
  38. FreeAndNil(FLogger);
  39. inherited Destroy;
  40. end;
  41. procedure TBrookFCLEventLog.Prepare;
  42. begin
  43. inherited Prepare;
  44. FLogger.Active := False;
  45. case Output of
  46. loFile: FLogger.LogType := EventLog.ltFile;
  47. loSystem: FLogger.LogType := EventLog.ltSystem;
  48. end;
  49. if FileName = '' then
  50. FLogger.FileName := BrookSettings.LogFile
  51. else
  52. FLogger.FileName := FileName;
  53. FLogger.RaiseExceptionOnError := False;
  54. FLogger.AppendContent := True;
  55. if Active then
  56. FLogger.Active := Active
  57. else
  58. FLogger.Active := BrookSettings.LogActive;
  59. end;
  60. procedure TBrookFCLEventLog.Log(const AType: TBrookLogType; const S: string;
  61. const ACode: Word; const E: Exception);
  62. var
  63. X: string;
  64. begin
  65. if not (AType in Types) then
  66. Exit;
  67. if FLogger.Active then
  68. begin
  69. X := S;
  70. if Assigned(E) then
  71. X += '<Error>' + LineEnding +
  72. Format('%s exception was raised with the following message: %s',
  73. [E.ClassName, E.Message]) + LineEnding +
  74. BrookDumpStack(LineEnding) + LineEnding +
  75. BrookDumpStackTrace(LineEnding) + '</Error>';
  76. case AType of
  77. ltCustom:
  78. begin
  79. FLogger.CustomLogType := ACode;
  80. FLogger.Log(etCustom, X);
  81. end;
  82. ltInfo: FLogger.Log(etInfo, X);
  83. ltWarning: FLogger.Log(etWarning, X);
  84. ltError: FLogger.Log(etError, X);
  85. ltDebug: FLogger.Log(etDebug, X);
  86. end;
  87. end;
  88. end;
  89. initialization
  90. TBrookFCLEventLog.RegisterService;
  91. finalization
  92. TBrookFCLEventLog.UnregisterService;
  93. end.