00001 // Module: Log4CPLUS 00002 // File: fileappender.h 00003 // Created: 6/2001 00004 // Author: Tad E. Smith 00005 // 00006 // 00007 // Copyright 2001-2009 Tad E. Smith 00008 // 00009 // Licensed under the Apache License, Version 2.0 (the "License"); 00010 // you may not use this file except in compliance with the License. 00011 // You may obtain a copy of the License at 00012 // 00013 // http://www.apache.org/licenses/LICENSE-2.0 00014 // 00015 // Unless required by applicable law or agreed to in writing, software 00016 // distributed under the License is distributed on an "AS IS" BASIS, 00017 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 // See the License for the specific language governing permissions and 00019 // limitations under the License. 00020 00023 #ifndef _LOG4CPLUS_FILE_APPENDER_HEADER_ 00024 #define _LOG4CPLUS_FILE_APPENDER_HEADER_ 00025 00026 #include "dcmtk/oflog/config.h" 00027 #include "dcmtk/oflog/appender.h" 00028 #include "dcmtk/oflog/fstreams.h" 00029 #include "dcmtk/oflog/helpers/property.h" 00030 #include "dcmtk/oflog/helpers/timehelp.h" 00031 00032 #if defined(__DECCXX) 00033 # define LOG4CPLUS_OPEN_MODE_TYPE LOG4CPLUS_FSTREAM_NAMESPACE::ios::open_mode 00034 #else 00035 # define LOG4CPLUS_OPEN_MODE_TYPE LOG4CPLUS_FSTREAM_NAMESPACE::ios::openmode 00036 #endif 00037 00038 namespace log4cplus { 00039 00064 class LOG4CPLUS_EXPORT FileAppender : public Appender { 00065 public: 00066 // Ctors 00067 FileAppender(const log4cplus::tstring& filename, 00068 LOG4CPLUS_OPEN_MODE_TYPE mode = LOG4CPLUS_FSTREAM_NAMESPACE::ios::trunc, 00069 bool immediateFlush = true); 00070 FileAppender(const log4cplus::helpers::Properties& properties, 00071 log4cplus::tstring& error, 00072 LOG4CPLUS_OPEN_MODE_TYPE mode = LOG4CPLUS_FSTREAM_NAMESPACE::ios::trunc); 00073 00074 // Dtor 00075 virtual ~FileAppender(); 00076 00077 // Methods 00078 virtual void close(); 00079 00080 protected: 00081 virtual void append(const spi::InternalLoggingEvent& event); 00082 00083 void open(LOG4CPLUS_OPEN_MODE_TYPE mode); 00084 bool reopen(); 00085 00086 // Data 00099 bool immediateFlush; 00100 00108 int reopenDelay; 00109 00110 log4cplus::tofstream out; 00111 log4cplus::tstring filename; 00112 00113 log4cplus::helpers::Time reopen_time; 00114 00115 private: 00116 void init(const log4cplus::tstring& filename, 00117 LOG4CPLUS_OPEN_MODE_TYPE mode); 00118 00119 // Disallow copying of instances of this class 00120 FileAppender(const FileAppender&); 00121 FileAppender& operator=(const FileAppender&); 00122 }; 00123 00124 00125 00146 class LOG4CPLUS_EXPORT RollingFileAppender : public FileAppender { 00147 public: 00148 // Ctors 00149 RollingFileAppender(const log4cplus::tstring& filename, 00150 long maxFileSize = 10*1024*1024, // 10 MB 00151 int maxBackupIndex = 1, 00152 bool immediateFlush = true); 00153 RollingFileAppender(const log4cplus::helpers::Properties& properties, log4cplus::tstring& error); 00154 00155 // Dtor 00156 virtual ~RollingFileAppender(); 00157 00158 protected: 00159 virtual void append(const spi::InternalLoggingEvent& event); 00160 void rollover(); 00161 00162 // Data 00163 long maxFileSize; 00164 int maxBackupIndex; 00165 00166 private: 00167 void init(long maxFileSize, int maxBackupIndex); 00168 }; 00169 00170 00171 00172 enum DailyRollingFileSchedule { MONTHLY, WEEKLY, DAILY, 00173 TWICE_DAILY, HOURLY, MINUTELY}; 00174 00196 class LOG4CPLUS_EXPORT DailyRollingFileAppender : public FileAppender { 00197 public: 00198 // Ctors 00199 DailyRollingFileAppender(const log4cplus::tstring& filename, 00200 DailyRollingFileSchedule schedule = DAILY, 00201 bool immediateFlush = true, 00202 int maxBackupIndex = 10); 00203 DailyRollingFileAppender(const log4cplus::helpers::Properties& properties, log4cplus::tstring& error); 00204 00205 // Dtor 00206 virtual ~DailyRollingFileAppender(); 00207 00208 // Methods 00209 virtual void close(); 00210 00211 protected: 00212 virtual void append(const spi::InternalLoggingEvent& event); 00213 void rollover(); 00214 log4cplus::helpers::Time calculateNextRolloverTime(const log4cplus::helpers::Time& t) const; 00215 log4cplus::tstring getFilename(const log4cplus::helpers::Time& t) const; 00216 00217 // Data 00218 DailyRollingFileSchedule schedule; 00219 log4cplus::tstring scheduledFilename; 00220 log4cplus::helpers::Time nextRolloverTime; 00221 int maxBackupIndex; 00222 00223 private: 00224 void init(DailyRollingFileSchedule schedule); 00225 }; 00226 00227 } // end namespace log4cplus 00228 00229 #endif // _LOG4CPLUS_FILE_APPENDER_HEADER_ 00230