aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/src/LogFile_WIN32U.cpp
blob: b89bf8ec03acf0c68047369aefb523d37d9ee200 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// LogFile_WIN32U.cpp
//
// Library: Foundation
// Package: Logging
// Module:  LogFile
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/LogFile_WIN32U.h"
#include "Poco/File.h"
#include "Poco/Exception.h"
#include "Poco/UnicodeConverter.h"


namespace Poco {


LogFileImpl::LogFileImpl(const std::string& path): _path(path), _hFile(INVALID_HANDLE_VALUE)
{
	File file(path);
	if (file.exists())
	{
		if (0 == sizeImpl())
			_creationDate = file.getLastModified();
		else
			_creationDate = file.created();
	}
}


LogFileImpl::~LogFileImpl()
{
	CloseHandle(_hFile);
}


void LogFileImpl::writeImpl(const std::string& text, bool flush)
{
	if (INVALID_HANDLE_VALUE == _hFile)	createFile();

	DWORD bytesWritten;
	BOOL res = WriteFile(_hFile, text.data(), (DWORD) text.size(), &bytesWritten, NULL);
	if (!res) throw WriteFileException(_path);
	res = WriteFile(_hFile, "\r\n", 2, &bytesWritten, NULL);
	if (!res) throw WriteFileException(_path);
	if (flush)
	{
		res = FlushFileBuffers(_hFile);
		if (!res) throw WriteFileException(_path);
	}
}


UInt64 LogFileImpl::sizeImpl() const
{
	if (INVALID_HANDLE_VALUE == _hFile)
	{
		File file(_path);
		if (file.exists()) return file.getSize();
		else return 0;
	}

	LARGE_INTEGER li;
	li.HighPart = 0;
	li.LowPart  = SetFilePointer(_hFile, 0, &li.HighPart, FILE_CURRENT);
	return li.QuadPart;
}


Timestamp LogFileImpl::creationDateImpl() const
{
	return _creationDate;
}


const std::string& LogFileImpl::pathImpl() const
{
	return _path;
}


void LogFileImpl::createFile()
{
	std::wstring upath;
	FileImpl::convertPath(_path, upath);
	
	_hFile = CreateFileW(upath.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (_hFile == INVALID_HANDLE_VALUE) throw OpenFileException(_path);
	SetFilePointer(_hFile, 0, 0, FILE_END);
	// There seems to be a strange "optimization" in the Windows NTFS
	// filesystem that causes it to reuse directory entries of deleted
	// files. Example:
	// 1. create a file named "test.dat"
	//    note the file's creation date
	// 2. delete the file "test.dat"
	// 3. wait a few seconds
	// 4. create a file named "test.dat"
	//    the new file will have the same creation
	//    date as the old one.
	// We work around this bug by taking the file's
	// modification date as a reference when the
	// file is empty.
	if (sizeImpl() == 0)
		_creationDate = File(_path).getLastModified();
	else
		_creationDate = File(_path).created();
}


} // namespace Poco