IdLogStream.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.05.20 12:34:32 PM czhower
  18. Removed more non .NET compatible stream read and writes
  19. Rev 1.4 2004.01.20 10:03:30 PM czhower
  20. InitComponent
  21. Rev 1.3 2003.10.17 6:15:56 PM czhower
  22. Upgrades
  23. Rev 1.2 2003.10.17 4:28:54 PM czhower
  24. Changed stream names to be consistent with IOHandlerStream
  25. Rev 1.1 2003.10.14 1:27:12 PM czhower
  26. Uupdates + Intercept support
  27. Rev 1.0 11/13/2002 07:56:18 AM JPMugaas
  28. }
  29. unit IdLogStream;
  30. interface
  31. {$I IdCompilerDefines.inc}
  32. //Put FPC into Delphi mode
  33. uses
  34. Classes,
  35. IdLogBase, IdGlobal;
  36. type
  37. TIdLogStream = class(TIdLogBase)
  38. protected
  39. FFreeStreams: Boolean;
  40. FReceiveStream: TStream;
  41. FSendStream: TStream;
  42. //
  43. procedure InitComponent; override;
  44. procedure LogStatus(const AText: string); override;
  45. procedure LogReceivedData(const AText, AData: string); override;
  46. procedure LogSentData(const AText, AData: string); override;
  47. public
  48. procedure Disconnect; override;
  49. //
  50. property FreeStreams: Boolean read FFreeStreams write FFreeStreams;
  51. property ReceiveStream: TStream read FReceiveStream write FReceiveStream;
  52. property SendStream: TStream read FSendStream write FSendStream;
  53. end;
  54. implementation
  55. uses
  56. SysUtils;
  57. // TODO: This was orginally for VCL. For .Net what do we do? Convert back to
  58. // 7 bit? Log all? Logging all seems to be a disaster.
  59. // Text seems to be best, users are expecting text in this class. But
  60. // this write stream will dump unicode out in .net.....
  61. // So just convert it again back to 7 bit? How is proper to write
  62. // 7 bit to file? Use AnsiString?
  63. { TIdLogStream }
  64. procedure TIdLogStream.Disconnect;
  65. begin
  66. inherited Disconnect;
  67. if FreeStreams then begin
  68. FreeAndNil(FReceiveStream);
  69. FreeAndNil(FSendStream);
  70. end;
  71. end;
  72. procedure TIdLogStream.InitComponent;
  73. begin
  74. inherited InitComponent;
  75. FFreeStreams := True;
  76. end;
  77. procedure TIdLogStream.LogReceivedData(const AText, AData: string);
  78. var
  79. LEncoding: IIdTextEncoding;
  80. begin
  81. if FReceiveStream <> nil then begin
  82. LEncoding := IndyTextEncoding_8Bit;
  83. WriteStringToStream(FReceiveStream, AData, LEncoding{$IFDEF STRING_IS_ANSI}, LEncoding{$ENDIF});
  84. end;
  85. end;
  86. procedure TIdLogStream.LogSentData(const AText, AData: string);
  87. var
  88. LEncoding: IIdTextEncoding;
  89. begin
  90. if FSendStream <> nil then begin
  91. LEncoding := IndyTextEncoding_8Bit;
  92. WriteStringToStream(FSendStream, AData, LEncoding{$IFDEF STRING_IS_ANSI}, LEncoding{$ENDIF});
  93. end;
  94. end;
  95. procedure TIdLogStream.LogStatus(const AText: string);
  96. begin
  97. // We just leave this empty because the AText is not part of the stream and we
  98. // do not want to raise an abstract method exception.
  99. end;
  100. end.