~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/src/SimpleFileChannel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// SimpleFileChannel.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/src/SimpleFileChannel.cpp#1 $
 
5
//
 
6
// Library: Foundation
 
7
// Package: Logging
 
8
// Module:  SimpleFileChannel
 
9
//
 
10
// Copyright (c) 2005-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/SimpleFileChannel.h"
 
38
#include "Poco/LogFile.h"
 
39
#include "Poco/File.h"
 
40
#include "Poco/Message.h"
 
41
#include "Poco/Exception.h"
 
42
#include <ctype.h>
 
43
 
 
44
 
 
45
namespace Poco {
 
46
 
 
47
 
 
48
const std::string SimpleFileChannel::PROP_PATH          = "path";
 
49
const std::string SimpleFileChannel::PROP_SECONDARYPATH = "secondaryPath";
 
50
const std::string SimpleFileChannel::PROP_ROTATION      = "rotation";
 
51
 
 
52
 
 
53
SimpleFileChannel::SimpleFileChannel(): 
 
54
        _limit(0),
 
55
        _pFile(0)
 
56
{
 
57
}
 
58
 
 
59
 
 
60
SimpleFileChannel::SimpleFileChannel(const std::string& path):
 
61
        _path(path),
 
62
        _secondaryPath(path + ".0"),
 
63
        _limit(0),
 
64
        _pFile(0)
 
65
{
 
66
}
 
67
 
 
68
 
 
69
SimpleFileChannel::~SimpleFileChannel()
 
70
{
 
71
        close();
 
72
}
 
73
 
 
74
 
 
75
void SimpleFileChannel::open()
 
76
{
 
77
        FastMutex::ScopedLock lock(_mutex);
 
78
        
 
79
        if (!_pFile)
 
80
        {
 
81
                File primary(_path);
 
82
                File secondary(_secondaryPath);
 
83
                Timestamp pt = primary.exists() ? primary.getLastModified() : 0;
 
84
                Timestamp st = secondary.exists() ? secondary.getLastModified() : 0;
 
85
                std::string path;
 
86
                if (pt >= st)
 
87
                        path = _path;
 
88
                else
 
89
                        path = _secondaryPath;
 
90
                _pFile = new LogFile(path);
 
91
        }
 
92
}
 
93
 
 
94
 
 
95
void SimpleFileChannel::close()
 
96
{
 
97
        FastMutex::ScopedLock lock(_mutex);
 
98
 
 
99
        delete _pFile;
 
100
        _pFile = 0;
 
101
}
 
102
 
 
103
 
 
104
void SimpleFileChannel::log(const Message& msg)
 
105
{
 
106
        open();
 
107
 
 
108
        FastMutex::ScopedLock lock(_mutex);
 
109
 
 
110
        if (_limit > 0 && _pFile->size() >= _limit)
 
111
        {
 
112
                rotate();
 
113
        }
 
114
        _pFile->write(msg.getText());
 
115
}
 
116
 
 
117
        
 
118
void SimpleFileChannel::setProperty(const std::string& name, const std::string& value)
 
119
{
 
120
        FastMutex::ScopedLock lock(_mutex);
 
121
 
 
122
        if (name == PROP_PATH)
 
123
        {
 
124
                _path = value;
 
125
                if (_secondaryPath.empty())
 
126
                        _secondaryPath = _path + ".0";
 
127
        }
 
128
        else if (name == PROP_SECONDARYPATH)
 
129
                _secondaryPath = value;
 
130
        else if (name == PROP_ROTATION)
 
131
                setRotation(value);
 
132
        else
 
133
                Channel::setProperty(name, value);
 
134
}
 
135
 
 
136
 
 
137
std::string SimpleFileChannel::getProperty(const std::string& name) const
 
138
{
 
139
        if (name == PROP_PATH)
 
140
                return _path;
 
141
        else if (name == PROP_SECONDARYPATH)
 
142
                return _secondaryPath;
 
143
        else if (name == PROP_ROTATION)
 
144
                return _rotation;
 
145
        else
 
146
                return Channel::getProperty(name);
 
147
}
 
148
 
 
149
 
 
150
Timestamp SimpleFileChannel::creationDate() const
 
151
{
 
152
        if (_pFile)
 
153
                return _pFile->creationDate();
 
154
        else
 
155
                return 0;
 
156
}
 
157
 
 
158
        
 
159
UInt64 SimpleFileChannel::size() const
 
160
{
 
161
        if (_pFile)
 
162
                return _pFile->size();
 
163
        else
 
164
                return 0;
 
165
}
 
166
 
 
167
 
 
168
const std::string& SimpleFileChannel::path() const
 
169
{
 
170
        return _path;
 
171
}
 
172
 
 
173
 
 
174
const std::string& SimpleFileChannel::secondaryPath() const
 
175
{
 
176
        return _secondaryPath;
 
177
}
 
178
 
 
179
 
 
180
void SimpleFileChannel::setRotation(const std::string& rotation)
 
181
{
 
182
        std::string::const_iterator it  = rotation.begin();
 
183
        std::string::const_iterator end = rotation.end();
 
184
        UInt64 n = 0;
 
185
        while (it != end && isspace(*it)) ++it;
 
186
        while (it != end && isdigit(*it)) { n *= 10; n += *it++ - '0'; }
 
187
        while (it != end && isspace(*it)) ++it;
 
188
        std::string unit;
 
189
        while (it != end && isalpha(*it)) unit += *it++;
 
190
        
 
191
        if (unit == "K")
 
192
                _limit = n*1024;
 
193
        else if (unit == "M")
 
194
                _limit = n*1024*1024;
 
195
        else if (unit.empty())
 
196
                _limit = n;
 
197
        else if (unit == "never")
 
198
                _limit = 0;
 
199
        else
 
200
                throw InvalidArgumentException("rotation", rotation);
 
201
        _rotation = rotation;
 
202
}
 
203
 
 
204
 
 
205
void SimpleFileChannel::rotate()
 
206
{
 
207
        std::string newPath;
 
208
        if (_pFile->path() == _path)
 
209
                newPath = _secondaryPath;
 
210
        else
 
211
                newPath = _path;
 
212
        File f(newPath);
 
213
        if (f.exists())
 
214
        {
 
215
                try
 
216
                {
 
217
                        f.remove();
 
218
                }
 
219
                catch (...)
 
220
                {
 
221
                }
 
222
        }
 
223
        delete _pFile;
 
224
        _pFile = new LogFile(newPath);
 
225
}
 
226
 
 
227
 
 
228
} // namespace Poco