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 00069 class LOG4CPLUS_EXPORT FileAppender : public Appender { 00070 public: 00071 // Ctors 00072 FileAppender(const log4cplus::tstring& filename, 00073 LOG4CPLUS_OPEN_MODE_TYPE mode = LOG4CPLUS_FSTREAM_NAMESPACE::ios::trunc, 00074 bool immediateFlush = true); 00075 FileAppender(const log4cplus::helpers::Properties& properties, 00076 log4cplus::tstring& error, 00077 LOG4CPLUS_OPEN_MODE_TYPE mode = LOG4CPLUS_FSTREAM_NAMESPACE::ios::trunc); 00078 00079 // Dtor 00080 virtual ~FileAppender(); 00081 00082 // Methods 00083 virtual void close(); 00084 00085 protected: 00086 virtual void append(const spi::InternalLoggingEvent& event); 00087 00088 void open(LOG4CPLUS_OPEN_MODE_TYPE mode); 00089 bool reopen(); 00090 00091 // Data 00104 bool immediateFlush; 00105 00113 int reopenDelay; 00114 00115 unsigned long bufferSize; 00116 log4cplus::tchar * buffer; 00117 00118 log4cplus::tofstream out; 00119 log4cplus::tstring filename; 00120 00121 log4cplus::helpers::Time reopen_time; 00122 00123 private: 00124 void init(const log4cplus::tstring& filename, 00125 LOG4CPLUS_OPEN_MODE_TYPE mode); 00126 00127 // Disallow copying of instances of this class 00128 FileAppender(const FileAppender&); 00129 FileAppender& operator=(const FileAppender&); 00130 }; 00131 00132 00133 00154 class LOG4CPLUS_EXPORT RollingFileAppender : public FileAppender { 00155 public: 00156 // Ctors 00157 RollingFileAppender(const log4cplus::tstring& filename, 00158 long maxFileSize = 10*1024*1024, // 10 MB 00159 int maxBackupIndex = 1, 00160 bool immediateFlush = true); 00161 RollingFileAppender(const log4cplus::helpers::Properties& properties, log4cplus::tstring& error); 00162 00163 // Dtor 00164 virtual ~RollingFileAppender(); 00165 00166 protected: 00167 virtual void append(const spi::InternalLoggingEvent& event); 00168 void rollover(); 00169 00170 // Data 00171 long maxFileSize; 00172 int maxBackupIndex; 00173 00174 private: 00175 void init(long maxFileSize, int maxBackupIndex); 00176 }; 00177 00178 00179 00180 enum DailyRollingFileSchedule { MONTHLY, WEEKLY, DAILY, 00181 TWICE_DAILY, HOURLY, MINUTELY}; 00182 00204 class LOG4CPLUS_EXPORT DailyRollingFileAppender : public FileAppender { 00205 public: 00206 // Ctors 00207 DailyRollingFileAppender(const log4cplus::tstring& filename, 00208 DailyRollingFileSchedule schedule = DAILY, 00209 bool immediateFlush = true, 00210 int maxBackupIndex = 10); 00211 DailyRollingFileAppender(const log4cplus::helpers::Properties& properties, log4cplus::tstring& error); 00212 00213 // Dtor 00214 virtual ~DailyRollingFileAppender(); 00215 00216 // Methods 00217 virtual void close(); 00218 00219 protected: 00220 virtual void append(const spi::InternalLoggingEvent& event); 00221 void rollover(); 00222 log4cplus::helpers::Time calculateNextRolloverTime(const log4cplus::helpers::Time& t) const; 00223 log4cplus::tstring getFilename(const log4cplus::helpers::Time& t) const; 00224 00225 // Data 00226 DailyRollingFileSchedule schedule; 00227 log4cplus::tstring scheduledFilename; 00228 log4cplus::helpers::Time nextRolloverTime; 00229 int maxBackupIndex; 00230 00231 private: 00232 void init(DailyRollingFileSchedule schedule); 00233 }; 00234 00235 } // end namespace log4cplus 00236 00237 #endif // _LOG4CPLUS_FILE_APPENDER_HEADER_ 00238