FileChannel.h 11 KB

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