2
* RollingFileAppender.cpp
4
* See the COPYING file for the terms of usage and distribution.
7
#include "PortabilityImpl.hh"
8
#ifdef LOG4SHIB_HAVE_IO_H
11
#ifdef LOG4SHIB_HAVE_UNISTD_H
15
#include <sys/types.h>
18
#include <log4shib/RollingFileAppender.hh>
19
#include <log4shib/Category.hh>
20
#ifdef LOG4SHIB_HAVE_SSTREAM
26
RollingFileAppender::RollingFileAppender(const std::string& name,
27
const std::string& fileName,
29
unsigned int maxBackupIndex,
32
FileAppender(name, fileName, append, mode),
33
_maxBackupIndex(maxBackupIndex),
34
_maxFileSize(maxFileSize) {
37
void RollingFileAppender::setMaxBackupIndex(unsigned int maxBackups) {
38
_maxBackupIndex = maxBackups;
41
unsigned int RollingFileAppender::getMaxBackupIndex() const {
42
return _maxBackupIndex;
45
void RollingFileAppender::setMaximumFileSize(size_t maxFileSize) {
46
_maxFileSize = maxFileSize;
49
size_t RollingFileAppender::getMaxFileSize() const {
53
void RollingFileAppender::rollOver() {
56
if (_maxBackupIndex > 0) {
57
std::ostringstream oldName;
58
oldName << _fileName << "." << _maxBackupIndex << std::ends;
59
::remove(oldName.str().c_str());
60
size_t n = _fileName.length() + 1;
61
for(unsigned int i = _maxBackupIndex; i > 1; i--) {
62
std::string newName = oldName.str();
63
#ifndef LOG4SHIB_STLPORT_AND_BOOST_BUILD
64
oldName.seekp(n, std::ios::beg);
66
// the direction parameter is broken in STLport 4.5.3,
67
// so we don't specify it (the code works without it)
70
oldName << i-1 << std::ends;
71
::rename(oldName.str().c_str(), newName.c_str());
73
::rename(_fileName.c_str(), oldName.str().c_str());
75
_fd = ::open(_fileName.c_str(), _flags, _mode);
78
void RollingFileAppender::_append(const LoggingEvent& event) {
79
FileAppender::_append(event);
82
offset = ::lseek(_fd, 0, SEEK_END);
84
// XXX we got an error, ignore for now
86
if(static_cast<size_t>(offset) >= _maxFileSize) {