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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
//
// SimpleFileChannel.cpp
//
// Library: Foundation
// Package: Logging
// Module: SimpleFileChannel
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/SimpleFileChannel.h"
#include "Poco/LogFile.h"
#include "Poco/File.h"
#include "Poco/Message.h"
#include "Poco/Exception.h"
#include "Poco/Ascii.h"
#include "Poco/String.h"
namespace Poco {
const std::string SimpleFileChannel::PROP_PATH = "path";
const std::string SimpleFileChannel::PROP_SECONDARYPATH = "secondaryPath";
const std::string SimpleFileChannel::PROP_ROTATION = "rotation";
const std::string SimpleFileChannel::PROP_FLUSH = "flush";
SimpleFileChannel::SimpleFileChannel():
_limit(0),
_flush(true),
_pFile(0)
{
}
SimpleFileChannel::SimpleFileChannel(const std::string& rPath):
_path(rPath),
_secondaryPath(rPath + ".0"),
_limit(0),
_flush(true),
_pFile(0)
{
}
SimpleFileChannel::~SimpleFileChannel()
{
try
{
close();
}
catch (...)
{
poco_unexpected();
}
}
void SimpleFileChannel::open()
{
FastMutex::ScopedLock lock(_mutex);
if (!_pFile)
{
File primary(_path);
File secondary(_secondaryPath);
Timestamp pt = primary.exists() ? primary.getLastModified() : 0;
Timestamp st = secondary.exists() ? secondary.getLastModified() : 0;
std::string pathString;
if (pt >= st)
pathString = _path;
else
pathString = _secondaryPath;
_pFile = new LogFile(pathString);
}
}
void SimpleFileChannel::close()
{
FastMutex::ScopedLock lock(_mutex);
delete _pFile;
_pFile = 0;
}
void SimpleFileChannel::log(const Message& msg)
{
open();
FastMutex::ScopedLock lock(_mutex);
if (_limit > 0 && _pFile->size() >= _limit)
{
rotate();
}
_pFile->write(msg.getText(), _flush);
}
void SimpleFileChannel::setProperty(const std::string& name, const std::string& value)
{
FastMutex::ScopedLock lock(_mutex);
if (name == PROP_PATH)
{
_path = value;
if (_secondaryPath.empty())
_secondaryPath = _path + ".0";
}
else if (name == PROP_SECONDARYPATH)
_secondaryPath = value;
else if (name == PROP_ROTATION)
setRotation(value);
else if (name == PROP_FLUSH)
setFlush(value);
else
Channel::setProperty(name, value);
}
std::string SimpleFileChannel::getProperty(const std::string& name) const
{
if (name == PROP_PATH)
return _path;
else if (name == PROP_SECONDARYPATH)
return _secondaryPath;
else if (name == PROP_ROTATION)
return _rotation;
else if (name == PROP_FLUSH)
return std::string(_flush ? "true" : "false");
else
return Channel::getProperty(name);
}
Timestamp SimpleFileChannel::creationDate() const
{
if (_pFile)
return _pFile->creationDate();
else
return 0;
}
UInt64 SimpleFileChannel::size() const
{
if (_pFile)
return _pFile->size();
else
return 0;
}
const std::string& SimpleFileChannel::path() const
{
return _path;
}
const std::string& SimpleFileChannel::secondaryPath() const
{
return _secondaryPath;
}
void SimpleFileChannel::setRotation(const std::string& rotation)
{
std::string::const_iterator it = rotation.begin();
std::string::const_iterator end = rotation.end();
UInt64 n = 0;
while (it != end && Ascii::isSpace(*it)) ++it;
while (it != end && Ascii::isDigit(*it)) { n *= 10; n += *it++ - '0'; }
while (it != end && Ascii::isSpace(*it)) ++it;
std::string unit;
while (it != end && Ascii::isAlpha(*it)) unit += *it++;
if (unit == "K")
_limit = n*1024;
else if (unit == "M")
_limit = n*1024*1024;
else if (unit.empty())
_limit = n;
else if (unit == "never")
_limit = 0;
else
throw InvalidArgumentException("rotation", rotation);
_rotation = rotation;
}
void SimpleFileChannel::setFlush(const std::string& flush)
{
_flush = icompare(flush, "true") == 0;
}
void SimpleFileChannel::rotate()
{
std::string newPath;
if (_pFile->path() == _path)
newPath = _secondaryPath;
else
newPath = _path;
File f(newPath);
if (f.exists())
{
try
{
f.remove();
}
catch (...)
{
}
}
delete _pFile;
_pFile = new LogFile(newPath);
}
} // namespace Poco
|