Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "PortabilityImpl.hh"
00008 #ifdef LOG4CPP_HAVE_IO_H
00009 # include <io.h>
00010 #endif
00011 #ifdef LOG4CPP_HAVE_UNISTD_H
00012 # include <unistd.h>
00013 #endif
00014
00015 #include <sys/types.h>
00016 #include <sys/stat.h>
00017 #include <fcntl.h>
00018 #include <log4cpp/RollingFileAppender.hh>
00019 #include <log4cpp/Category.hh>
00020 #include <log4cpp/FactoryParams.hh>
00021 #include <memory>
00022 #include <stdio.h>
00023
00024 #ifdef LOG4CPP_HAVE_SSTREAM
00025 #include <sstream>
00026 #endif
00027
00028 namespace log4cpp {
00029
00030 RollingFileAppender::RollingFileAppender(const std::string& name,
00031 const std::string& fileName,
00032 size_t maxFileSize,
00033 unsigned int maxBackupIndex,
00034 bool append,
00035 mode_t mode) :
00036 FileAppender(name, fileName, append, mode),
00037 _maxBackupIndex(maxBackupIndex),
00038 _maxFileSize(maxFileSize) {
00039 }
00040
00041 void RollingFileAppender::setMaxBackupIndex(unsigned int maxBackups) {
00042 _maxBackupIndex = maxBackups;
00043 }
00044
00045 unsigned int RollingFileAppender::getMaxBackupIndex() const {
00046 return _maxBackupIndex;
00047 }
00048
00049 void RollingFileAppender::setMaximumFileSize(size_t maxFileSize) {
00050 _maxFileSize = maxFileSize;
00051 }
00052
00053 size_t RollingFileAppender::getMaxFileSize() const {
00054 return _maxFileSize;
00055 }
00056
00057 void RollingFileAppender::rollOver() {
00058 ::close(_fd);
00059 if (_maxBackupIndex > 0) {
00060 std::ostringstream oldName;
00061 oldName << _fileName << "." << _maxBackupIndex << std::ends;
00062 ::remove(oldName.str().c_str());
00063 size_t n = _fileName.length() + 1;
00064 for(unsigned int i = _maxBackupIndex; i > 1; i--) {
00065 std::string newName = oldName.str();
00066 #ifndef LOG4CPP_STLPORT_AND_BOOST_BUILD
00067 oldName.seekp(static_cast<std::ios::off_type>(n), std::ios::beg);
00068 #else
00069
00070
00071 oldName.seekp(n);
00072 #endif
00073 oldName << i-1 << std::ends;
00074 ::rename(oldName.str().c_str(), newName.c_str());
00075 }
00076 ::rename(_fileName.c_str(), oldName.str().c_str());
00077 }
00078 _fd = ::open(_fileName.c_str(), _flags, _mode);
00079 }
00080
00081 void RollingFileAppender::_append(const LoggingEvent& event) {
00082 FileAppender::_append(event);
00083 off_t offset = ::lseek(_fd, 0, SEEK_END);
00084 if (offset < 0) {
00085
00086 } else {
00087 if(static_cast<size_t>(offset) >= _maxFileSize) {
00088 rollOver();
00089 }
00090 }
00091 }
00092
00093 std::auto_ptr<Appender> create_roll_file_appender(const FactoryParams& params)
00094 {
00095 std::string name, filename;
00096 bool append = true;
00097 mode_t mode = 664;
00098 int max_file_size = 0, max_backup_index = 0;
00099 params.get_for("rool file appender").required("name", name)("filename", filename)("max_file_size", max_file_size)
00100 ("max_backup_index", max_backup_index)
00101 .optional("append", append)("mode", mode);
00102
00103 return std::auto_ptr<Appender>(new RollingFileAppender(name, filename, max_file_size, max_backup_index, append, mode));
00104 }
00105 }
log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung.
autogenerated on Thu Jan 2 2014 11:35:02