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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
//
// FileChannel.cpp
//
// Library: Foundation
// Package: Logging
// Module: FileChannel
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/FileChannel.h"
#include "Poco/ArchiveStrategy.h"
#include "Poco/RotateStrategy.h"
#include "Poco/PurgeStrategy.h"
#include "Poco/Message.h"
#include "Poco/NumberParser.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTime.h"
#include "Poco/LocalDateTime.h"
#include "Poco/String.h"
#include "Poco/Exception.h"
#include "Poco/Ascii.h"
namespace Poco {
const std::string FileChannel::PROP_PATH = "path";
const std::string FileChannel::PROP_ROTATION = "rotation";
const std::string FileChannel::PROP_ARCHIVE = "archive";
const std::string FileChannel::PROP_TIMES = "times";
const std::string FileChannel::PROP_COMPRESS = "compress";
const std::string FileChannel::PROP_PURGEAGE = "purgeAge";
const std::string FileChannel::PROP_PURGECOUNT = "purgeCount";
const std::string FileChannel::PROP_FLUSH = "flush";
const std::string FileChannel::PROP_ROTATEONOPEN = "rotateOnOpen";
FileChannel::FileChannel():
_times("utc"),
_compress(false),
_flush(true),
_rotateOnOpen(false),
_pFile(0),
_pRotateStrategy(0),
_pArchiveStrategy(new ArchiveByNumberStrategy),
_pPurgeStrategy(0)
{
}
FileChannel::FileChannel(const std::string& rPath):
_path(rPath),
_times("utc"),
_compress(false),
_flush(true),
_rotateOnOpen(false),
_pFile(0),
_pRotateStrategy(0),
_pArchiveStrategy(new ArchiveByNumberStrategy),
_pPurgeStrategy(0)
{
}
FileChannel::~FileChannel()
{
try
{
close();
delete _pRotateStrategy;
delete _pArchiveStrategy;
delete _pPurgeStrategy;
}
catch (...)
{
poco_unexpected();
}
}
void FileChannel::open()
{
FastMutex::ScopedLock lock(_mutex);
unsafeOpen();
}
void FileChannel::close()
{
FastMutex::ScopedLock lock(_mutex);
delete _pFile;
_pFile = 0;
}
void FileChannel::log(const Message& msg)
{
FastMutex::ScopedLock lock(_mutex);
unsafeOpen();
if (_pRotateStrategy && _pArchiveStrategy && _pRotateStrategy->mustRotate(_pFile))
{
try
{
_pFile = _pArchiveStrategy->archive(_pFile);
purge();
}
catch (...)
{
_pFile = new LogFile(_path);
}
// we must call mustRotate() again to give the
// RotateByIntervalStrategy a chance to write its timestamp
// to the new file.
_pRotateStrategy->mustRotate(_pFile);
}
try
{
_pFile->write(msg.getText(), _flush);
}
catch (const WriteFileException &)
{
// In case of no space left on device,
// we try to purge old files or truncate current file.
// NOTE: error reason is not preserved in WriteFileException, we need to check errno manually.
// NOTE: other reasons like quota exceeded are not handled.
// NOTE: current log message will be lost.
if (errno == ENOSPC)
{
PurgeOneFileStrategy().purge(_path);
}
delete _pFile;
_pFile = 0;
_pFile = new LogFile(_path);
}
}
void FileChannel::setProperty(const std::string& name, const std::string& value)
{
FastMutex::ScopedLock lock(_mutex);
if (name == PROP_TIMES)
{
_times = value;
if (!_rotation.empty())
setRotation(_rotation);
if (!_archive.empty())
setArchive(_archive);
}
else if (name == PROP_PATH)
_path = value;
else if (name == PROP_ROTATION)
setRotation(value);
else if (name == PROP_ARCHIVE)
setArchive(value);
else if (name == PROP_COMPRESS)
setCompress(value);
else if (name == PROP_PURGEAGE)
setPurgeAge(value);
else if (name == PROP_PURGECOUNT)
setPurgeCount(value);
else if (name == PROP_FLUSH)
setFlush(value);
else if (name == PROP_ROTATEONOPEN)
setRotateOnOpen(value);
else
Channel::setProperty(name, value);
}
std::string FileChannel::getProperty(const std::string& name) const
{
if (name == PROP_TIMES)
return _times;
else if (name == PROP_PATH)
return _path;
else if (name == PROP_ROTATION)
return _rotation;
else if (name == PROP_ARCHIVE)
return _archive;
else if (name == PROP_COMPRESS)
return std::string(_compress ? "true" : "false");
else if (name == PROP_PURGEAGE)
return _purgeAge;
else if (name == PROP_PURGECOUNT)
return _purgeCount;
else if (name == PROP_FLUSH)
return std::string(_flush ? "true" : "false");
else if (name == PROP_ROTATEONOPEN)
return std::string(_rotateOnOpen ? "true" : "false");
else
return Channel::getProperty(name);
}
Timestamp FileChannel::creationDate() const
{
if (_pFile)
return _pFile->creationDate();
else
return 0;
}
UInt64 FileChannel::size() const
{
if (_pFile)
return _pFile->size();
else
return 0;
}
const std::string& FileChannel::path() const
{
return _path;
}
void FileChannel::setRotation(const std::string& rotation)
{
std::string::const_iterator it = rotation.begin();
std::string::const_iterator end = rotation.end();
int 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++;
RotateStrategy* pStrategy = 0;
if ((rotation.find(',') != std::string::npos) || (rotation.find(':') != std::string::npos))
{
if (_times == "utc")
pStrategy = new RotateAtTimeStrategy<DateTime>(rotation);
else if (_times == "local")
pStrategy = new RotateAtTimeStrategy<LocalDateTime>(rotation);
else
throw PropertyNotSupportedException("times", _times);
}
else if (unit == "daily")
pStrategy = new RotateByIntervalStrategy(Timespan(1*Timespan::DAYS));
else if (unit == "weekly")
pStrategy = new RotateByIntervalStrategy(Timespan(7*Timespan::DAYS));
else if (unit == "monthly")
pStrategy = new RotateByIntervalStrategy(Timespan(30*Timespan::DAYS));
else if (unit == "seconds") // for testing only
pStrategy = new RotateByIntervalStrategy(Timespan(n*Timespan::SECONDS));
else if (unit == "minutes")
pStrategy = new RotateByIntervalStrategy(Timespan(n*Timespan::MINUTES));
else if (unit == "hours")
pStrategy = new RotateByIntervalStrategy(Timespan(n*Timespan::HOURS));
else if (unit == "days")
pStrategy = new RotateByIntervalStrategy(Timespan(n*Timespan::DAYS));
else if (unit == "weeks")
pStrategy = new RotateByIntervalStrategy(Timespan(n*7*Timespan::DAYS));
else if (unit == "months")
pStrategy = new RotateByIntervalStrategy(Timespan(n*30*Timespan::DAYS));
else if (unit == "K")
pStrategy = new RotateBySizeStrategy(n*1024);
else if (unit == "M")
pStrategy = new RotateBySizeStrategy(n*1024*1024);
else if (unit.empty())
pStrategy = new RotateBySizeStrategy(n);
else if (unit != "never")
throw InvalidArgumentException("rotation", rotation);
delete _pRotateStrategy;
_pRotateStrategy = pStrategy;
_rotation = rotation;
}
void FileChannel::setArchive(const std::string& archive)
{
ArchiveStrategy* pStrategy = 0;
if (archive == "number")
{
pStrategy = new ArchiveByNumberStrategy;
}
else if (archive == "timestamp")
{
if (_times == "utc")
pStrategy = new ArchiveByTimestampStrategy<DateTime>;
else if (_times == "local")
pStrategy = new ArchiveByTimestampStrategy<LocalDateTime>;
else
throw PropertyNotSupportedException("times", _times);
}
else throw InvalidArgumentException("archive", archive);
delete _pArchiveStrategy;
pStrategy->compress(_compress);
_pArchiveStrategy = pStrategy;
_archive = archive;
}
void FileChannel::setCompress(const std::string& compress)
{
_compress = icompare(compress, "true") == 0;
if (_pArchiveStrategy)
_pArchiveStrategy->compress(_compress);
}
void FileChannel::setPurgeAge(const std::string& age)
{
if (setNoPurge(age)) return;
std::string::const_iterator nextToDigit;
int num = extractDigit(age, &nextToDigit);
Timespan::TimeDiff factor = extractFactor(age, nextToDigit);
setPurgeStrategy(new PurgeByAgeStrategy(Timespan(num * factor)));
_purgeAge = age;
}
void FileChannel::setPurgeCount(const std::string& count)
{
if (setNoPurge(count)) return;
setPurgeStrategy(new PurgeByCountStrategy(extractDigit(count)));
_purgeCount = count;
}
void FileChannel::setFlush(const std::string& flush)
{
_flush = icompare(flush, "true") == 0;
}
void FileChannel::setRotateOnOpen(const std::string& rotateOnOpen)
{
_rotateOnOpen = icompare(rotateOnOpen, "true") == 0;
}
void FileChannel::purge()
{
if (_pPurgeStrategy)
{
try
{
_pPurgeStrategy->purge(_path);
}
catch (...)
{
}
}
}
void FileChannel::unsafeOpen()
{
if (!_pFile)
{
_pFile = new LogFile(_path);
if (_rotateOnOpen && _pFile->size() > 0)
{
try
{
_pFile = _pArchiveStrategy->archive(_pFile);
purge();
}
catch (...)
{
_pFile = new LogFile(_path);
}
}
}
}
bool FileChannel::setNoPurge(const std::string& value)
{
if (value.empty() || 0 == icompare(value, "none"))
{
delete _pPurgeStrategy;
_pPurgeStrategy = 0;
_purgeAge = "none";
return true;
}
else return false;
}
int FileChannel::extractDigit(const std::string& value, std::string::const_iterator* nextToDigit) const
{
std::string::const_iterator it = value.begin();
std::string::const_iterator end = value.end();
int digit = 0;
while (it != end && Ascii::isSpace(*it)) ++it;
while (it != end && Ascii::isDigit(*it))
{
digit *= 10;
digit += *it++ - '0';
}
if (digit == 0)
throw InvalidArgumentException("Zero is not valid purge age.");
if (nextToDigit) *nextToDigit = it;
return digit;
}
void FileChannel::setPurgeStrategy(PurgeStrategy* strategy)
{
delete _pPurgeStrategy;
_pPurgeStrategy = strategy;
}
Timespan::TimeDiff FileChannel::extractFactor(const std::string& value, std::string::const_iterator start) const
{
while (start != value.end() && Ascii::isSpace(*start)) ++start;
std::string unit;
while (start != value.end() && Ascii::isAlpha(*start)) unit += *start++;
if (unit == "seconds")
return Timespan::SECONDS;
if (unit == "minutes")
return Timespan::MINUTES;
else if (unit == "hours")
return Timespan::HOURS;
else if (unit == "days")
return Timespan::DAYS;
else if (unit == "weeks")
return 7 * Timespan::DAYS;
else if (unit == "months")
return 30 * Timespan::DAYS;
else throw InvalidArgumentException("purgeAge", value);
return Timespan::TimeDiff();
}
} // namespace Poco
|