~ubuntu-branches/ubuntu/trusty/log4shib/trusty

« back to all changes in this revision

Viewing changes to src/RollingFileAppender.cpp

  • Committer: Package Import Robot
  • Author(s): Russ Allbery
  • Date: 2012-06-05 21:20:25 UTC
  • Revision ID: package-import@ubuntu.com-20120605212025-uyigtav7dqwvnf41
Tags: upstream-1.0.4
ImportĀ upstreamĀ versionĀ 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * RollingFileAppender.cpp
 
3
 *
 
4
 * See the COPYING file for the terms of usage and distribution.
 
5
 */
 
6
 
 
7
#include "PortabilityImpl.hh"
 
8
#ifdef LOG4SHIB_HAVE_IO_H
 
9
#    include <io.h>
 
10
#endif
 
11
#ifdef LOG4SHIB_HAVE_UNISTD_H
 
12
#    include <unistd.h>
 
13
#endif
 
14
 
 
15
#include <sys/types.h>
 
16
#include <sys/stat.h>
 
17
#include <fcntl.h>
 
18
#include <log4shib/RollingFileAppender.hh>
 
19
#include <log4shib/Category.hh>
 
20
#ifdef LOG4SHIB_HAVE_SSTREAM
 
21
#include <sstream>
 
22
#endif
 
23
 
 
24
namespace log4shib {
 
25
 
 
26
    RollingFileAppender::RollingFileAppender(const std::string& name,
 
27
                                             const std::string& fileName, 
 
28
                                             size_t maxFileSize, 
 
29
                                             unsigned int maxBackupIndex,
 
30
                                             bool append,
 
31
                                             mode_t mode) :
 
32
        FileAppender(name, fileName, append, mode),
 
33
        _maxBackupIndex(maxBackupIndex),
 
34
        _maxFileSize(maxFileSize) {
 
35
    }
 
36
 
 
37
    void RollingFileAppender::setMaxBackupIndex(unsigned int maxBackups) { 
 
38
        _maxBackupIndex = maxBackups; 
 
39
    }
 
40
    
 
41
    unsigned int RollingFileAppender::getMaxBackupIndex() const { 
 
42
        return _maxBackupIndex; 
 
43
    }
 
44
 
 
45
    void RollingFileAppender::setMaximumFileSize(size_t maxFileSize) {
 
46
        _maxFileSize = maxFileSize;
 
47
    }
 
48
 
 
49
    size_t RollingFileAppender::getMaxFileSize() const { 
 
50
        return _maxFileSize; 
 
51
    }
 
52
 
 
53
    void RollingFileAppender::rollOver() {
 
54
        if (_fd != -1)
 
55
            ::close(_fd);
 
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);
 
65
#else
 
66
                                // the direction parameter is broken in STLport 4.5.3, 
 
67
                                // so we don't specify it (the code works without it)
 
68
                                oldName.seekp(n);
 
69
#endif
 
70
                oldName << i-1 << std::ends;
 
71
                ::rename(oldName.str().c_str(), newName.c_str());
 
72
            }
 
73
            ::rename(_fileName.c_str(), oldName.str().c_str());
 
74
        }
 
75
        _fd = ::open(_fileName.c_str(), _flags, _mode);
 
76
    }
 
77
 
 
78
    void RollingFileAppender::_append(const LoggingEvent& event) {
 
79
        FileAppender::_append(event);
 
80
        off_t offset = -1;
 
81
        if (_fd != -1)
 
82
            offset = ::lseek(_fd, 0, SEEK_END);
 
83
        if (offset < 0) {
 
84
            // XXX we got an error, ignore for now
 
85
        } else {
 
86
            if(static_cast<size_t>(offset) >= _maxFileSize) {
 
87
                rollOver();
 
88
            }
 
89
        }
 
90
    }
 
91
}