GenerationalFileAppender.cpp
Go to the documentation of this file.
1 /*
2  * GenerationalFileAppender.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 
24 #ifdef LOG4CPP_HAVE_SSTREAM
25 #include <sstream>
26 #endif
27 
28 namespace log4cpp {
29 
31  const std::string& fileName,
32  bool append,
33  mode_t mode) :
34  FileAppender(name, fileName+".0", append, mode),
35  _originalFileName(fileName),
36  _generation(0) {
37  }
38 
40  ++_generation;
42  str << _originalFileName << "." << _generation << std::ends;
43  int new_fd = ::open(str.str().c_str(), _flags, _mode);
44  if (-1 != new_fd)
45  {
46  // avoid race-conditions as much as possible (w/o locks)
47  int old_fd = _fd;
48  _fd = new_fd;
49  ::close(old_fd);
50  }
52  }
53 
54  std::auto_ptr<Appender> create_generation_file_appender(const FactoryParams& params)
55  {
56  std::string name, filename;
57  bool append = true;
58  mode_t mode = 664;
59  params.get_for("rool file appender").required("name", name)("filename", filename)
60  .optional("append", append)("mode", mode);
61 
62  return std::auto_ptr<Appender>(new GenerationalFileAppender(name, filename, append, mode));
63  }
64 }
std::auto_ptr< Appender > create_generation_file_appender(const FactoryParams &)
required_params_validator required(const char *param, T &value) const
unsigned int _generation
The current generation (initializes to 0)
virtual void close()
int mode_t
Definition: config-win32.h:159
details::parameter_validator get_for(const char *tag) const
std::string _originalFileName
Filename given to the constructor, before an extension is applied.
GenerationalFileAppender(const std::string &name, const std::string &fileName, bool append=true, mode_t mode=00644)
Open "filename.0" and set _generation=0.


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:10:00