1 // Module: Log4CPLUS
2 // File: fileappender.h
3 // Created: 6/2001
4 // Author: Tad E. Smith
5 //
6 //
7 // Copyright (C) Tad E. Smith All rights reserved.
8 //
9 // This software is published under the terms of the Apache Software
10 // License version 1.1, a copy of which has been included with this
11 // distribution in the LICENSE.APL file.
12 //
13
14 /** @file */
15
16 #ifndef _LOG4CPLUS_FILE_APPENDER_HEADER_
17 #define _LOG4CPLUS_FILE_APPENDER_HEADER_
18
19 #include <log4cplus/config.h>
20 #include <log4cplus/appender.h>
21 #include <log4cplus/fstreams.h>
22 #include <log4cplus/helpers/property.h>
23
24 #if defined(__DECCXX)
25 # define LOG4CPLUS_OPEN_MODE_TYPE LOG4CPLUS_FSTREAM_NAMESPACE::ios::open_mode
26 #else
27 # define LOG4CPLUS_OPEN_MODE_TYPE LOG4CPLUS_FSTREAM_NAMESPACE::ios::openmode
28 #endif
29
30 namespace log4cplus {
31
32 /**
33 * Appends log events to a file.
34 */
35 class LOG4CPLUS_EXPORT FileAppender : public Appender {
36 public:
37 // Ctors
38 FileAppender(const log4cplus::tstring& filename,
39 LOG4CPLUS_OPEN_MODE_TYPE mode = LOG4CPLUS_FSTREAM_NAMESPACE::ios::trunc,
40 bool immediateFlush = true);
41 FileAppender(const log4cplus::helpers::Properties& properties,
42 LOG4CPLUS_OPEN_MODE_TYPE mode = LOG4CPLUS_FSTREAM_NAMESPACE::ios::trunc);
43
44 // Dtor
45 virtual ~FileAppender();
46
47 // Methods
48 virtual void close();
49
50 protected:
51 virtual void append(const spi::InternalLoggingEvent& event);
52
53 // Data
54 /**
55 * Immediate flush means that the underlying writer or output stream
56 * will be flushed at the end of each append operation. Immediate
57 * flush is slower but ensures that each append request is actually
58 * written. If <code>immediateFlush</code> is set to
59 * <code>false</code>, then there is a good chance that the last few
60 * logs events are not actually written to persistent media if and
61 * when the application crashes.
62 *
63 * <p>The <code>immediateFlush</code> variable is set to
64 * <code>true</code> by default.
65 */
66 bool immediateFlush;
67
68 log4cplus::tofstream out;
69 log4cplus::tstring filename;
70
71 private:
72 void init(const log4cplus::tstring& filename,
73 LOG4CPLUS_OPEN_MODE_TYPE mode);
74
75 // Disallow copying of instances of this class
76 FileAppender(const FileAppender&);
77 FileAppender& operator=(const FileAppender&);
78 };
79
80
81
82 /**
83 * RollingFileAppender extends FileAppender to backup the log files when
84 * they reach a certain size.
85 */
86 class LOG4CPLUS_EXPORT RollingFileAppender : public FileAppender {
87 public:
88 // Ctors
89 RollingFileAppender(const log4cplus::tstring& filename,
90 long maxFileSize = 10*1024*1024, // 10 MB
91 int maxBackupIndex = 1,
92 bool immediateFlush = true);
93 RollingFileAppender(const log4cplus::helpers::Properties& properties);
94
95 // Dtor
96 virtual ~RollingFileAppender();
97
98 protected:
99 virtual void append(const spi::InternalLoggingEvent& event);
100 void rollover();
101
102 // Data
103 long maxFileSize;
104 int maxBackupIndex;
105
106 private:
107 void init(long maxFileSize, int maxBackupIndex);
108 };
109
110
111
112 enum DailyRollingFileSchedule { MONTHLY, WEEKLY, DAILY,
113 TWICE_DAILY, HOURLY, MINUTELY};
114
115 /**
116 * DailyRollingFileAppender extends {@link FileAppender} so that the
117 * underlying file is rolled over at a user chosen frequency.
118 *
119 * <p>
120 *
121 */
122 class LOG4CPLUS_EXPORT DailyRollingFileAppender : public FileAppender {
123 public:
124 // Ctors
125 DailyRollingFileAppender(const log4cplus::tstring& filename,
126 DailyRollingFileSchedule schedule = DAILY,
127 bool immediateFlush = true,
128 int maxBackupIndex = 10);
129 DailyRollingFileAppender(const log4cplus::helpers::Properties& properties);
130
131 // Dtor
132 virtual ~DailyRollingFileAppender();
133
134 // Methods
135 virtual void close();
136
137 protected:
138 virtual void append(const spi::InternalLoggingEvent& event);
139 void rollover();
140 log4cplus::helpers::Time calculateNextRolloverTime(const log4cplus::helpers::Time& t) const;
141 log4cplus::tstring getFilename(const log4cplus::helpers::Time& t) const;
142
143 // Data
144 DailyRollingFileSchedule schedule;
145 log4cplus::tstring scheduledFilename;
146 log4cplus::helpers::Time nextRolloverTime;
147 int maxBackupIndex;
148
149 private:
150 void init(DailyRollingFileSchedule schedule);
151 };
152
153 } // end namespace log4cplus
154
155 #endif // _LOG4CPLUS_FILE_APPENDER_HEADER_
syntax highlighted by Code2HTML, v. 0.9.1