FileAppender.cpp
Go to the documentation of this file.
1 /*
2  * FileAppender.cpp
3  *
4  * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
5  * Copyright 2000, Bastiaan Bakker. All rights reserved.
6  *
7  * See the COPYING file for the terms of usage and distribution.
8  */
9 
10 #include "PortabilityImpl.hh"
11 #ifdef LOG4CPP_HAVE_IO_H
12 # include <io.h>
13 #endif
14 #ifdef LOG4CPP_HAVE_UNISTD_H
15 # include <unistd.h>
16 #endif
17 
18 #include <memory>
19 #include <stdio.h>
20 #include <time.h>
21 #include <log4cpp/FileAppender.hh>
22 #include <log4cpp/Category.hh>
23 #include <log4cpp/FactoryParams.hh>
24 
25 namespace log4cpp {
26 
27  FileAppender::FileAppender(const std::string& name,
28  const std::string& fileName,
29  bool append,
30  mode_t mode) :
31  LayoutAppender(name),
32  _fileName(fileName),
33  _flags(O_CREAT | O_APPEND | O_WRONLY),
34  _mode(mode) {
35  if (!append)
36  _flags |= O_TRUNC;
37  _fd = ::open(_fileName.c_str(), _flags, _mode);
38  }
39 
40  FileAppender::FileAppender(const std::string& name, int fd) :
41  LayoutAppender(name),
42  _fileName(""),
43  _fd(fd),
44  _flags(O_CREAT | O_APPEND | O_WRONLY),
45  _mode(00644) {
46  }
47 
49  close();
50  }
51 
53  if (_fd!=-1) {
54  ::close(_fd);
55  _fd=-1;
56  }
57  }
58 
59  void FileAppender::setAppend(bool append) {
60  if (append) {
61  _flags &= ~O_TRUNC;
62  } else {
63  _flags |= O_TRUNC;
64  }
65  }
66 
67  bool FileAppender::getAppend() const {
68  return (_flags & O_TRUNC) == 0;
69  }
70 
72  _mode = mode;
73  }
74 
76  return _mode;
77  }
78 
79  void FileAppender::_append(const LoggingEvent& event) {
80  std::string message(_getLayout().format(event));
81  if (!::write(_fd, message.data(), message.length())) {
82  // XXX help! help!
83  }
84  }
85 
87  if (_fileName != "") {
88  int fd = ::open(_fileName.c_str(), _flags, _mode);
89  if (fd < 0)
90  return false;
91  else {
92  if (_fd != -1)
93  ::close(_fd);
94  _fd = fd;
95  return true;
96  }
97  } else {
98  return true;
99  }
100  }
101 
102  std::auto_ptr<Appender> create_file_appender(const FactoryParams& params)
103  {
104  std::string name, filename;
105  bool append = true;
106  mode_t mode = 664;
107 
108  params.get_for("file appender").required("name", name)("filename", filename)
109  .optional("append", append)("mode", mode);
110 
111  return std::auto_ptr<Appender>(new FileAppender(name, filename, append, mode));
112  }
113 }
const std::string _fileName
Definition: FileAppender.hh:85
virtual void setAppend(bool append)
required_params_validator required(const char *param, T &value) const
virtual void _append(const LoggingEvent &event)
virtual bool getAppend() const
virtual void close()
FileAppender(const std::string &name, const std::string &fileName, bool append=true, mode_t mode=00644)
virtual bool reopen()
virtual void setMode(mode_t mode)
int mode_t
Definition: config-win32.h:159
virtual mode_t getMode() const
std::auto_ptr< Appender > create_file_appender(const FactoryParams &)
details::parameter_validator get_for(const char *tag) const


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