FileAppender.cpp
Go to the documentation of this file.
00001 /*
00002  * FileAppender.cpp
00003  *
00004  * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
00005  * Copyright 2000, Bastiaan Bakker. All rights reserved.
00006  *
00007  * See the COPYING file for the terms of usage and distribution.
00008  */
00009 
00010 #include "PortabilityImpl.hh"
00011 #ifdef LOG4CPP_HAVE_IO_H
00012 #    include <io.h>
00013 #endif
00014 #ifdef LOG4CPP_HAVE_UNISTD_H
00015 #    include <unistd.h>
00016 #endif
00017 
00018 #include <memory>
00019 #include <stdio.h>
00020 #include <time.h>
00021 #include <log4cpp/FileAppender.hh>
00022 #include <log4cpp/Category.hh>
00023 #include <log4cpp/FactoryParams.hh>
00024 
00025 namespace log4cpp {
00026 
00027     FileAppender::FileAppender(const std::string& name, 
00028                                const std::string& fileName,
00029                                bool append,
00030                                mode_t mode) : 
00031             LayoutAppender(name),
00032             _fileName(fileName),
00033 #ifdef __APPLE__
00034             _flags(O_CREAT | O_APPEND | O_WRONLY),
00035 #else
00036             _flags(O_CREAT | O_APPEND | O_WRONLY | O_LARGEFILE),
00037 #endif
00038             _mode(mode) {
00039         if (!append)
00040             _flags |= O_TRUNC;
00041         _fd = ::open(_fileName.c_str(), _flags, _mode);
00042     }
00043     
00044     FileAppender::FileAppender(const std::string& name, int fd) :
00045         LayoutAppender(name),
00046         _fileName(""),
00047         _fd(fd),
00048 #ifdef __APPLE__
00049         _flags(O_CREAT | O_APPEND | O_WRONLY),
00050 #else
00051         _flags(O_CREAT | O_APPEND | O_WRONLY | O_LARGEFILE),
00052 #endif
00053         _mode(00644) {
00054     }
00055     
00056     FileAppender::~FileAppender() {
00057         close();
00058     }
00059 
00060     void FileAppender::close() {
00061         if (_fd!=-1) {
00062             ::close(_fd);
00063             _fd=-1;
00064         }
00065     }
00066 
00067     void FileAppender::setAppend(bool append) {
00068         if (append) {
00069             _flags &= ~O_TRUNC;
00070         } else {
00071             _flags |= O_TRUNC;
00072         }
00073     }
00074 
00075     bool FileAppender::getAppend() const {
00076         return (_flags & O_TRUNC) == 0;
00077     }
00078 
00079     void FileAppender::setMode(mode_t mode) {
00080         _mode = mode;
00081     }
00082 
00083     mode_t FileAppender::getMode() const {
00084         return _mode;
00085     }
00086 
00087     void FileAppender::_append(const LoggingEvent& event) {
00088         std::string message(_getLayout().format(event));
00089         if (!::write(_fd, message.data(), message.length())) {
00090             // XXX help! help!
00091         }
00092     }
00093 
00094     bool FileAppender::reopen() {
00095         if (_fileName != "") {
00096             int fd = ::open(_fileName.c_str(), _flags, _mode);
00097             if (fd < 0)
00098                 return false;
00099             else {
00100                 if (_fd != -1)
00101                     ::close(_fd);
00102                 _fd = fd;
00103                 return true;
00104             }
00105         } else {
00106             return true;
00107         }      
00108     }
00109 
00110    std::auto_ptr<Appender> create_file_appender(const FactoryParams& params)
00111    {
00112       std::string name, filename;
00113       bool append = true;
00114       mode_t mode = 664;
00115 
00116       params.get_for("file appender").required("name", name)("filename", filename)
00117                                      .optional("append", append)("mode", mode);
00118 
00119       return std::auto_ptr<Appender>(new FileAppender(name, filename, append, mode));
00120    }
00121 }


log4cpp
Author(s): Stephen Roderick
autogenerated on Mon Oct 6 2014 03:13:14