LogFile_WIN32.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // LogFile_WIN32.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/LogFile_WIN32.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: LogFile
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/LogFile_WIN32.h"
  16. #include "Poco/File.h"
  17. #include "Poco/Exception.h"
  18. namespace Poco {
  19. LogFileImpl::LogFileImpl(const std::string& path): _path(path), _hFile(INVALID_HANDLE_VALUE)
  20. {
  21. File file(path);
  22. if (file.exists())
  23. {
  24. if (0 == sizeImpl())
  25. _creationDate = file.getLastModified();
  26. else
  27. _creationDate = file.created();
  28. }
  29. }
  30. LogFileImpl::~LogFileImpl()
  31. {
  32. CloseHandle(_hFile);
  33. }
  34. void LogFileImpl::writeImpl(const std::string& text, bool flush)
  35. {
  36. if (INVALID_HANDLE_VALUE == _hFile) createFile();
  37. DWORD bytesWritten;
  38. BOOL res = WriteFile(_hFile, text.data(), (DWORD) text.size(), &bytesWritten, NULL);
  39. if (!res) throw WriteFileException(_path);
  40. res = WriteFile(_hFile, "\r\n", 2, &bytesWritten, NULL);
  41. if (!res) throw WriteFileException(_path);
  42. if (flush)
  43. {
  44. res = FlushFileBuffers(_hFile);
  45. if (!res) throw WriteFileException(_path);
  46. }
  47. }
  48. UInt64 LogFileImpl::sizeImpl() const
  49. {
  50. if (INVALID_HANDLE_VALUE == _hFile)
  51. {
  52. File file(_path);
  53. if (file.exists()) return file.getSize();
  54. else return 0;
  55. }
  56. LARGE_INTEGER li;
  57. li.HighPart = 0;
  58. li.LowPart = SetFilePointer(_hFile, 0, &li.HighPart, FILE_CURRENT);
  59. return li.QuadPart;
  60. }
  61. Timestamp LogFileImpl::creationDateImpl() const
  62. {
  63. return _creationDate;
  64. }
  65. const std::string& LogFileImpl::pathImpl() const
  66. {
  67. return _path;
  68. }
  69. void LogFileImpl::createFile()
  70. {
  71. _hFile = CreateFileA(_path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  72. if (_hFile == INVALID_HANDLE_VALUE) throw OpenFileException(_path);
  73. SetFilePointer(_hFile, 0, 0, FILE_END);
  74. // There seems to be a strange "optimization" in the Windows NTFS
  75. // filesystem that causes it to reuse directory entries of deleted
  76. // files. Example:
  77. // 1. create a file named "test.dat"
  78. // note the file's creation date
  79. // 2. delete the file "test.dat"
  80. // 3. wait a few seconds
  81. // 4. create a file named "test.dat"
  82. // the new file will have the same creation
  83. // date as the old one.
  84. // We work around this bug by taking the file's
  85. // modification date as a reference when the
  86. // file is empty.
  87. if (sizeImpl() == 0)
  88. _creationDate = File(_path).getLastModified();
  89. else
  90. _creationDate = File(_path).created();
  91. }
  92. } // namespace Poco