log4cplus  2.0.0
fileappender.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // Module:  Log4CPLUS
00003 // File:    fileappender.h
00004 // Created: 6/2001
00005 // Author:  Tad E. Smith
00006 //
00007 //
00008 // Copyright 2001-2015 Tad E. Smith
00009 //
00010 // Licensed under the Apache License, Version 2.0 (the "License");
00011 // you may not use this file except in compliance with the License.
00012 // You may obtain a copy of the License at
00013 //
00014 //     http://www.apache.org/licenses/LICENSE-2.0
00015 //
00016 // Unless required by applicable law or agreed to in writing, software
00017 // distributed under the License is distributed on an "AS IS" BASIS,
00018 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019 // See the License for the specific language governing permissions and
00020 // limitations under the License.
00021 
00024 #ifndef LOG4CPLUS_FILE_APPENDER_HEADER_
00025 #define LOG4CPLUS_FILE_APPENDER_HEADER_
00026 
00027 #include <log4cplus/config.hxx>
00028 
00029 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
00030 #pragma once
00031 #endif
00032 
00033 #include <log4cplus/appender.h>
00034 #include <log4cplus/fstreams.h>
00035 #include <log4cplus/helpers/timehelper.h>
00036 #include <log4cplus/helpers/lockfile.h>
00037 #include <fstream>
00038 #include <locale>
00039 #include <memory>
00040 
00041 
00042 namespace log4cplus
00043 {
00044 
00109     class LOG4CPLUS_EXPORT FileAppender : public Appender {
00110     public:
00111       // Ctors
00112         FileAppender(const log4cplus::tstring& filename,
00113                      std::ios_base::openmode mode = std::ios_base::trunc,
00114                      bool immediateFlush = true, bool createDirs = false);
00115         FileAppender(const log4cplus::helpers::Properties& properties,
00116                      std::ios_base::openmode mode = std::ios_base::trunc);
00117 
00118       // Dtor
00119         virtual ~FileAppender();
00120 
00121       // Methods
00122         virtual void close();
00123 
00126         virtual std::locale imbue(std::locale const& loc);
00127 
00129         virtual std::locale getloc () const;
00130 
00131     protected:
00132         virtual void append(const spi::InternalLoggingEvent& event);
00133 
00134         void open(std::ios_base::openmode mode);
00135         bool reopen();
00136 
00137       // Data
00150         bool immediateFlush;
00151 
00158         bool createDirs;
00159 
00167         int reopenDelay;
00168 
00169         unsigned long bufferSize;
00170         std::unique_ptr<log4cplus::tchar[]> buffer;
00171 
00172         log4cplus::tofstream out;
00173         log4cplus::tstring filename;
00174         log4cplus::tstring localeName;
00175 
00176         log4cplus::helpers::Time reopen_time;
00177 
00178     private:
00179         LOG4CPLUS_PRIVATE void init(const log4cplus::tstring& filename,
00180             std::ios_base::openmode mode,
00181             const log4cplus::tstring& lockFileName);
00182 
00183       // Disallow copying of instances of this class
00184         FileAppender(const FileAppender&);
00185         FileAppender& operator=(const FileAppender&);
00186     };
00187 
00188 
00189     typedef helpers::SharedObjectPtr<FileAppender> SharedFileAppenderPtr;
00190 
00191 
00192 
00213     class LOG4CPLUS_EXPORT RollingFileAppender : public FileAppender {
00214     public:
00215       // Ctors
00216         RollingFileAppender(const log4cplus::tstring& filename,
00217                             long maxFileSize = 10*1024*1024, // 10 MB
00218                             int maxBackupIndex = 1,
00219                             bool immediateFlush = true,
00220                             bool createDirs = false);
00221         RollingFileAppender(const log4cplus::helpers::Properties& properties);
00222 
00223       // Dtor
00224         virtual ~RollingFileAppender();
00225 
00226     protected:
00227         virtual void append(const spi::InternalLoggingEvent& event);
00228         void rollover(bool alreadyLocked = false);
00229 
00230       // Data
00231         long maxFileSize;
00232         int maxBackupIndex;
00233 
00234     private:
00235         LOG4CPLUS_PRIVATE void init(long maxFileSize, int maxBackupIndex);
00236     };
00237 
00238 
00239     typedef helpers::SharedObjectPtr<RollingFileAppender>
00240         SharedRollingFileAppenderPtr;
00241 
00242 
00243     enum DailyRollingFileSchedule { MONTHLY, WEEKLY, DAILY,
00244                                     TWICE_DAILY, HOURLY, MINUTELY};
00245 
00267     class LOG4CPLUS_EXPORT DailyRollingFileAppender : public FileAppender {
00268     public:
00269       // Ctors
00270         DailyRollingFileAppender(const log4cplus::tstring& filename,
00271                                  DailyRollingFileSchedule schedule = DAILY,
00272                                  bool immediateFlush = true,
00273                                  int maxBackupIndex = 10,
00274                                  bool createDirs = false);
00275         DailyRollingFileAppender(const log4cplus::helpers::Properties& properties);
00276 
00277       // Dtor
00278         virtual ~DailyRollingFileAppender();
00279 
00280       // Methods
00281         virtual void close();
00282 
00283     protected:
00284         virtual void append(const spi::InternalLoggingEvent& event);
00285         void rollover(bool alreadyLocked = false);
00286         log4cplus::helpers::Time calculateNextRolloverTime(const log4cplus::helpers::Time& t) const;
00287         log4cplus::tstring getFilename(const log4cplus::helpers::Time& t) const;
00288 
00289       // Data
00290         DailyRollingFileSchedule schedule;
00291         log4cplus::tstring scheduledFilename;
00292         log4cplus::helpers::Time nextRolloverTime;
00293         int maxBackupIndex;
00294 
00295     private:
00296         LOG4CPLUS_PRIVATE void init(DailyRollingFileSchedule schedule);
00297     };
00298 
00299     typedef helpers::SharedObjectPtr<DailyRollingFileAppender>
00300         SharedDailyRollingFileAppenderPtr;
00301 
00302 } // end namespace log4cplus
00303 
00304 #endif // LOG4CPLUS_FILE_APPENDER_HEADER_