IdLogFile.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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: 10237: IdLogFile.pas
  11. {
  12. { Rev 1.1 7/23/04 6:13:28 PM RLebeau
  13. { TFileStream access right tweak for Open()
  14. }
  15. {
  16. { Rev 1.0 2002.11.12 10:44:30 PM czhower
  17. }
  18. unit IdLogFile;
  19. {
  20. Revision History:
  21. 19-Aug-2001 DSiders Fixed bug in Open. Use file mode fmCreate when Filename
  22. does *not* exist.
  23. 19-Aug-2001 DSiders Added protected method TIdLogFile.LogWriteString.
  24. 19-Aug-2001 DSiders Changed implementation of TIdLogFile methods LogStatus,
  25. LogReceivedData, and LogSentData to use LogWriteString.
  26. 19-Aug-2001 DSiders Added class TIdLogFileEx with the LogFormat method.
  27. }
  28. interface
  29. uses
  30. Classes,
  31. IdLogBase,
  32. SysUtils;
  33. type
  34. TIdLogFile = class(TIdLogBase)
  35. protected
  36. FFilename: TFilename;
  37. FFileStream: TFileStream;
  38. //
  39. procedure Close; override;
  40. procedure LogFormat(const AFormat: string; const AArgs: array of const); virtual;
  41. procedure LogReceivedData(const AText: string; const AData: string); override;
  42. procedure LogSentData(const AText: string; const AData: string); override;
  43. procedure LogStatus(const AText: string); override;
  44. procedure LogWriteString(const AText: string); virtual;
  45. procedure Open; override;
  46. public
  47. published
  48. property Filename: TFilename read FFilename write FFilename;
  49. end;
  50. implementation
  51. uses
  52. IdGlobal,
  53. IdResourceStrings;
  54. { TIdLogFile }
  55. procedure TIdLogFile.Close;
  56. begin
  57. FreeAndNil(FFileStream);
  58. end;
  59. procedure TIdLogFile.LogReceivedData(const AText, AData: string);
  60. begin
  61. LogWriteString(RSLogRecv + AText + ': ' + AData + EOL); {Do not translate}
  62. end;
  63. procedure TIdLogFile.LogSentData(const AText, AData: string);
  64. begin
  65. LogWriteString(RSLogSent + AText + ': ' + AData + EOL); {Do not translate}
  66. end;
  67. procedure TIdLogFile.LogStatus(const AText: string);
  68. begin
  69. LogWriteString(RSLogStat + AText + EOL);
  70. end;
  71. procedure TIdLogFile.Open;
  72. begin
  73. if not (csDesigning in ComponentState) then begin
  74. if not FileExists(Filename) then begin
  75. FFileStream := TFileStream.Create(Filename, fmCreate);
  76. end else begin
  77. FFileStream := TFileStream.Create(Filename, fmOpenReadWrite or fmShareDenyWrite);
  78. FFileStream.Position := FFileStream.Size;
  79. end;
  80. end;
  81. end;
  82. procedure TIdLogFile.LogWriteString(const AText: string);
  83. begin
  84. if Length(AText) > 0 then begin
  85. FFileStream.WriteBuffer(AText[1], Length(AText));
  86. end;
  87. end;
  88. procedure TIdLogFile.LogFormat(const AFormat: string; const AArgs: array of const);
  89. var
  90. sPre: string;
  91. sMsg: string;
  92. sData: string;
  93. begin
  94. // forces Open to be called prior to Connect
  95. if not Active then
  96. begin
  97. Active := True;
  98. end;
  99. sPre := ''; {Do not translate}
  100. sMsg := ''; {Do not translate}
  101. if LogTime then
  102. begin
  103. sPre := DateTimeToStr(Now) + ' ' ; {Do not translate}
  104. end;
  105. sData := Format(AFormat, AArgs);
  106. if FReplaceCRLF then begin
  107. sData := StringReplace(sData, EOL, RSLogEOL, [rfReplaceAll]);
  108. sData := StringReplace(sData, CR, RSLogCR, [rfReplaceAll]);
  109. sData := StringReplace(sData, LF, RSLogLF, [rfReplaceAll]);
  110. end;
  111. sMsg := sPre + sData + EOL;
  112. LogWriteString(sMsg);
  113. end;
  114. end.