RollingFileAppender.cpp
Go to the documentation of this file.
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 LOG4CPP_HAVE_IO_H
9 # include <io.h>
10 #endif
11 #ifdef LOG4CPP_HAVE_UNISTD_H
12 # include <unistd.h>
13 #endif
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
19 #include <log4cpp/Category.hh>
20 #include <log4cpp/FactoryParams.hh>
21 #include <memory>
22 #include <stdio.h>
23 #include <math.h>
24 
25 #ifdef LOG4CPP_HAVE_SSTREAM
26 #include <sstream>
27 #include <iomanip>
28 #endif
29 
30 namespace log4cpp {
31 
33  const std::string& fileName,
34  size_t maxFileSize,
35  unsigned int maxBackupIndex,
36  bool append,
37  mode_t mode) :
38  FileAppender(name, fileName, append, mode),
39  _maxBackupIndex(maxBackupIndex > 0 ? maxBackupIndex : 1),
40  _maxBackupIndexWidth((_maxBackupIndex > 0) ? log10((float)_maxBackupIndex)+1 : 1),
41  _maxFileSize(maxFileSize) {
42  }
43 
44  void RollingFileAppender::setMaxBackupIndex(unsigned int maxBackups) {
45  _maxBackupIndex = maxBackups;
46  _maxBackupIndexWidth = (_maxBackupIndex > 0) ? log10((float)_maxBackupIndex)+1 : 1;
47  }
48 
50  return _maxBackupIndex;
51  }
52 
53  void RollingFileAppender::setMaximumFileSize(size_t maxFileSize) {
54  _maxFileSize = maxFileSize;
55  }
56 
58  return _maxFileSize;
59  }
60 
62  ::close(_fd);
63  if (_maxBackupIndex > 0) {
64  std::ostringstream filename_stream;
65  filename_stream << _fileName << "." << std::setw( _maxBackupIndexWidth ) << std::setfill( '0' ) << _maxBackupIndex << std::ends;
66  // remove the very last (oldest) file
67  std::string last_log_filename = filename_stream.str();
68  std::cout << last_log_filename << std::endl;
69  ::remove(last_log_filename.c_str());
70 
71  // rename each existing file to the consequent one
72  for(unsigned int i = _maxBackupIndex; i > 1; i--) {
73  filename_stream.str(std::string());
74  filename_stream << _fileName << '.' << std::setw( _maxBackupIndexWidth ) << std::setfill( '0' ) << i - 1 << std::ends; // set padding so the files are listed in order
75  ::rename(filename_stream.str().c_str(), last_log_filename.c_str());
76  last_log_filename = filename_stream.str();
77  }
78  // new file will be numbered 1
79  ::rename(_fileName.c_str(), last_log_filename.c_str());
80  }
81  _fd = ::open(_fileName.c_str(), _flags, _mode);
82  }
83 
85  FileAppender::_append(event);
86  off_t offset = ::lseek(_fd, 0, SEEK_END);
87  if (offset < 0) {
88  // XXX we got an error, ignore for now
89  } else {
90  if(static_cast<size_t>(offset) >= _maxFileSize) {
91  rollOver();
92  }
93  }
94  }
95 
96  std::auto_ptr<Appender> create_roll_file_appender(const FactoryParams& params)
97  {
98  std::string name, filename;
99  bool append = true;
100  mode_t mode = 664;
101  int max_file_size = 0, max_backup_index = 0;
102  params.get_for("roll file appender").required("name", name)("filename", filename)("max_file_size", max_file_size)
103  ("max_backup_index", max_backup_index)
104  .optional("append", append)("mode", mode);
105 
106  return std::auto_ptr<Appender>(new RollingFileAppender(name, filename, max_file_size, max_backup_index, append, mode));
107  }
108 }
const std::string _fileName
Definition: FileAppender.hh:85
virtual size_t getMaxFileSize() const
required_params_validator required(const char *param, T &value) const
virtual void _append(const LoggingEvent &event)
virtual void _append(const LoggingEvent &event)
virtual void close()
RollingFileAppender(const std::string &name, const std::string &fileName, size_t maxFileSize=10 *1024 *1024, unsigned int maxBackupIndex=1, bool append=true, mode_t mode=00644)
std::auto_ptr< Appender > create_roll_file_appender(const FactoryParams &)
virtual void setMaxBackupIndex(unsigned int maxBackups)
unsigned short int _maxBackupIndexWidth
int mode_t
Definition: config-win32.h:159
virtual unsigned int getMaxBackupIndex() const
details::parameter_validator get_for(const char *tag) const
virtual void setMaximumFileSize(size_t maxFileSize)


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:14:17