BrookLogger.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. { Contains classes for basic logging. }
  26. unit BrookLogger;
  27. {$I BrookDefines.inc}
  28. interface
  29. uses
  30. RTLConsts,
  31. {$IFDEF FPC}
  32. Math,
  33. {$ELSE}
  34. Types,
  35. IOUtils,
  36. {$ENDIF}
  37. SysUtils,
  38. DateUtils,
  39. Classes,
  40. BrookExtra;
  41. const
  42. { Default logger name. }
  43. BROOK_LOGGER_OUTPUT_NAME = 'Console';
  44. { Default logger tag. }
  45. BROOK_LOGGER_TAG = 'BrookLogger_';
  46. resourcestring
  47. { Error message @code('Empty output name.'). }
  48. SBrookEmptyOutputName = 'Empty output name.';
  49. { Error message @code('Active output log.'). }
  50. SBrookActiveOutput = 'Active output log.';
  51. { Error message @code('Inactive output log.'). }
  52. SBrookInactiveOutput = 'Inactive output log.';
  53. { Error message @code('Invalid output class: <class-name>.'). }
  54. SBrookInvalidOutputClass = 'Invalid output class: %s.';
  55. { Error message @code('Unknown output name: <output-name>.'). }
  56. SBrookUnknownOutputName = 'Unknown output name: %s.';
  57. { Name for information log level. }
  58. SBrookLevelInfo = 'INFO';
  59. { Name for hint log level. }
  60. SBrookLevelHint = 'HINT';
  61. { Name for warning log level. }
  62. SBrookLevelWarn = 'WARN';
  63. { Name for debug log level. }
  64. SBrookLevelDebug = 'DEBUG';
  65. { Name for error log level. }
  66. SBrookLevelError = 'ERROR';
  67. type
  68. { Abstract class for logger output. }
  69. TBrookLoggerOutput = class abstract(TPersistent)
  70. private
  71. FFilters: TStringList;
  72. FOptions: TStringList;
  73. class function InternalFormat(const ALevel,
  74. AMessage: string): string; inline;
  75. protected
  76. class function FormatLog(const ALevel, AMessage: string): string; virtual;
  77. class function FormatFail(const ALevel: string;
  78. AException: Exception): string; virtual;
  79. public
  80. { Creates an instance of @code(TBrookLoggerOutput).
  81. @param(AFilters[in] Filters to be assigned to the logger instance.)
  82. @param(AOptions[in] Options to be assigned to the logger instance.) }
  83. constructor Create(AFilters, AOptions: TStringList); virtual;
  84. { Returns the alias name for output source.
  85. @returns(Output source alias.) }
  86. class function GetRegisterAlias: string; virtual;
  87. { Returns the name for output source.
  88. @returns(Output source name.) }
  89. class function GetName: string; virtual; abstract;
  90. { Returns @True if a certain log level is filtered.
  91. @param(ALevel[in] Log level.) }
  92. function IsFiltered(const ALevel: string): Boolean; virtual;
  93. { Appends a message to the output log.
  94. @param(ALevel[in] Log level.)
  95. @param(AMessage[in] Log message.) }
  96. procedure Log(const ALevel, AMessage: string); virtual; abstract;
  97. { Appends an exception message to the output log.
  98. @param(ALevel[in] Log level.)
  99. @param(AException[in] Log exception.) }
  100. procedure Fail(const ALevel: string;
  101. AException: Exception); virtual; abstract;
  102. { List containing the filtered log levels. }
  103. property Filters: TStringList read FFilters;
  104. { List containing additional options to the output. }
  105. property Options: TStringList read FOptions;
  106. end;
  107. { Class-reference for @code(TBrookLoggerOutput). }
  108. TBrookLoggerOutputClass = class of TBrookLoggerOutput;
  109. { Class for console logger output. }
  110. TBrookLoggerOutputConsole = class(TBrookLoggerOutput)
  111. public
  112. { Returns the name for output source.
  113. @returns(Output source name.) }
  114. class function GetName: string; override;
  115. { Appends a message to the output log.
  116. @param(ALevel[in] Log level.)
  117. @param(AMessage[in] Log message.) }
  118. procedure Log(const ALevel, AMessage: string); override;
  119. { Appends an exception message to the output log.
  120. @param(ALevel[in] Log level.)
  121. @param(AException[in] Log exception.) }
  122. procedure Fail(const ALevel: string; AException: Exception); override;
  123. end;
  124. { Class for file logger output. }
  125. TBrookLoggerOutputFile = class(TBrookLoggerOutput)
  126. private
  127. FHandle: TFileStream;
  128. FEncoding: TEncoding;
  129. FLastDate: TDate;
  130. FDirectory: string;
  131. FFileName: TFileName;
  132. procedure SetDirectory(const AValue: string);
  133. protected
  134. function CreateFile(AEncoding: TEncoding;
  135. const AFileName: TFileName): TFileStream; overload; virtual;
  136. function CreateFile(
  137. const AFileName: TFileName): TFileStream; overload; virtual;
  138. function RecreateFile(const AFileName: TFileName): TFileStream; virtual;
  139. procedure UpgradeFileName; virtual;
  140. procedure UpgradeFile; virtual;
  141. procedure WriteLog(const AMsg: string); inline;
  142. property LastDate: TDate read FLastDate;
  143. property Handle: TFileStream read FHandle;
  144. public
  145. { Method triggered after the constructor is called. }
  146. procedure AfterConstruction; override;
  147. { Destroys an instance of @code(TBrookLoggerOutputFile). }
  148. destructor Destroy; override;
  149. { Returns the name for output source.
  150. @returns(Output source name.) }
  151. class function GetName: string; override;
  152. { Appends a message to the output log.
  153. @param(ALevel[in] Log level.)
  154. @param(AMessage[in] Log message.) }
  155. procedure Log(const ALevel, AMessage: string); override;
  156. { Appends an exception message to the output log.
  157. @param(ALevel[in] Log level.)
  158. @param(AException[in] Log exception.) }
  159. procedure Fail(const ALevel: string; AException: Exception); override;
  160. { Specifies the output directory containing the logs. }
  161. property Directory: string read FDirectory write SetDirectory;
  162. { Generated absolute filename for the log. }
  163. property FileName: TFileName read FFileName;
  164. end;
  165. { Class that retains the log levels. }
  166. TBrookLoggerLevels = class(TPersistent)
  167. private
  168. FInfo: string;
  169. FHint: string;
  170. FWarn: string;
  171. FDebug: string;
  172. FError: string;
  173. function IsInfoStored: Boolean;
  174. function IsHintStored: Boolean;
  175. function IsWarnStored: Boolean;
  176. function IsDebugStored: Boolean;
  177. function IsErrorStored: Boolean;
  178. public
  179. { Creates an instance of @code(TBrookLoggerLevels). }
  180. constructor Create; virtual;
  181. { Copies the properties of the source levels.
  182. @param(ASource[in] Levels source to be copied.) }
  183. procedure Assign(ASource: TPersistent); override;
  184. published
  185. { Level message for information log. }
  186. property Info: string read FInfo write FInfo stored IsInfoStored;
  187. { Level message for hint log. }
  188. property Hint: string read FHint write FHint stored IsHintStored;
  189. { Level message for warning log. }
  190. property Warn: string read FWarn write FWarn stored IsWarnStored;
  191. { Level message for debug log. }
  192. property Debug: string read FDebug write FDebug stored IsDebugStored;
  193. { Level message for error log. }
  194. property Error: string read FError write FError stored IsErrorStored;
  195. end;
  196. { Component that writes log to a predefined output type. }
  197. TBrookLogger = class(TComponent)
  198. private
  199. FOutput: TBrookLoggerOutput;
  200. FFilters: TStringList;
  201. FOptions: TStringList;
  202. FLevels: TBrookLoggerLevels;
  203. FOutputName: string;
  204. FStreamedActive: Boolean;
  205. FActive: Boolean;
  206. function GetOutput: TBrookLoggerOutput;
  207. procedure SetActive(AValue: Boolean);
  208. procedure SetOutputName(const AValue: string);
  209. function IsActiveStored: Boolean;
  210. function IsOutputNameStored: Boolean;
  211. procedure SetFilters(AValue: TStringList);
  212. procedure SetOptions(AValue: TStringList);
  213. protected
  214. procedure Loaded; override;
  215. function CreateFilters: TStringList; virtual;
  216. function CreateOptions: TStringList; virtual;
  217. function CreateOutput(AFilters,
  218. AOptions: TStringList): TBrookLoggerOutput; virtual;
  219. function CreateLevels: TBrookLoggerLevels; virtual;
  220. procedure DoOpen; virtual;
  221. procedure DoClose; virtual;
  222. procedure CheckActive; inline;
  223. procedure CheckInactive; inline;
  224. procedure CheckOutputName; inline;
  225. public
  226. { Creates an instance of @code(TBrookLogger).
  227. @param(AOwner[in] Owner component.) }
  228. constructor Create(AOwner: TComponent); override;
  229. { Destroys an instance of @code(TBrookLogger). }
  230. destructor Destroy; override;
  231. { Gets an output log class from the classes register. }
  232. function GetOutputClass: TBrookLoggerOutputClass; inline;
  233. { Enabled the logger component. }
  234. procedure Open;
  235. { Disables the logger component. }
  236. procedure Close;
  237. { Appends a message to the output log.
  238. @param(ALevel[in] Log level.)
  239. @param(AMessage[in] Log message.) }
  240. procedure Log(const ALevel, AMessage: string); inline;
  241. { Appends an exception message to the output log.
  242. @param(ALevel[in] Log level.)
  243. @param(AException[in] Log exception.) }
  244. procedure Fail(const ALevel: string; AException: Exception); inline;
  245. { Appends a message to the output log as information level.
  246. @param(AMessage[in] Log message.) }
  247. procedure Info(const AMessage: string); inline;
  248. { Appends a message to the output log as hint level.
  249. @param(AMessage[in] Log message.) }
  250. procedure Hint(const AMessage: string); inline;
  251. { Appends a message to the output log as warning level.
  252. @param(AMessage[in] Log message.) }
  253. procedure Warn(const AMessage: string); inline;
  254. { Appends a message to the output log as debug level.
  255. @param(AMessage[in] Log message.) }
  256. procedure Debug(const AMessage: string); inline;
  257. { Appends a message to the output log as error level.
  258. @param(AMessage[in] Log message.) }
  259. procedure Error(AException: Exception); inline;
  260. { Current active log output. }
  261. property Output: TBrookLoggerOutput read GetOutput;
  262. published
  263. { Activates the logger component. }
  264. property Active: Boolean read FActive write SetActive stored IsActiveStored;
  265. { Retains the log levels. }
  266. property Levels: TBrookLoggerLevels read FLevels write FLevels;
  267. { Name of the chosen output type. }
  268. property OutputName: string read FOutputName write SetOutputName
  269. stored IsOutputNameStored;
  270. { List containing the filtered log levels. }
  271. property Filters: TStringList read FFilters write SetFilters;
  272. { List containing additional options to the chosen output. }
  273. property Options: TStringList read FOptions write SetOptions;
  274. end;
  275. implementation
  276. { TBrookLoggerOutput }
  277. constructor TBrookLoggerOutput.Create(AFilters, AOptions: TStringList);
  278. begin
  279. inherited Create;
  280. if not Assigned(AFilters) then
  281. raise EArgumentNilException.CreateFmt(SParamIsNil, ['AFilters']);
  282. if not Assigned(AOptions) then
  283. raise EArgumentNilException.CreateFmt(SParamIsNil, ['AOptions']);
  284. FFilters := AFilters;
  285. FOptions := AOptions;
  286. end;
  287. class function TBrookLoggerOutput.GetRegisterAlias: string;
  288. begin
  289. Result := Concat(BROOK_LOGGER_TAG, GetName);
  290. end;
  291. class function TBrookLoggerOutput.InternalFormat(const ALevel,
  292. AMessage: string): string;
  293. begin
  294. Result := Concat(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now), ' ', ALevel,
  295. ': ', AMessage.TrimRight);
  296. end;
  297. class function TBrookLoggerOutput.FormatLog(const ALevel,
  298. AMessage: string): string;
  299. begin
  300. Result := InternalFormat(ALevel, AMessage);
  301. end;
  302. class function TBrookLoggerOutput.FormatFail(const ALevel: string;
  303. AException: Exception): string;
  304. begin
  305. if not Assigned(AException) then
  306. raise EArgumentNilException.CreateFmt(SParamIsNil, ['AException']);
  307. Result := FormatLog(ALevel, Concat(AException.ClassName, ': ',
  308. AException.Message));
  309. end;
  310. function TBrookLoggerOutput.IsFiltered(const ALevel: string): Boolean;
  311. begin
  312. Result := FFilters.IndexOf(ALevel) > -1;
  313. end;
  314. { TBrookLoggerOutputConsole }
  315. class function TBrookLoggerOutputConsole.GetName: string;
  316. begin
  317. Result := 'Console';
  318. end;
  319. procedure TBrookLoggerOutputConsole.Log(const ALevel, AMessage: string);
  320. begin
  321. if IsConsole and not IsFiltered(ALevel) then
  322. WriteLn(Output, FormatLog(ALevel, AMessage));
  323. end;
  324. procedure TBrookLoggerOutputConsole.Fail(const ALevel: string;
  325. AException: Exception);
  326. begin
  327. if IsConsole and not IsFiltered(ALevel) then
  328. WriteLn(ErrOutput, FormatFail(ALevel, AException));
  329. end;
  330. { TBrookLoggerOutputFile }
  331. procedure TBrookLoggerOutputFile.AfterConstruction;
  332. begin
  333. inherited AfterConstruction;
  334. if not FDirectory.IsEmpty then
  335. Exit;
  336. FDirectory := FOptions.Values['Directory'];
  337. if FDirectory.IsEmpty then
  338. FDirectory :=
  339. {$IFDEF FPC}
  340. GetUserDir
  341. {$ELSE}
  342. TPath.GetHomePath
  343. {$ENDIF};
  344. UpgradeFileName;
  345. end;
  346. destructor TBrookLoggerOutputFile.Destroy;
  347. begin
  348. FHandle.Free;
  349. inherited Destroy;
  350. end;
  351. function TBrookLoggerOutputFile.CreateFile(AEncoding: TEncoding;
  352. const AFileName: TFileName): TFileStream;
  353. var
  354. VMode: Word;
  355. begin
  356. FEncoding := AEncoding;
  357. if not Assigned(FEncoding) then
  358. FEncoding := TEncoding.UTF8;
  359. if FileExists(AFileName) then
  360. VMode := fmOpenReadWrite
  361. else
  362. VMode := fmCreate;
  363. VMode := VMode or fmShareDenyWrite;
  364. Result := TFileStream.Create(AFileName, VMode, BROOK_FILE_RIGHTS);
  365. end;
  366. function TBrookLoggerOutputFile.CreateFile(
  367. const AFileName: TFileName): TFileStream;
  368. begin
  369. Result := CreateFile(TEncoding.UTF8, AFileName);
  370. end;
  371. function TBrookLoggerOutputFile.RecreateFile(
  372. const AFileName: TFileName): TFileStream;
  373. begin
  374. FHandle.Free;
  375. Result := CreateFile(FEncoding, AFileName);
  376. end;
  377. procedure TBrookLoggerOutputFile.UpgradeFileName;
  378. var
  379. VDate: TDate;
  380. begin
  381. VDate := Date;
  382. if CompareDateTime(IncDay(FLastDate), VDate) <> LessThanValue then
  383. Exit;
  384. FLastDate := VDate;
  385. FFileName := Concat(IncludeTrailingPathDelimiter(FDirectory),
  386. ChangeFileExt(ExtractFileName(ParamStr(0)), ''), '_',
  387. FormatDateTime('yyyymmdd', FLastDate), '.log');
  388. end;
  389. procedure TBrookLoggerOutputFile.UpgradeFile;
  390. begin
  391. if not FDirectory.IsEmpty then
  392. ForceDirectories(FDirectory);
  393. UpgradeFileName;
  394. FHandle := RecreateFile(FFileName);
  395. end;
  396. procedure TBrookLoggerOutputFile.SetDirectory(const AValue: string);
  397. begin
  398. if AValue = FDirectory then
  399. Exit;
  400. FDirectory := AValue;
  401. FLastDate := 0;
  402. end;
  403. procedure TBrookLoggerOutputFile.WriteLog(const AMsg: string);
  404. var
  405. VBuffer: TBytes;
  406. begin
  407. if not Assigned(FHandle) then
  408. Exit;
  409. VBuffer := FEncoding.GetBytes(
  410. {$IFDEF FPC}UnicodeString({$ENDIF}Concat(AMsg, sLineBreak){$IFDEF FPC}){$ENDIF});
  411. FHandle.Seek(0, TSeekOrigin.soEnd);
  412. FHandle.WriteBuffer(VBuffer[0], FEncoding.GetCharCount(VBuffer));
  413. end;
  414. class function TBrookLoggerOutputFile.GetName: string;
  415. begin
  416. Result := 'File';
  417. end;
  418. procedure TBrookLoggerOutputFile.Log(const ALevel, AMessage: string);
  419. begin
  420. if IsFiltered(ALevel) then
  421. Exit;
  422. UpgradeFile;
  423. WriteLog(FormatLog(ALevel, AMessage));
  424. end;
  425. procedure TBrookLoggerOutputFile.Fail(const ALevel: string;
  426. AException: Exception);
  427. begin
  428. if IsFiltered(ALevel) then
  429. Exit;
  430. UpgradeFile;
  431. WriteLog(FormatFail(ALevel, AException));
  432. end;
  433. { TBrookLoggerLevels }
  434. constructor TBrookLoggerLevels.Create;
  435. begin
  436. inherited Create;
  437. FInfo := SBrookLevelInfo;
  438. FHint := SBrookLevelHint;
  439. FWarn := SBrookLevelWarn;
  440. FDebug := SBrookLevelDebug;
  441. FError := SBrookLevelError;
  442. end;
  443. procedure TBrookLoggerLevels.Assign(ASource: TPersistent);
  444. var
  445. VSrc: TBrookLoggerLevels;
  446. begin
  447. if ASource is TBrookLoggerLevels then
  448. begin
  449. VSrc := ASource as TBrookLoggerLevels;
  450. FInfo := VSrc.Info;
  451. FHint := VSrc.Hint;
  452. FWarn := VSrc.Warn;
  453. FDebug := VSrc.Debug;
  454. FError := VSrc.Error;
  455. end
  456. else
  457. inherited Assign(ASource);
  458. end;
  459. function TBrookLoggerLevels.IsInfoStored: Boolean;
  460. begin
  461. Result := FInfo <> SBrookLevelInfo;
  462. end;
  463. function TBrookLoggerLevels.IsHintStored: Boolean;
  464. begin
  465. Result := FHint <> SBrookLevelHint;
  466. end;
  467. function TBrookLoggerLevels.IsWarnStored: Boolean;
  468. begin
  469. Result := FWarn <> SBrookLevelWarn;
  470. end;
  471. function TBrookLoggerLevels.IsDebugStored: Boolean;
  472. begin
  473. Result := FDebug <> SBrookLevelDebug;
  474. end;
  475. function TBrookLoggerLevels.IsErrorStored: Boolean;
  476. begin
  477. Result := FError <> SBrookLevelError;
  478. end;
  479. { TBrookLogger }
  480. constructor TBrookLogger.Create(AOwner: TComponent);
  481. begin
  482. inherited Create(AOwner);
  483. FLevels := CreateLevels;
  484. FFilters := CreateFilters;
  485. FOptions := CreateOptions;
  486. FOutputName := BROOK_LOGGER_OUTPUT_NAME;
  487. end;
  488. destructor TBrookLogger.Destroy;
  489. begin
  490. SetActive(False);
  491. FLevels.Free;
  492. FOptions.Free;
  493. FFilters.Free;
  494. inherited Destroy;
  495. end;
  496. function TBrookLogger.CreateFilters: TStringList;
  497. begin
  498. Result := TStringList.Create;
  499. Result.Add(FLevels.Info);
  500. Result.Add(FLevels.Hint);
  501. Result.Add(FLevels.Debug);
  502. end;
  503. function TBrookLogger.CreateOptions: TStringList;
  504. begin
  505. Result := TStringList.Create;
  506. end;
  507. function TBrookLogger.GetOutputClass: TBrookLoggerOutputClass;
  508. var
  509. C: TPersistentClass;
  510. N: string;
  511. begin
  512. N := Concat(BROOK_LOGGER_TAG, FOutputName);
  513. C := Classes.GetClass(N);
  514. if not Assigned(C) then
  515. raise EClassNotFound.CreateFmt(SBrookUnknownOutputName, [FOutputName]);
  516. if not C.InheritsFrom(TBrookLoggerOutput) then
  517. raise EInvalidCast.CreateFmt(SBrookInvalidOutputClass, [C.ClassName]);
  518. Result := TBrookLoggerOutputClass(C);
  519. end;
  520. function TBrookLogger.CreateOutput(AFilters,
  521. AOptions: TStringList): TBrookLoggerOutput;
  522. begin
  523. Result := GetOutputClass.Create(AFilters, AOptions);
  524. end;
  525. function TBrookLogger.CreateLevels: TBrookLoggerLevels;
  526. begin
  527. Result := TBrookLoggerLevels.Create;
  528. end;
  529. procedure TBrookLogger.CheckOutputName;
  530. begin
  531. if FOutputName.IsEmpty then
  532. raise EArgumentException.Create(SBrookEmptyOutputName);
  533. end;
  534. procedure TBrookLogger.CheckActive;
  535. begin
  536. if not Active then
  537. raise EInvalidOpException.Create(SBrookInactiveOutput);
  538. end;
  539. procedure TBrookLogger.CheckInactive;
  540. begin
  541. if (not (csLoading in ComponentState)) and Active then
  542. raise EInvalidOpException.Create(SBrookActiveOutput);
  543. end;
  544. procedure TBrookLogger.Loaded;
  545. begin
  546. inherited Loaded;
  547. try
  548. if FStreamedActive then
  549. SetActive(True);
  550. except
  551. if csDesigning in ComponentState then
  552. begin
  553. if Assigned(ApplicationHandleException) then
  554. ApplicationHandleException(ExceptObject)
  555. else
  556. ShowException(ExceptObject, ExceptAddr);
  557. end
  558. else
  559. raise;
  560. end;
  561. end;
  562. procedure TBrookLogger.DoOpen;
  563. begin
  564. if Assigned(FOutput) then
  565. Exit;
  566. CheckOutputName;
  567. FOutput := CreateOutput(FFilters, FOptions);
  568. FActive := True;
  569. end;
  570. procedure TBrookLogger.DoClose;
  571. begin
  572. if not Assigned(FOutput) then
  573. Exit;
  574. FOutput.Destroy;
  575. FOutput := nil;
  576. FActive := False;
  577. end;
  578. function TBrookLogger.IsActiveStored: Boolean;
  579. begin
  580. Result := FActive;
  581. end;
  582. function TBrookLogger.GetOutput: TBrookLoggerOutput;
  583. begin
  584. CheckActive;
  585. Result := FOutput;
  586. end;
  587. function TBrookLogger.IsOutputNameStored: Boolean;
  588. begin
  589. Result := FOutputName <> BROOK_LOGGER_OUTPUT_NAME;
  590. end;
  591. procedure TBrookLogger.SetActive(AValue: Boolean);
  592. begin
  593. if AValue = FActive then
  594. Exit;
  595. if csDesigning in ComponentState then
  596. FActive := AValue
  597. else
  598. if AValue then
  599. begin
  600. if csReading in ComponentState then
  601. FStreamedActive := True
  602. else
  603. DoOpen;
  604. end
  605. else
  606. DoClose;
  607. end;
  608. procedure TBrookLogger.SetFilters(AValue: TStringList);
  609. begin
  610. FFilters.Assign(AValue);
  611. end;
  612. procedure TBrookLogger.SetOptions(AValue: TStringList);
  613. begin
  614. FOptions.Assign(AValue);
  615. end;
  616. procedure TBrookLogger.SetOutputName(const AValue: string);
  617. begin
  618. if FOutputName = AValue then
  619. Exit;
  620. if not FStreamedActive then
  621. CheckInactive;
  622. FOutputName := AValue;
  623. if FOutputName.IsEmpty then
  624. FOutputName := BROOK_LOGGER_OUTPUT_NAME;
  625. end;
  626. procedure TBrookLogger.Open;
  627. begin
  628. SetActive(True);
  629. end;
  630. procedure TBrookLogger.Close;
  631. begin
  632. SetActive(False);
  633. end;
  634. procedure TBrookLogger.Log(const ALevel, AMessage: string);
  635. begin
  636. if Active then
  637. Output.Log(ALevel, AMessage);
  638. end;
  639. procedure TBrookLogger.Fail(const ALevel: string; AException: Exception);
  640. begin
  641. if Active then
  642. Output.Fail(ALevel, AException);
  643. end;
  644. procedure TBrookLogger.Info(const AMessage: string);
  645. begin
  646. Log(FLevels.Info, AMessage);
  647. end;
  648. procedure TBrookLogger.Hint(const AMessage: string);
  649. begin
  650. Log(FLevels.Hint, AMessage);
  651. end;
  652. procedure TBrookLogger.Warn(const AMessage: string);
  653. begin
  654. Log(FLevels.Warn, AMessage);
  655. end;
  656. procedure TBrookLogger.Debug(const AMessage: string);
  657. begin
  658. Log(FLevels.Debug, AMessage);
  659. end;
  660. procedure TBrookLogger.Error(AException: Exception);
  661. begin
  662. Fail(FLevels.Error, AException);
  663. end;
  664. initialization
  665. RegisterClassAlias(TBrookLoggerOutputConsole,
  666. TBrookLoggerOutputConsole.GetRegisterAlias);
  667. RegisterClassAlias(TBrookLoggerOutputFile,
  668. TBrookLoggerOutputFile.GetRegisterAlias);
  669. finalization
  670. UnregisterClass(TBrookLoggerOutputConsole);
  671. UnregisterClass(TBrookLoggerOutputFile);
  672. end.