LogFile_WIN32U.cpp 2.6 KB

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