IdServerInterceptLogBase.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. procedure InitComponent; override;
  47. public
  48. procedure Init; override;
  49. function Accept(AConnection: TComponent): TIdConnectionIntercept; override;
  50. destructor Destroy; 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 VCL_XE3_OR_ABOVE}
  68. System.SyncObjs,
  69. {$ENDIF}
  70. IdIOHandlerSocket,
  71. IdResourceStringsCore,
  72. IdTCPConnection,
  73. SysUtils;
  74. { TIdServerInterceptLogFile }
  75. function TIdServerInterceptLogBase.Accept(AConnection: TComponent): TIdConnectionIntercept;
  76. begin
  77. Result := TIdServerInterceptLogFileConnection.Create(nil);
  78. TIdServerInterceptLogFileConnection(Result).FServerInterceptLog := Self;
  79. TIdServerInterceptLogFileConnection(Result).LogTime := FLogTime;
  80. TIdServerInterceptLogFileConnection(Result).ReplaceCRLF := FReplaceCRLF;
  81. TIdServerInterceptLogFileConnection(Result).Active := True;
  82. end;
  83. procedure TIdServerInterceptLogBase.InitComponent;
  84. begin
  85. inherited InitComponent;
  86. FReplaceCRLF := True;
  87. FLogTime := True;
  88. FLock := TIdCriticalSection.Create;
  89. end;
  90. destructor TIdServerInterceptLogBase.Destroy;
  91. begin
  92. FreeAndNil(FLock);
  93. inherited Destroy;
  94. end;
  95. procedure TIdServerInterceptLogBase.Init;
  96. begin
  97. end;
  98. procedure TIdServerInterceptLogBase.LogWriteString(const AText: string);
  99. begin
  100. if Length(AText) > 0 then begin
  101. FLock.Enter;
  102. try
  103. if not FHasInit then begin
  104. Init; // BGO: This is just a hack, TODO find out where to call init
  105. FHasInit := True;
  106. end;
  107. DoLogWriteString(AText);
  108. finally
  109. FLock.Leave;
  110. end;
  111. end;
  112. end;
  113. { TIdServerInterceptLogFileConnection }
  114. procedure TIdServerInterceptLogFileConnection.LogReceivedData(const AText, AData: string);
  115. begin
  116. FServerInterceptLog.LogWriteString(GetConnectionID + ' ' + RSLogRecv + AText + ': ' + AData + EOL); {Do not translate}
  117. end;
  118. procedure TIdServerInterceptLogFileConnection.LogSentData(const AText, AData: string);
  119. begin
  120. FServerInterceptLog.LogWriteString(GetConnectionID + ' ' + RSLogSent + AText + ': ' + AData + EOL); {Do not translate}
  121. end;
  122. procedure TIdServerInterceptLogFileConnection.LogStatus(const AText: string);
  123. begin
  124. FServerInterceptLog.LogWriteString(GetConnectionID + ' ' + RSLogStat + AText + EOL);
  125. end;
  126. function TIdServerInterceptLogFileConnection.GetConnectionID: string;
  127. var
  128. LSocket: TIdIOHandlerSocket;
  129. begin
  130. if FConnection is TIdTCPConnection then begin
  131. LSocket := TIdTCPConnection(FConnection).Socket;
  132. if (LSocket <> nil) and (LSocket.Binding <> nil) then begin
  133. Result := LSocket.Binding.PeerIP + ':' + IntToStr(LSocket.Binding.PeerPort);
  134. Exit;
  135. end;
  136. end;
  137. Result := '0.0.0.0:0';
  138. end;
  139. end.