IdLogBase.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10231: IdLogBase.pas
  11. {
  12. { Rev 1.0 2002.11.12 10:44:00 PM czhower
  13. }
  14. unit IdLogBase;
  15. interface
  16. uses
  17. Classes,
  18. IdIntercept,
  19. IdSocketHandle;
  20. type
  21. TIdLogBase = class(TIdConnectionIntercept)
  22. protected
  23. FActive: Boolean;
  24. FLogTime: Boolean;
  25. FReplaceCRLF: Boolean;
  26. FStreamedActive: Boolean;
  27. //
  28. procedure Close; virtual;
  29. procedure LogStatus(const AText: string); virtual; abstract;
  30. procedure LogReceivedData(const AText: string; const AData: string); virtual; abstract;
  31. procedure LogSentData(const AText: string; const AData: string); virtual; abstract;
  32. procedure Open; virtual;
  33. procedure SetActive(const AValue: Boolean); virtual;
  34. procedure Loaded; override;
  35. public
  36. procedure Connect(AConnection: TComponent); override;
  37. constructor Create(AOwner: TComponent); override;
  38. procedure Receive(ABuffer: TStream); override;
  39. procedure Send(ABuffer: TStream); override;
  40. destructor Destroy; override;
  41. procedure Disconnect; override;
  42. published
  43. property Active: Boolean read FActive write SetActive default False;
  44. property LogTime: Boolean read FLogTime write FLogTime default True;
  45. property ReplaceCRLF: Boolean read FReplaceCRLF write FReplaceCRLF default true;
  46. end;
  47. implementation
  48. uses
  49. IdGlobal,
  50. IdResourceStrings,
  51. SysUtils;
  52. { TIdLogBase }
  53. procedure TIdLogBase.Close;
  54. begin
  55. end;
  56. procedure TIdLogBase.Connect(AConnection: TComponent);
  57. begin
  58. if FActive then
  59. begin
  60. inherited Connect(AConnection);
  61. LogStatus(RSLogConnected);
  62. end;
  63. end;
  64. constructor TIdLogBase.Create(AOwner: TComponent);
  65. begin
  66. inherited Create(AOwner);
  67. FLogTime := True;
  68. ReplaceCRLF := True;
  69. end;
  70. destructor TIdLogBase.Destroy;
  71. begin
  72. Active := False;
  73. inherited Destroy;
  74. end;
  75. procedure TIdLogBase.Disconnect;
  76. begin
  77. if FActive then
  78. begin
  79. LogStatus(RSLogDisconnected);
  80. inherited Disconnect;
  81. end;
  82. end;
  83. procedure TIdLogBase.Loaded;
  84. begin
  85. Active := FStreamedActive;
  86. end;
  87. procedure TIdLogBase.Open;
  88. begin
  89. end;
  90. procedure TIdLogBase.Receive(ABuffer: TStream);
  91. var
  92. s: string;
  93. LMsg: string;
  94. begin
  95. if FActive then
  96. begin
  97. inherited Receive(ABuffer);
  98. with TStringStream.Create('') do try {Do not translate}
  99. CopyFrom(ABuffer, ABuffer.Size);
  100. LMsg := ''; {Do not translate}
  101. if LogTime then begin
  102. LMsg := DateTimeToStr(Now);
  103. end;
  104. s := DataString;
  105. if FReplaceCRLF then begin
  106. s := StringReplace(s, EOL, RSLogEOL, [rfReplaceAll]);
  107. s := StringReplace(s, CR, RSLogCR, [rfReplaceAll]);
  108. s := StringReplace(s, LF, RSLogLF, [rfReplaceAll]);
  109. end;
  110. LogReceivedData(LMsg, s);
  111. finally
  112. Free;
  113. end;
  114. end;
  115. end;
  116. procedure TIdLogBase.Send(ABuffer: TStream);
  117. var
  118. s: string;
  119. LMsg: string;
  120. begin
  121. if FActive then
  122. begin
  123. inherited Send(ABuffer);
  124. with TStringStream.Create('') do try
  125. CopyFrom(ABuffer, ABuffer.Size);
  126. LMsg := '';
  127. if LogTime then begin
  128. LMsg := DateTimeToStr(Now);
  129. end;
  130. s := DataString;
  131. if FReplaceCRLF then begin
  132. s := StringReplace(s, EOL, RSLogEOL, [rfReplaceAll]);
  133. s := StringReplace(s, CR, RSLogCR, [rfReplaceAll]);
  134. s := StringReplace(s, LF, RSLogLF, [rfReplaceAll]);
  135. end;
  136. LogSentData(LMsg, s);
  137. finally Free; end;
  138. end;
  139. end;
  140. procedure TIdLogBase.SetActive(const AValue: Boolean);
  141. begin
  142. if (csReading in ComponentState) then
  143. FStreamedActive := AValue
  144. else
  145. if FActive <> AValue then
  146. begin
  147. FActive := AValue;
  148. if FActive then
  149. Open
  150. else
  151. Close;
  152. end;
  153. end;
  154. end.