source: XMLIO_V2/external/src/POCO/Foundation/LogFile_WIN32U.hpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

File size: 3.8 KB
Line 
1//
2// LogFile_WIN32U.cpp
3//
4// $Id: //poco/1.3/Foundation/src/LogFile_WIN32U.cpp#2 $
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// Permission is hereby granted, free of charge, to any person or organization
14// obtaining a copy of the software and accompanying documentation covered by
15// this license (the "Software") to use, reproduce, display, distribute,
16// execute, and transmit the Software, and to prepare derivative works of the
17// Software, and to permit third-parties to whom the Software is furnished to
18// do so, all subject to the following:
19//
20// The copyright notices in the Software and this entire statement, including
21// the above license grant, this restriction and the following disclaimer,
22// must be included in all copies of the Software, in whole or in part, and
23// all derivative works of the Software, unless such copies or derivative
24// works are solely in the form of machine-executable object code generated by
25// a source language processor.
26//
27// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33// DEALINGS IN THE SOFTWARE.
34//
35
36
37#include <Poco/LogFile_WIN32U.h>
38#include <Poco/File.h>
39#include <Poco/Exception.h>
40#include <Poco/UnicodeConverter.h>
41
42
43namespace Poco {
44
45
46LogFileImpl::LogFileImpl(const std::string& path): _path(path), _hFile(INVALID_HANDLE_VALUE)
47{
48        File file(path);
49        if (file.exists())
50        {
51                if (0 == sizeImpl())
52                        _creationDate = file.getLastModified();
53                else
54                        _creationDate = file.created();
55        }
56}
57
58
59LogFileImpl::~LogFileImpl()
60{
61        CloseHandle(_hFile);
62}
63
64
65void LogFileImpl::writeImpl(const std::string& text)
66{
67        if (INVALID_HANDLE_VALUE == _hFile)     createFile();
68
69        DWORD bytesWritten;
70        BOOL res = WriteFile(_hFile, text.data(), (DWORD) text.size(), &bytesWritten, NULL);
71        if (!res) throw WriteFileException(_path);
72        res = WriteFile(_hFile, "\r\n", 2, &bytesWritten, NULL);
73        if (!res) throw WriteFileException(_path);
74        res = FlushFileBuffers(_hFile);
75        if (!res) throw WriteFileException(_path);
76}
77
78
79UInt64 LogFileImpl::sizeImpl() const
80{
81        if (INVALID_HANDLE_VALUE == _hFile)
82        {
83                File file(_path);
84                if (file.exists()) return file.getSize();
85                else return 0;
86        }
87
88        LARGE_INTEGER li;
89        li.HighPart = 0;
90        li.LowPart  = SetFilePointer(_hFile, 0, &li.HighPart, FILE_CURRENT);
91        return li.QuadPart;
92}
93
94
95Timestamp LogFileImpl::creationDateImpl() const
96{
97        return _creationDate;
98}
99
100
101const std::string& LogFileImpl::pathImpl() const
102{
103        return _path;
104}
105
106
107void LogFileImpl::createFile()
108{
109        std::wstring upath;
110        UnicodeConverter::toUTF16(_path, upath);
111       
112        _hFile = CreateFileW(upath.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
113        if (_hFile == INVALID_HANDLE_VALUE) throw OpenFileException(_path);
114        SetFilePointer(_hFile, 0, 0, FILE_END);
115        // There seems to be a strange "optimization" in the Windows NTFS
116        // filesystem that causes it to reuse directory entries of deleted
117        // files. Example:
118        // 1. create a file named "test.dat"
119        //    note the file's creation date
120        // 2. delete the file "test.dat"
121        // 3. wait a few seconds
122        // 4. create a file named "test.dat"
123        //    the new file will have the same creation
124        //    date as the old one.
125        // We work around this bug by taking the file's
126        // modification date as a reference when the
127        // file is empty.
128        if (sizeImpl() == 0)
129                _creationDate = File(_path).getLastModified();
130        else
131                _creationDate = File(_path).created();
132}
133
134
135} // namespace Poco
Note: See TracBrowser for help on using the repository browser.