2
0

IdServerInterceptLogBase.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.6 1/19/05 11:18:16 AM RLebeau
  18. bug fix for GetConnectionID()
  19. Rev 1.5 2004.02.03 5:45:38 PM czhower
  20. Name changes
  21. Rev 1.4 2004.01.22 5:58:58 PM czhower
  22. IdCriticalSection
  23. Rev 1.3 1/21/2004 4:03:20 PM JPMugaas
  24. InitComponent
  25. Rev 1.2 2003.10.17 6:15:02 PM czhower
  26. consts removed
  27. Rev 1.1 2003.10.14 1:31:14 PM czhower
  28. DotNet
  29. Rev 1.0 3/22/2003 10:59:20 PM BGooijen
  30. Initial check in.
  31. ServerIntercept to ease debugging, data/status are logged to a file
  32. }
  33. unit IdServerInterceptLogBase;
  34. interface
  35. {$i IdCompilerDefines.inc}
  36. uses
  37. IdIntercept, IdGlobal, IdLogBase, IdBaseComponent, Classes;
  38. type
  39. TIdServerInterceptLogBase = class(TIdServerIntercept)
  40. protected
  41. FLock: TIdCriticalSection;
  42. FLogTime: Boolean;
  43. FReplaceCRLF: Boolean;
  44. //
  45. FHasInit: Boolean; // BGO: can be removed later, see comment below (.Init)
  46. public
  47. constructor Create(AOwner: TComponent); override;
  48. destructor Destroy; override;
  49. procedure Init; override;
  50. function Accept(AConnection: TComponent): TIdConnectionIntercept; override;
  51. procedure DoLogWriteString(const AText: string); virtual; abstract;
  52. procedure LogWriteString(const AText: string); virtual;
  53. published
  54. property LogTime: Boolean read FLogTime write FLogTime default True;
  55. property ReplaceCRLF: Boolean read FReplaceCRLF write FReplaceCRLF default true;
  56. end;
  57. TIdServerInterceptLogFileConnection = class(TIdLogBase) //BGO: i just love long class names <g>
  58. protected
  59. FServerInterceptLog:TIdServerInterceptLogBase;
  60. procedure LogReceivedData(const AText, AData: string); override;
  61. procedure LogSentData(const AText, AData: string); override;
  62. procedure LogStatus(const AText: string); override;
  63. function GetConnectionID: string; virtual;
  64. end;
  65. implementation
  66. uses
  67. {$IFDEF DCC_XE3_OR_ABOVE}
  68. System.SyncObjs,
  69. {$ENDIF}
  70. IdIOHandlerSocket,
  71. IdResourceStringsCore,
  72. IdTCPConnection,
  73. SysUtils;
  74. { TIdServerInterceptLogFile }
  75. constructor TIdServerInterceptLogBase.Create(AOwner: TComponent);
  76. begin
  77. inherited Create(AOwner;
  78. FReplaceCRLF := True;
  79. FLogTime := True;
  80. FLock := TIdCriticalSection.Create;
  81. end;
  82. destructor TIdServerInterceptLogBase.Destroy;
  83. begin
  84. FLock.Free;
  85. inherited Destroy;
  86. end;
  87. function TIdServerInterceptLogBase.Accept(AConnection: TComponent): TIdConnectionIntercept;
  88. var
  89. LConn: TIdServerInterceptLogFileConnection;
  90. begin
  91. LConn := TIdServerInterceptLogFileConnection.Create(nil);
  92. LConn.FServerInterceptLog := Self;
  93. LConn.LogTime := FLogTime;
  94. LConn.ReplaceCRLF := FReplaceCRLF;
  95. LConn.Active := True;
  96. Result := LConn;
  97. end;
  98. procedure TIdServerInterceptLogBase.Init;
  99. begin
  100. end;
  101. procedure TIdServerInterceptLogBase.LogWriteString(const AText: string);
  102. begin
  103. if AText <> '' then begin
  104. FLock.Enter;
  105. try
  106. if not FHasInit then begin
  107. Init; // BGO: This is just a hack, TODO find out where to call init
  108. FHasInit := True;
  109. end;
  110. DoLogWriteString(AText);
  111. finally
  112. FLock.Leave;
  113. end;
  114. end;
  115. end;
  116. { TIdServerInterceptLogFileConnection }
  117. procedure TIdServerInterceptLogFileConnection.LogReceivedData(const AText, AData: string);
  118. begin
  119. FServerInterceptLog.LogWriteString(GetConnectionID + ' ' + RSLogRecv + AText + ': ' + AData + EOL); {Do not translate}
  120. end;
  121. procedure TIdServerInterceptLogFileConnection.LogSentData(const AText, AData: string);
  122. begin
  123. FServerInterceptLog.LogWriteString(GetConnectionID + ' ' + RSLogSent + AText + ': ' + AData + EOL); {Do not translate}
  124. end;
  125. procedure TIdServerInterceptLogFileConnection.LogStatus(const AText: string);
  126. begin
  127. FServerInterceptLog.LogWriteString(GetConnectionID + ' ' + RSLogStat + AText + EOL);
  128. end;
  129. function TIdServerInterceptLogFileConnection.GetConnectionID: string;
  130. var
  131. LSocket: TIdIOHandlerSocket;
  132. begin
  133. if FConnection is TIdTCPConnection then begin
  134. LSocket := TIdTCPConnection(FConnection).Socket;
  135. if (LSocket <> nil) and (LSocket.Binding <> nil) then begin
  136. Result := LSocket.Binding.PeerIP + ':' + IntToStr(LSocket.Binding.PeerPort);
  137. Exit;
  138. end;
  139. end;
  140. Result := '0.0.0.0:0';
  141. end;
  142. end.