~ubuntu-branches/ubuntu/quantal/poco/quantal

« back to all changes in this revision

Viewing changes to Foundation/src/LogFile_WIN32.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2008-11-15 11:39:15 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20081115113915-7kauhm2c3m2i7oid
Tags: 1.3.3p1-2
* Fixed FTBFS with GCC 4.4 due to missing #include (Closes: #505619)
* Renamed 20_gcc43-missing-include.dpatch to 20_gcc44-missing-include.dpatch
* Downgraded dependencies on -dbg packages (Closes: #504342)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//
2
2
// LogFile_WIN32.cpp
3
3
//
4
 
// $Id: //poco/1.3/Foundation/src/LogFile_WIN32.cpp#2 $
 
4
// $Id: //poco/1.3/Foundation/src/LogFile_WIN32.cpp#3 $
5
5
//
6
6
// Library: Foundation
7
7
// Package: Logging
42
42
namespace Poco {
43
43
 
44
44
 
45
 
LogFileImpl::LogFileImpl(const std::string& path): _path(path)
46
 
{
47
 
        _hFile = CreateFileA(path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
48
 
        if (_hFile == INVALID_HANDLE_VALUE) throw OpenFileException(path);
 
45
LogFileImpl::LogFileImpl(const std::string& path): _path(path), _hFile(INVALID_HANDLE_VALUE)
 
46
{
 
47
        File file(path);
 
48
        if (file.exists())
 
49
        {
 
50
                if (0 == sizeImpl())
 
51
                        _creationDate = file.getLastModified();
 
52
                else
 
53
                        _creationDate = file.created();
 
54
        }
 
55
}
 
56
 
 
57
 
 
58
LogFileImpl::~LogFileImpl()
 
59
{
 
60
        CloseHandle(_hFile);
 
61
}
 
62
 
 
63
 
 
64
void LogFileImpl::writeImpl(const std::string& text)
 
65
{
 
66
        if (INVALID_HANDLE_VALUE == _hFile)     createFile();
 
67
 
 
68
        DWORD bytesWritten;
 
69
        BOOL res = WriteFile(_hFile, text.data(), (DWORD) text.size(), &bytesWritten, NULL);
 
70
        if (!res) throw WriteFileException(_path);
 
71
        res = WriteFile(_hFile, "\r\n", 2, &bytesWritten, NULL);
 
72
        if (!res) throw WriteFileException(_path);
 
73
        res = FlushFileBuffers(_hFile);
 
74
        if (!res) throw WriteFileException(_path);
 
75
}
 
76
 
 
77
 
 
78
UInt64 LogFileImpl::sizeImpl() const
 
79
{
 
80
        if (INVALID_HANDLE_VALUE == _hFile)
 
81
        {
 
82
                File file(_path);
 
83
                if (file.exists()) return file.getSize();
 
84
                else return 0;
 
85
        }
 
86
 
 
87
        LARGE_INTEGER li;
 
88
        li.HighPart = 0;
 
89
        li.LowPart  = SetFilePointer(_hFile, 0, &li.HighPart, FILE_CURRENT);
 
90
        return li.QuadPart;
 
91
}
 
92
 
 
93
 
 
94
Timestamp LogFileImpl::creationDateImpl() const
 
95
{
 
96
        return _creationDate;
 
97
}
 
98
 
 
99
 
 
100
const std::string& LogFileImpl::pathImpl() const
 
101
{
 
102
        return _path;
 
103
}
 
104
 
 
105
 
 
106
void LogFileImpl::createFile()
 
107
{
 
108
        _hFile = CreateFileA(_path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 
109
        if (_hFile == INVALID_HANDLE_VALUE) throw OpenFileException(_path);
49
110
        SetFilePointer(_hFile, 0, 0, FILE_END);
50
111
        // There seems to be a strange "optimization" in the Windows NTFS
51
112
        // filesystem that causes it to reuse directory entries of deleted
61
122
        // modification date as a reference when the
62
123
        // file is empty.
63
124
        if (sizeImpl() == 0)
64
 
                _creationDate = File(path).getLastModified();
 
125
                _creationDate = File(_path).getLastModified();
65
126
        else
66
 
                _creationDate = File(path).created();
67
 
}
68
 
 
69
 
 
70
 
LogFileImpl::~LogFileImpl()
71
 
{
72
 
        CloseHandle(_hFile);
73
 
}
74
 
 
75
 
 
76
 
void LogFileImpl::writeImpl(const std::string& text)
77
 
{
78
 
        DWORD bytesWritten;
79
 
        BOOL res = WriteFile(_hFile, text.data(), (DWORD) text.size(), &bytesWritten, NULL);
80
 
        if (!res) throw WriteFileException(_path);
81
 
        res = WriteFile(_hFile, "\r\n", 2, &bytesWritten, NULL);
82
 
        if (!res) throw WriteFileException(_path);
83
 
        res = FlushFileBuffers(_hFile);
84
 
        if (!res) throw WriteFileException(_path);
85
 
}
86
 
 
87
 
 
88
 
UInt64 LogFileImpl::sizeImpl() const
89
 
{
90
 
        LARGE_INTEGER li;
91
 
        li.HighPart = 0;
92
 
        li.LowPart  = SetFilePointer(_hFile, 0, &li.HighPart, FILE_CURRENT);
93
 
        return li.QuadPart;
94
 
}
95
 
 
96
 
 
97
 
Timestamp LogFileImpl::creationDateImpl() const
98
 
{
99
 
        return _creationDate;
100
 
}
101
 
 
102
 
 
103
 
const std::string& LogFileImpl::pathImpl() const
104
 
{
105
 
        return _path;
106
 
}
107
 
 
 
127
                _creationDate = File(_path).created();
 
128
}
108
129
 
109
130
} // namespace Poco