FileChannel.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // FileChannel.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/FileChannel.h#5 $
  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. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_FileChannel_INCLUDED
  18. #define Foundation_FileChannel_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Channel.h"
  21. #include "Poco/Timestamp.h"
  22. #include "Poco/Mutex.h"
  23. namespace Poco {
  24. class LogFile;
  25. class RotateStrategy;
  26. class ArchiveStrategy;
  27. class PurgeStrategy;
  28. class Foundation_API FileChannel: public Channel
  29. /// A Channel that writes to a file. This class supports
  30. /// flexible log file rotation and archiving, as well
  31. /// as automatic purging of archived log files.
  32. ///
  33. /// Only the message's text is written, followed
  34. /// by a newline.
  35. ///
  36. /// Chain this channel to a FormattingChannel with an
  37. /// appropriate Formatter to control what is in the text.
  38. ///
  39. /// The FileChannel support log file rotation based
  40. /// on log file size or time intervals.
  41. /// Archived log files can be compressed in gzip format.
  42. /// Older archived files can be automatically deleted
  43. /// (purged).
  44. ///
  45. /// The rotation strategy can be specified with the
  46. /// "rotation" property, which can take one of the
  47. /// follwing values:
  48. ///
  49. /// * never: no log rotation
  50. /// * [day,][hh]:mm: the file is rotated on specified day/time
  51. /// day - day is specified as long or short day name (Monday|Mon, Tuesday|Tue, ... );
  52. /// day can be omitted, in which case log is rotated every day
  53. /// hh - valid hour range is 00-23;
  54. /// hour can be omitted, in which case log is rotated every hour
  55. /// mm - valid minute range is 00-59;
  56. /// minute must be specified
  57. /// * daily: the file is rotated daily
  58. /// * weekly: the file is rotated every seven days
  59. /// * monthly: the file is rotated every 30 days
  60. /// * <n> minutes: the file is rotated every <n> minutes,
  61. /// where <n> is an integer greater than zero.
  62. /// * <n> hours: the file is rotated every <n> hours, where
  63. /// <n> is an integer greater than zero.
  64. /// * <n> days: the file is rotated every <n> days, where
  65. /// <n> is an integer greater than zero.
  66. /// * <n> weeks: the file is rotated every <n> weeks, where
  67. /// <n> is an integer greater than zero.
  68. /// * <n> months: the file is rotated every <n> months, where
  69. /// <n> is an integer greater than zero and
  70. /// a month has 30 days.
  71. /// * <n>: the file is rotated when its size exceeds
  72. /// <n> bytes.
  73. /// * <n> K: the file is rotated when its size exceeds
  74. /// <n> Kilobytes.
  75. /// * <n> M: the file is rotated when its size exceeds
  76. /// <n> Megabytes.
  77. ///
  78. /// NOTE: For periodic log file rotation (daily, weekly, monthly, etc.),
  79. /// the date and time of log file creation or last rotation is
  80. /// written into the first line of the log file. This is because
  81. /// there is no reliable way to find out the real creation date of
  82. /// a file on many platforms (e.g., most Unix platforms do not
  83. /// provide the creation date, and Windows has its own issues
  84. /// with its "File System Tunneling Capabilities").
  85. ///
  86. /// Using the "archive" property it is possible to specify
  87. /// how archived log files are named. The following values
  88. /// for the "archive" property are supported:
  89. ///
  90. /// * number: A number, starting with 0, is appended to
  91. /// the name of archived log files. The newest
  92. /// archived log file always has the number 0.
  93. /// For example, if the log file is named
  94. /// "access.log", and it fulfils the criteria
  95. /// for rotation, the file is renamed to
  96. /// "access.log.0". If a file named "access.log.0"
  97. /// already exists, it is renamed to "access.log.1",
  98. /// and so on.
  99. /// * timestamp: A timestamp is appended to the log file name.
  100. /// For example, if the log file is named
  101. /// "access.log", and it fulfils the criteria
  102. /// for rotation, the file is renamed to
  103. /// "access.log.20050802110300".
  104. ///
  105. /// Using the "times" property it is possible to specify
  106. /// time mode for the day/time based rotation. The following values
  107. /// for the "times" property are supported:
  108. ///
  109. /// * utc: Rotation strategy is based on UTC time (default).
  110. /// * local: Rotation strategy is based on local time.
  111. ///
  112. /// Archived log files can be compressed using the gzip compression
  113. /// method. Compressing can be controlled with the "compress"
  114. /// property. The following values for the "compress" property
  115. /// are supported:
  116. ///
  117. /// * true: Compress archived log files.
  118. /// * false: Do not compress archived log files.
  119. ///
  120. /// Archived log files can be automatically purged, either if
  121. /// they reach a certain age, or if the number of archived
  122. /// log files reaches a given maximum number. This is
  123. /// controlled by the purgeAge and purgeCount properties.
  124. ///
  125. /// The purgeAge property can have the following values:
  126. ///
  127. /// * <n> [seconds]: the maximum age is <n> seconds.
  128. /// * <n> minutes: the maximum age is <n> minutes.
  129. /// * <n> hours: the maximum age is <n> hours.
  130. /// * <n> days: the maximum age is <n> days.
  131. /// * <n> weeks: the maximum age is <n> weeks.
  132. /// * <n> months: the maximum age is <n> months, where a month has 30 days.
  133. ///
  134. /// The purgeCount property has an integer value that specifies the maximum number
  135. /// of archived log files. If the number is exceeded, archived log files are
  136. /// deleted, starting with the oldest. When "none" or empty string are
  137. /// supplied, they reset purgeCount to none (no purging).
  138. ///
  139. /// The flush property specifies whether each log message is flushed
  140. /// immediately to the log file (which may hurt application performance,
  141. /// but ensures that everything is in the log in case of a system crash),
  142. // or whether it's allowed to stay in the system's file buffer for some time.
  143. /// Valid values are:
  144. ///
  145. /// * true: Every essages is immediately flushed to the log file (default).
  146. /// * false: Messages are not immediately flushed to the log file.
  147. ///
  148. /// The rotateOnOpen property specifies whether an existing log file should be
  149. /// rotated (and archived) when the channel is opened. Valid values are:
  150. ///
  151. /// * true: The log file is rotated (and archived) when the channel is opened.
  152. /// * false: Log messages will be appended to an existing log file,
  153. /// if it exists (unless other conditions for a rotation are met).
  154. /// This is the default.
  155. ///
  156. /// For a more lightweight file channel class, see SimpleFileChannel.
  157. {
  158. public:
  159. FileChannel();
  160. /// Creates the FileChannel.
  161. FileChannel(const std::string& path);
  162. /// Creates the FileChannel for a file with the given path.
  163. void open();
  164. /// Opens the FileChannel and creates the log file if necessary.
  165. void close();
  166. /// Closes the FileChannel.
  167. void log(const Message& msg);
  168. /// Logs the given message to the file.
  169. void setProperty(const std::string& name, const std::string& value);
  170. /// Sets the property with the given name.
  171. ///
  172. /// The following properties are supported:
  173. /// * path: The log file's path.
  174. /// * rotation: The log file's rotation mode. See the
  175. /// FileChannel class for details.
  176. /// * archive: The log file's archive mode. See the
  177. /// FileChannel class for details.
  178. /// * times: The log file's time mode. See the
  179. /// FileChannel class for details.
  180. /// * compress: Enable or disable compression of
  181. /// archived files. See the FileChannel class
  182. /// for details.
  183. /// * purgeAge: Maximum age of an archived log file before
  184. /// it is purged. See the FileChannel class for
  185. /// details.
  186. /// * purgeCount: Maximum number of archived log files before
  187. /// files are purged. See the FileChannel class
  188. /// for details.
  189. /// * flush: Specifies whether messages are immediately
  190. /// flushed to the log file. See the FileChannel class
  191. /// for details.
  192. /// * rotateOnOpen: Specifies whether an existing log file should be
  193. /// rotated and archived when the channel is opened.
  194. std::string getProperty(const std::string& name) const;
  195. /// Returns the value of the property with the given name.
  196. /// See setProperty() for a description of the supported
  197. /// properties.
  198. Timestamp creationDate() const;
  199. /// Returns the log file's creation date.
  200. UInt64 size() const;
  201. /// Returns the log file's current size in bytes.
  202. const std::string& path() const;
  203. /// Returns the log file's path.
  204. static const std::string PROP_PATH;
  205. static const std::string PROP_ROTATION;
  206. static const std::string PROP_ARCHIVE;
  207. static const std::string PROP_TIMES;
  208. static const std::string PROP_COMPRESS;
  209. static const std::string PROP_PURGEAGE;
  210. static const std::string PROP_PURGECOUNT;
  211. static const std::string PROP_FLUSH;
  212. static const std::string PROP_ROTATEONOPEN;
  213. protected:
  214. ~FileChannel();
  215. void setRotation(const std::string& rotation);
  216. void setArchive(const std::string& archive);
  217. void setCompress(const std::string& compress);
  218. void setPurgeAge(const std::string& age);
  219. void setPurgeCount(const std::string& count);
  220. void setFlush(const std::string& flush);
  221. void setRotateOnOpen(const std::string& rotateOnOpen);
  222. void purge();
  223. private:
  224. std::string _path;
  225. std::string _times;
  226. std::string _rotation;
  227. std::string _archive;
  228. bool _compress;
  229. std::string _purgeAge;
  230. std::string _purgeCount;
  231. bool _flush;
  232. bool _rotateOnOpen;
  233. LogFile* _pFile;
  234. RotateStrategy* _pRotateStrategy;
  235. ArchiveStrategy* _pArchiveStrategy;
  236. PurgeStrategy* _pPurgeStrategy;
  237. FastMutex _mutex;
  238. };
  239. } // namespace Poco
  240. #endif // Foundation_FileChannel_INCLUDED