1 | // |
---|
2 | // FileChannel.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/FileChannel.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Logging |
---|
8 | // Module: FileChannel |
---|
9 | // |
---|
10 | // Definition of the FileChannel class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_FileChannel_INCLUDED |
---|
40 | #define Foundation_FileChannel_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Channel.h" |
---|
45 | #include "Poco/Timestamp.h" |
---|
46 | #include "Poco/Mutex.h" |
---|
47 | |
---|
48 | |
---|
49 | namespace Poco { |
---|
50 | |
---|
51 | |
---|
52 | class LogFile; |
---|
53 | class RotateStrategy; |
---|
54 | class ArchiveStrategy; |
---|
55 | class PurgeStrategy; |
---|
56 | |
---|
57 | |
---|
58 | class Foundation_API FileChannel: public Channel |
---|
59 | /// A Channel that writes to a file. This class supports |
---|
60 | /// flexible log file rotation and archiving, as well |
---|
61 | /// as automatic purging of archived log files. |
---|
62 | /// |
---|
63 | /// Only the message's text is written, followed |
---|
64 | /// by a newline. |
---|
65 | /// |
---|
66 | /// Chain this channel to a FormattingChannel with an |
---|
67 | /// appropriate Formatter to control what is in the text. |
---|
68 | /// |
---|
69 | /// The FileChannel support log file rotation based |
---|
70 | /// on log file size or time intervals. |
---|
71 | /// Archived log files can be compressed in gzip format. |
---|
72 | /// Older archived files can be automatically deleted |
---|
73 | /// (purged). |
---|
74 | /// |
---|
75 | /// The rotation strategy can be specified with the |
---|
76 | /// "rotation" property, which can take one of the |
---|
77 | /// follwing values: |
---|
78 | /// |
---|
79 | /// * never: no log rotation |
---|
80 | /// * [day,][hh]:mm: the file is rotated on specified day/time |
---|
81 | /// day - day is specified as long or short day name (Monday|Mon, Tuesday|Tue, ... ); |
---|
82 | /// day can be omitted, in which case log is rotated every day |
---|
83 | /// hh - valid hour range is 00-23; |
---|
84 | /// hour can be omitted, in which case log is rotated every hour |
---|
85 | /// mm - valid minute range is 00-59; |
---|
86 | /// minute must be specified |
---|
87 | /// * daily: the file is rotated daily |
---|
88 | /// * weekly: the file is rotated every seven days |
---|
89 | /// * monthly: the file is rotated every 30 days |
---|
90 | /// * <n> minutes: the file is rotated every <n> minutes, |
---|
91 | /// where <n> is an integer greater than zero. |
---|
92 | /// * <n> hours: the file is rotated every <n> hours, where |
---|
93 | /// <n> is an integer greater than zero. |
---|
94 | /// * <n> days: the file is rotated every <n> days, where |
---|
95 | /// <n> is an integer greater than zero. |
---|
96 | /// * <n> weeks: the file is rotated every <n> weeks, where |
---|
97 | /// <n> is an integer greater than zero. |
---|
98 | /// * <n> months: the file is rotated every <n> months, where |
---|
99 | /// <n> is an integer greater than zero and |
---|
100 | /// a month has 30 days. |
---|
101 | /// * <n>: the file is rotated when its size exceeds |
---|
102 | /// <n> bytes. |
---|
103 | /// * <n> K: the file is rotated when its size exceeds |
---|
104 | /// <n> Kilobytes. |
---|
105 | /// * <n> M: the file is rotated when its size exceeds |
---|
106 | /// <n> Megabytes. |
---|
107 | /// |
---|
108 | /// NOTE: For periodic log file rotation (daily, weekly, monthly, etc.), |
---|
109 | /// the date and time of log file creation or last rotation is |
---|
110 | /// written into the first line of the log file. This is because |
---|
111 | /// there is no reliable way to find out the real creation date of |
---|
112 | /// a file on many platforms (e.g., most Unix platforms do not |
---|
113 | /// provide the creation date, and Windows has its own issues |
---|
114 | /// with its "File System Tunneling Capabilities"). |
---|
115 | /// |
---|
116 | /// Using the "archive" property it is possible to specify |
---|
117 | /// how archived log files are named. The following values |
---|
118 | /// for the "archive" property are supported: |
---|
119 | /// |
---|
120 | /// * number: A number, starting with 0, is appended to |
---|
121 | /// the name of archived log files. The newest |
---|
122 | /// archived log file always has the number 0. |
---|
123 | /// For example, if the log file is named |
---|
124 | /// "access.log", and it fulfils the criteria |
---|
125 | /// for rotation, the file is renamed to |
---|
126 | /// "access.log.0". If a file named "access.log.0" |
---|
127 | /// already exists, it is renamed to "access.log.1", |
---|
128 | /// and so on. |
---|
129 | /// * timestamp: A timestamp is appended to the log file name. |
---|
130 | /// For example, if the log file is named |
---|
131 | /// "access.log", and it fulfils the criteria |
---|
132 | /// for rotation, the file is renamed to |
---|
133 | /// "access.log.20050802110300". |
---|
134 | /// |
---|
135 | /// Using the "times" property it is possible to specify |
---|
136 | /// time mode for the day/time based rotation. The following values |
---|
137 | /// for the "times" property are supported: |
---|
138 | /// |
---|
139 | /// * utc: Rotation strategy is based on UTC time (default). |
---|
140 | /// * local: Rotation strategy is based on local time. |
---|
141 | /// |
---|
142 | /// Archived log files can be compressed using the gzip compression |
---|
143 | /// method. Compressing can be controlled with the "compression" |
---|
144 | /// property. The following values for the "compress" property |
---|
145 | /// are supported: |
---|
146 | /// |
---|
147 | /// * true: Compress archived log files. |
---|
148 | /// * false: Do not compress archived log files. |
---|
149 | /// |
---|
150 | /// Archived log files can be automatically purged, either if |
---|
151 | /// they reach a certain age, or if the number of archived |
---|
152 | /// log files reaches a given maximum number. This is |
---|
153 | /// controlled by the purgeAge and purgeCount properties. |
---|
154 | /// |
---|
155 | /// The purgeAge property can have the following values: |
---|
156 | /// |
---|
157 | /// * <n> [seconds] the maximum age is <n> seconds. |
---|
158 | /// * <n> minutes: the maximum age is <n> minutes. |
---|
159 | /// * <n> hours: the maximum age is <n> hours. |
---|
160 | /// * <n> days: the maximum age is <n> days. |
---|
161 | /// * <n> weeks: the maximum age is <n> weeks. |
---|
162 | /// * <n> months: the maximum age is <n> months, where a month has 30 days. |
---|
163 | /// |
---|
164 | /// The purgeCount property has an integer value that |
---|
165 | /// specifies the maximum number of archived log files. |
---|
166 | /// If the number is exceeded, archived log files are |
---|
167 | /// deleted, starting with the oldest. |
---|
168 | /// |
---|
169 | /// For a more lightweight file channel class, see SimpleFileChannel. |
---|
170 | { |
---|
171 | public: |
---|
172 | FileChannel(); |
---|
173 | /// Creates the FileChannel. |
---|
174 | |
---|
175 | FileChannel(const std::string& path); |
---|
176 | /// Creates the FileChannel for a file with the given path. |
---|
177 | |
---|
178 | void open(); |
---|
179 | /// Opens the FileChannel and creates the log file if necessary. |
---|
180 | |
---|
181 | void close(); |
---|
182 | /// Closes the FileChannel. |
---|
183 | |
---|
184 | void log(const Message& msg); |
---|
185 | /// Logs the given message to the file. |
---|
186 | |
---|
187 | void setProperty(const std::string& name, const std::string& value); |
---|
188 | /// Sets the property with the given name. |
---|
189 | /// |
---|
190 | /// The following properties are supported: |
---|
191 | /// * path: The log file's path. |
---|
192 | /// * rotation: The log file's rotation mode. See the |
---|
193 | /// FileChannel class for details. |
---|
194 | /// * archive: The log file's archive mode. See the |
---|
195 | /// FileChannel class for details. |
---|
196 | /// * times: The log file's time mode. See the |
---|
197 | /// FileChannel class for details. |
---|
198 | /// * compress: Enable or disable compression of |
---|
199 | /// archived files. See the FileChannel class |
---|
200 | /// for details. |
---|
201 | /// * purgeAge: Maximum age of an archived log file before |
---|
202 | /// it is purged. See the FileChannel class for |
---|
203 | /// details. |
---|
204 | /// * purgeCount: Maximum number of archived log files before |
---|
205 | /// files are purged. See the FileChannel class |
---|
206 | /// for details. |
---|
207 | |
---|
208 | std::string getProperty(const std::string& name) const; |
---|
209 | /// Returns the value of the property with the given name. |
---|
210 | /// See setProperty() for a description of the supported |
---|
211 | /// properties. |
---|
212 | |
---|
213 | Timestamp creationDate() const; |
---|
214 | /// Returns the log file's creation date. |
---|
215 | |
---|
216 | UInt64 size() const; |
---|
217 | /// Returns the log file's current size in bytes. |
---|
218 | |
---|
219 | const std::string& path() const; |
---|
220 | /// Returns the log file's path. |
---|
221 | |
---|
222 | static const std::string PROP_PATH; |
---|
223 | static const std::string PROP_ROTATION; |
---|
224 | static const std::string PROP_ARCHIVE; |
---|
225 | static const std::string PROP_TIMES; |
---|
226 | static const std::string PROP_COMPRESS; |
---|
227 | static const std::string PROP_PURGEAGE; |
---|
228 | static const std::string PROP_PURGECOUNT; |
---|
229 | |
---|
230 | protected: |
---|
231 | ~FileChannel(); |
---|
232 | void setRotation(const std::string& rotation); |
---|
233 | void setArchive(const std::string& archive); |
---|
234 | void setCompress(const std::string& compress); |
---|
235 | void setPurgeAge(const std::string& age); |
---|
236 | void setPurgeCount(const std::string& count); |
---|
237 | void purge(); |
---|
238 | |
---|
239 | private: |
---|
240 | std::string _path; |
---|
241 | std::string _times; |
---|
242 | std::string _rotation; |
---|
243 | std::string _archive; |
---|
244 | bool _compress; |
---|
245 | std::string _purgeAge; |
---|
246 | std::string _purgeCount; |
---|
247 | LogFile* _pFile; |
---|
248 | RotateStrategy* _pRotateStrategy; |
---|
249 | ArchiveStrategy* _pArchiveStrategy; |
---|
250 | PurgeStrategy* _pPurgeStrategy; |
---|
251 | FastMutex _mutex; |
---|
252 | }; |
---|
253 | |
---|
254 | |
---|
255 | } // namespace Poco |
---|
256 | |
---|
257 | |
---|
258 | #endif // Foundation_FileChannel_INCLUDED |
---|