log4cplus  1.1.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-2010 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 
00105     class LOG4CPLUS_EXPORT FileAppender : public Appender {
00106     public:
00107       // Ctors
00108         FileAppender(const log4cplus::tstring& filename, 
00109                      std::ios_base::openmode mode = std::ios_base::trunc,
00110                      bool immediateFlush = true);
00111         FileAppender(const log4cplus::helpers::Properties& properties,
00112                      std::ios_base::openmode mode = std::ios_base::trunc);
00113 
00114       // Dtor
00115         virtual ~FileAppender();
00116 
00117       // Methods
00118         virtual void close();
00119 
00122         virtual std::locale imbue(std::locale const& loc);
00123 
00125         virtual std::locale getloc () const;
00126 
00127     protected:
00128         virtual void append(const spi::InternalLoggingEvent& event);
00129 
00130         void open(std::ios_base::openmode mode);
00131         bool reopen();
00132 
00133       // Data
00146         bool immediateFlush;
00147 
00155         int reopenDelay;
00156 
00157         unsigned long bufferSize;
00158         log4cplus::tchar * buffer;
00159 
00160         log4cplus::tofstream out;
00161         log4cplus::tstring filename;
00162         log4cplus::tstring localeName;
00163 
00164         log4cplus::helpers::Time reopen_time;
00165 
00166     private:
00167         LOG4CPLUS_PRIVATE void init(const log4cplus::tstring& filename,
00168             std::ios_base::openmode mode,
00169             const log4cplus::tstring& lockFileName);
00170 
00171       // Disallow copying of instances of this class
00172         FileAppender(const FileAppender&);
00173         FileAppender& operator=(const FileAppender&);
00174     };
00175 
00176 
00177 
00198     class LOG4CPLUS_EXPORT RollingFileAppender : public FileAppender {
00199     public:
00200       // Ctors
00201         RollingFileAppender(const log4cplus::tstring& filename,
00202                             long maxFileSize = 10*1024*1024, // 10 MB
00203                             int maxBackupIndex = 1,
00204                             bool immediateFlush = true);
00205         RollingFileAppender(const log4cplus::helpers::Properties& properties);
00206 
00207       // Dtor
00208         virtual ~RollingFileAppender();
00209 
00210     protected:
00211         virtual void append(const spi::InternalLoggingEvent& event);
00212         void rollover(bool alreadyLocked = false);
00213 
00214       // Data
00215         long maxFileSize;
00216         int maxBackupIndex;
00217 
00218     private:
00219         LOG4CPLUS_PRIVATE void init(long maxFileSize, int maxBackupIndex);
00220     };
00221 
00222 
00223 
00224     enum DailyRollingFileSchedule { MONTHLY, WEEKLY, DAILY,
00225                                     TWICE_DAILY, HOURLY, MINUTELY};
00226 
00248     class LOG4CPLUS_EXPORT DailyRollingFileAppender : public FileAppender {
00249     public:
00250       // Ctors
00251         DailyRollingFileAppender(const log4cplus::tstring& filename,
00252                                  DailyRollingFileSchedule schedule = DAILY,
00253                                  bool immediateFlush = true,
00254                                  int maxBackupIndex = 10);
00255         DailyRollingFileAppender(const log4cplus::helpers::Properties& properties);
00256 
00257       // Dtor
00258         virtual ~DailyRollingFileAppender();
00259         
00260       // Methods
00261         virtual void close();
00262 
00263     protected:
00264         virtual void append(const spi::InternalLoggingEvent& event);
00265         void rollover(bool alreadyLocked = false);
00266         log4cplus::helpers::Time calculateNextRolloverTime(const log4cplus::helpers::Time& t) const;
00267         log4cplus::tstring getFilename(const log4cplus::helpers::Time& t) const;
00268 
00269       // Data
00270         DailyRollingFileSchedule schedule;
00271         log4cplus::tstring scheduledFilename;
00272         log4cplus::helpers::Time nextRolloverTime;
00273         int maxBackupIndex;
00274 
00275     private:
00276         LOG4CPLUS_PRIVATE void init(DailyRollingFileSchedule schedule);
00277     };
00278 
00279 } // end namespace log4cplus
00280 
00281 #endif // LOG4CPLUS_FILE_APPENDER_HEADER_
00282