IdLogBase.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.5 2004.02.03 4:17:14 PM czhower
  18. For unit name changes.
  19. Rev 1.4 2004.01.20 10:03:28 PM czhower
  20. InitComponent
  21. Rev 1.3 2003.10.17 6:15:54 PM czhower
  22. Upgrades
  23. Rev 1.2 2003.10.14 1:27:08 PM czhower
  24. Uupdates + Intercept support
  25. Rev 1.1 6/16/2003 10:39:02 AM EHill
  26. Done: Expose Open/Close as public in TIdLogBase
  27. Rev 1.0 11/13/2002 07:55:58 AM JPMugaas
  28. }
  29. unit IdLogBase;
  30. interface
  31. {$I IdCompilerDefines.inc}
  32. //Put FPC into Delphi mode
  33. uses
  34. Classes,
  35. IdIntercept, IdGlobal, IdBaseComponent;
  36. type
  37. TIdLogBase = class(TIdConnectionIntercept)
  38. protected
  39. FActive: Boolean;
  40. FLogTime: Boolean;
  41. FReplaceCRLF: Boolean;
  42. //
  43. procedure InitComponent; override;
  44. procedure LogStatus(const AText: string); virtual; abstract;
  45. procedure LogReceivedData(const AText, AData: string); virtual; abstract;
  46. procedure LogSentData(const AText, AData: string); virtual; abstract;
  47. procedure SetActive(AValue: Boolean); virtual;
  48. procedure Loaded; override;
  49. function ReplaceCR(const AString : String) : String;
  50. public
  51. procedure Open; virtual;
  52. procedure Close; virtual;
  53. procedure Connect(AConnection: TComponent); override;
  54. destructor Destroy; override;
  55. procedure Disconnect; override;
  56. procedure Receive(var ABuffer: TIdBytes); override;
  57. procedure Send(var ABuffer: TIdBytes); override;
  58. published
  59. property Active: Boolean read FActive write SetActive default False;
  60. property LogTime: Boolean read FLogTime write FLogTime default True;
  61. property ReplaceCRLF: Boolean read FReplaceCRLF write FReplaceCRLF default true;
  62. end;
  63. implementation
  64. uses
  65. IdResourceStringsCore, SysUtils;
  66. const
  67. LOldStr : array [0..2] of string =
  68. ( EOL, CR, LF );
  69. LNewStr : array [0..2] of string =
  70. ( RSLogEOL, RSLogCR, RSLogLF );
  71. { TIdLogBase }
  72. procedure TIdLogBase.Close;
  73. begin
  74. end;
  75. procedure TIdLogBase.Connect(AConnection: TComponent);
  76. begin
  77. inherited Connect(AConnection);
  78. if FActive then begin
  79. LogStatus(RSLogConnected);
  80. end;
  81. end;
  82. destructor TIdLogBase.Destroy;
  83. begin
  84. Active := False;
  85. inherited Destroy;
  86. end;
  87. procedure TIdLogBase.Disconnect;
  88. begin
  89. if FActive then begin
  90. LogStatus(RSLogDisconnected);
  91. end;
  92. inherited Disconnect;
  93. end;
  94. procedure TIdLogBase.InitComponent;
  95. begin
  96. inherited InitComponent;
  97. FLogTime := True;
  98. ReplaceCRLF := True;
  99. end;
  100. procedure TIdLogBase.Loaded;
  101. var
  102. b: Boolean;
  103. begin
  104. inherited Loaded;
  105. b := FActive;
  106. FActive := False;
  107. Active := b;
  108. end;
  109. procedure TIdLogBase.Open;
  110. begin
  111. end;
  112. procedure TIdLogBase.Receive(var ABuffer: TIdBytes);
  113. var
  114. s: string;
  115. LMsg: string;
  116. begin
  117. // let the next Intercept in the chain decode its data first
  118. inherited Receive(ABuffer);
  119. if FActive then begin
  120. LMsg := '';
  121. if LogTime then begin
  122. LMsg := DateTimeToStr(Now);
  123. end;
  124. s := BytesToStringRaw(ABuffer);
  125. if FReplaceCRLF then begin
  126. s := ReplaceCR(S);
  127. end;
  128. LogReceivedData(LMsg, s);
  129. end;
  130. end;
  131. function TIdLogBase.ReplaceCR(const AString: String): String;
  132. begin
  133. Result := StringsReplace(AString, LOldStr, LNewStr);
  134. end;
  135. procedure TIdLogBase.Send(var ABuffer: TIdBytes);
  136. var
  137. s: string;
  138. LMsg: string;
  139. begin
  140. if FActive then begin
  141. LMsg := '';
  142. if LogTime then begin
  143. LMsg := DateTimeToStr(Now);
  144. end;
  145. s := BytesToStringRaw(ABuffer);
  146. if FReplaceCRLF then begin
  147. s := ReplaceCR(S);
  148. end;
  149. LogSentData(LMsg, s);
  150. end;
  151. // let the next Intercept in the chain encode its data next
  152. inherited Send(ABuffer);
  153. end;
  154. procedure TIdLogBase.SetActive(AValue: Boolean);
  155. begin
  156. if IsDesignTime or IsLoading then begin
  157. FActive := AValue;
  158. end
  159. else if FActive <> AValue then
  160. begin
  161. if AValue then begin
  162. Open;
  163. end else begin
  164. Close;
  165. end;
  166. FActive := AValue;
  167. end;
  168. end;
  169. end.