IdLogFile.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.8 7/23/04 6:36:54 PM RLebeau
  18. Added extra exception handling to Open()
  19. Rev 1.7 2004.05.20 12:34:30 PM czhower
  20. Removed more non .NET compatible stream read and writes
  21. Rev 1.6 2004.02.03 4:17:16 PM czhower
  22. For unit name changes.
  23. Rev 1.5 2003.10.17 6:15:54 PM czhower
  24. Upgrades
  25. Rev 1.4 2003.10.16 11:24:36 AM czhower
  26. Bug fix
  27. Rev 1.3 10/15/2003 8:00:10 PM DSiders
  28. Added resource string for exception raised in TIdLogFile.SetFilename.
  29. Rev 1.2 2003.10.14 1:27:10 PM czhower
  30. Uupdates + Intercept support
  31. Rev 1.1 6/16/2003 11:01:06 AM EHill
  32. Throw exception if the filename is set while the log is open.
  33. Expose Open and Close as public instead of protected.
  34. Rev 1.0 11/13/2002 07:56:12 AM JPMugaas
  35. 19-Aug-2001 DSiders
  36. Fixed bug in Open. Use file mode fmCreate when Filename does *not* exist.
  37. 19-Aug-2001 DSiders
  38. Added protected method TIdLogFile.LogWriteString.
  39. 19-Aug-2001 DSiders
  40. Changed implementation of TIdLogFile methods LogStatus, LogReceivedData, and
  41. LogSentData to use LogWriteString.
  42. 19-Aug-2001 DSiders
  43. Added class TIdLogFileEx with the LogFormat method.
  44. }
  45. unit IdLogFile;
  46. interface
  47. {$I IdCompilerDefines.inc}
  48. //Put FPC into Delphi mode
  49. uses
  50. Classes,
  51. IdLogBase;
  52. type
  53. TIdLogFile = class(TIdLogBase)
  54. protected
  55. FFilename: String;
  56. FFileStream: TStream;
  57. //
  58. procedure LogFormat(const AFormat: string; const AArgs: array of const); virtual;
  59. procedure LogReceivedData(const AText, AData: string); override;
  60. procedure LogSentData(const AText, AData: string); override;
  61. procedure LogStatus(const AText: string); override;
  62. procedure LogWriteString(const AText: string); virtual;
  63. //
  64. procedure SetFilename(const AFilename: String);
  65. public
  66. procedure Open; override;
  67. procedure Close; override;
  68. published
  69. property Filename: String read FFilename write SetFilename;
  70. end;
  71. implementation
  72. uses
  73. IdGlobal, IdException, IdResourceStringsCore, IdBaseComponent, SysUtils;
  74. { TIdLogFile }
  75. procedure TIdLogFile.Close;
  76. begin
  77. FreeAndNil(FFileStream);
  78. end;
  79. procedure TIdLogFile.LogReceivedData(const AText, AData: string);
  80. begin
  81. LogWriteString(RSLogRecv + AText + ': ' + AData + EOL); {Do not translate}
  82. end;
  83. procedure TIdLogFile.LogSentData(const AText, AData: string);
  84. begin
  85. LogWriteString(RSLogSent + AText + ': ' + AData + EOL); {Do not translate}
  86. end;
  87. procedure TIdLogFile.LogStatus(const AText: string);
  88. begin
  89. LogWriteString(RSLogStat + AText + EOL);
  90. end;
  91. procedure TIdLogFile.Open;
  92. begin
  93. // RLebeau: this check is redundant, as TIdLogBase.SetActive() already checks for this before calling Open()...
  94. if not IsDesignTime then begin
  95. FFileStream := TIdAppendFileStream.Create(Filename);
  96. end;
  97. end;
  98. procedure TIdLogFile.LogWriteString(const AText: string);
  99. var
  100. LEncoding: IIdTextEncoding;
  101. begin
  102. if Assigned(FFileStream) then begin
  103. LEncoding := IndyTextEncoding_8Bit;
  104. WriteStringToStream(FFileStream, AText, LEncoding{$IFDEF STRING_IS_ANSI}, LEncoding{$ENDIF});
  105. end;
  106. end;
  107. procedure TIdLogFile.LogFormat(const AFormat: string; const AArgs: array of const);
  108. var
  109. sPre: string;
  110. sMsg: string;
  111. sData: string;
  112. begin
  113. // forces Open to be called prior to Connect
  114. if not Active then begin
  115. Active := True;
  116. end;
  117. sPre := ''; {Do not translate}
  118. sMsg := ''; {Do not translate}
  119. if LogTime then begin
  120. sPre := DateTimeToStr(Now) + ' '; {Do not translate}
  121. end;
  122. sData := IndyFormat(AFormat, AArgs);
  123. if FReplaceCRLF then begin
  124. sData := ReplaceCR(sData);
  125. end;
  126. sMsg := sPre + sData + EOL;
  127. LogWriteString(sMsg);
  128. end;
  129. procedure TIdLogFile.SetFilename(const AFilename: String);
  130. begin
  131. if Assigned(FFileStream) then begin
  132. raise EIdException.Create(RSLogFileAlreadyOpen); // TODO: create a new Exception class for this
  133. end;
  134. FFilename := AFilename;
  135. end;
  136. end.