2
// SimpleFileChannel.cpp
4
// $Id: //poco/1.2/Foundation/src/SimpleFileChannel.cpp#1 $
8
// Module: SimpleFileChannel
10
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
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:
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.
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.
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"
48
const std::string SimpleFileChannel::PROP_PATH = "path";
49
const std::string SimpleFileChannel::PROP_SECONDARYPATH = "secondaryPath";
50
const std::string SimpleFileChannel::PROP_ROTATION = "rotation";
53
SimpleFileChannel::SimpleFileChannel():
60
SimpleFileChannel::SimpleFileChannel(const std::string& path):
62
_secondaryPath(path + ".0"),
69
SimpleFileChannel::~SimpleFileChannel()
75
void SimpleFileChannel::open()
77
FastMutex::ScopedLock lock(_mutex);
82
File secondary(_secondaryPath);
83
Timestamp pt = primary.exists() ? primary.getLastModified() : 0;
84
Timestamp st = secondary.exists() ? secondary.getLastModified() : 0;
89
path = _secondaryPath;
90
_pFile = new LogFile(path);
95
void SimpleFileChannel::close()
97
FastMutex::ScopedLock lock(_mutex);
104
void SimpleFileChannel::log(const Message& msg)
108
FastMutex::ScopedLock lock(_mutex);
110
if (_limit > 0 && _pFile->size() >= _limit)
114
_pFile->write(msg.getText());
118
void SimpleFileChannel::setProperty(const std::string& name, const std::string& value)
120
FastMutex::ScopedLock lock(_mutex);
122
if (name == PROP_PATH)
125
if (_secondaryPath.empty())
126
_secondaryPath = _path + ".0";
128
else if (name == PROP_SECONDARYPATH)
129
_secondaryPath = value;
130
else if (name == PROP_ROTATION)
133
Channel::setProperty(name, value);
137
std::string SimpleFileChannel::getProperty(const std::string& name) const
139
if (name == PROP_PATH)
141
else if (name == PROP_SECONDARYPATH)
142
return _secondaryPath;
143
else if (name == PROP_ROTATION)
146
return Channel::getProperty(name);
150
Timestamp SimpleFileChannel::creationDate() const
153
return _pFile->creationDate();
159
UInt64 SimpleFileChannel::size() const
162
return _pFile->size();
168
const std::string& SimpleFileChannel::path() const
174
const std::string& SimpleFileChannel::secondaryPath() const
176
return _secondaryPath;
180
void SimpleFileChannel::setRotation(const std::string& rotation)
182
std::string::const_iterator it = rotation.begin();
183
std::string::const_iterator end = rotation.end();
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;
189
while (it != end && isalpha(*it)) unit += *it++;
193
else if (unit == "M")
194
_limit = n*1024*1024;
195
else if (unit.empty())
197
else if (unit == "never")
200
throw InvalidArgumentException("rotation", rotation);
201
_rotation = rotation;
205
void SimpleFileChannel::rotate()
208
if (_pFile->path() == _path)
209
newPath = _secondaryPath;
224
_pFile = new LogFile(newPath);