You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
9.5 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: fileappender.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2013 Tad E. Smith
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. /** @file */
  22. #ifndef LOG4CPLUS_FILE_APPENDER_HEADER_
  23. #define LOG4CPLUS_FILE_APPENDER_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <log4cplus/appender.h>
  29. #include <log4cplus/fstreams.h>
  30. #include <log4cplus/helpers/timehelper.h>
  31. #include <log4cplus/helpers/lockfile.h>
  32. #include <fstream>
  33. #include <locale>
  34. #include <memory>
  35. namespace log4cplus
  36. {
  37. /**
  38. * Appends log events to a file.
  39. *
  40. * <h3>Properties</h3>
  41. * <dl>
  42. * <dt><tt>File</tt></dt>
  43. * <dd>This property specifies output file name.</dd>
  44. *
  45. * <dt><tt>ImmediateFlush</tt></dt>
  46. * <dd>When it is set true, output stream will be flushed after
  47. * each appended event.</dd>
  48. *
  49. * <dt><tt>Append</tt></dt>
  50. * <dd>When it is set true, output file will be appended to
  51. * instead of being truncated at opening.</dd>
  52. *
  53. * <dt><tt>ReopenDelay</tt></dt>
  54. * <dd>This property sets a delay after which the appender will try
  55. * to reopen log file again, after last logging failure.
  56. * </dd>
  57. *
  58. * <dt><tt>BufferSize</tt></dt>
  59. * <dd>Non-zero value of this property sets up buffering of output
  60. * stream using a buffer of given size.
  61. * </dd>
  62. *
  63. * <dt><tt>UseLockFile</tt></dt>
  64. * <dd>Set this property to <tt>true</tt> if you want your output
  65. * to go into a log file shared by multiple processes. When this
  66. * property is set to true then log4cplus uses OS specific
  67. * facilities (e.g., <code>lockf()</code>) to provide
  68. * inter-process file locking.
  69. * \sa Appender
  70. * </dd>
  71. *
  72. * <dt><tt>LockFile</tt></dt>
  73. * <dd>This property specifies lock file, file used for
  74. * inter-process synchronization of log file access. When this
  75. * property is not specified, the value is derived from
  76. * <tt>File</tt> property by addition of ".lock" suffix. The
  77. * property is only used when <tt>UseLockFile</tt> is set to true.
  78. * \sa Appender
  79. * </dd>
  80. *
  81. * <dt><tt>Locale</tt></dt>
  82. * <dd>This property specifies a locale name that will be imbued
  83. * into output stream. Locale can be specified either by system
  84. * specific locale name, e.g., <tt>en_US.UTF-8</tt>, or by one of
  85. * four recognized keywords: <tt>GLOBAL</tt>, <tt>DEFAULT</tt>
  86. * (which is an alias for <tt>GLOBAL</tt>), <tt>USER</tt> and
  87. * <tt>CLASSIC</tt>. When specified locale is not available,
  88. * <tt>GLOBAL</tt> is used instead. It is possible to register
  89. * additional locale keywords by registering an instance of
  90. * <code>spi::LocaleFactory</code> in
  91. * <code>spi::LocaleFactoryRegistry</code>.
  92. * \sa spi::getLocaleFactoryRegistry()
  93. * </dd>
  94. *
  95. * </dl>
  96. */
  97. class LOG4CPLUS_EXPORT FileAppender : public Appender {
  98. public:
  99. // Ctors
  100. FileAppender(const log4cplus::tstring& filename,
  101. std::ios_base::openmode mode = std::ios_base::trunc,
  102. bool immediateFlush = true);
  103. FileAppender(const log4cplus::helpers::Properties& properties,
  104. std::ios_base::openmode mode = std::ios_base::trunc);
  105. // Dtor
  106. virtual ~FileAppender();
  107. // Methods
  108. virtual void close();
  109. //! Redefine default locale for output stream. It may be a good idea to
  110. //! provide UTF-8 locale in case UNICODE macro is defined.
  111. virtual std::locale imbue(std::locale const& loc);
  112. //! \returns Locale imbued in fstream.
  113. virtual std::locale getloc () const;
  114. protected:
  115. virtual void append(const spi::InternalLoggingEvent& event);
  116. void open(std::ios_base::openmode mode);
  117. bool reopen();
  118. // Data
  119. /**
  120. * Immediate flush means that the underlying writer or output stream
  121. * will be flushed at the end of each append operation. Immediate
  122. * flush is slower but ensures that each append request is actually
  123. * written. If <code>immediateFlush</code> is set to
  124. * <code>false</code>, then there is a good chance that the last few
  125. * logs events are not actually written to persistent media if and
  126. * when the application crashes.
  127. *
  128. * The <code>immediateFlush</code> variable is set to
  129. * <code>true</code> by default.
  130. */
  131. bool immediateFlush;
  132. /**
  133. * When any append operation fails, <code>reopenDelay</code> says
  134. * for how many seconds the next attempt to re-open the log file and
  135. * resume logging will be delayed. If <code>reopenDelay</code> is zero,
  136. * each failed append operation will cause log file to be re-opened.
  137. * By default, <code>reopenDelay</code> is 1 second.
  138. */
  139. int reopenDelay;
  140. unsigned long bufferSize;
  141. log4cplus::tchar * buffer;
  142. log4cplus::tofstream out;
  143. log4cplus::tstring filename;
  144. log4cplus::tstring localeName;
  145. log4cplus::helpers::Time reopen_time;
  146. private:
  147. LOG4CPLUS_PRIVATE void init(const log4cplus::tstring& filename,
  148. std::ios_base::openmode mode,
  149. const log4cplus::tstring& lockFileName);
  150. // Disallow copying of instances of this class
  151. FileAppender(const FileAppender&);
  152. FileAppender& operator=(const FileAppender&);
  153. };
  154. /**
  155. * RollingFileAppender extends FileAppender to backup the log
  156. * files when they reach a certain size.
  157. *
  158. * <h3>Properties</h3>
  159. * <p>Properties additional to {@link FileAppender}'s properties:
  160. *
  161. * <dl>
  162. * <dt><tt>MaxFileSize</tt></dt>
  163. * <dd>This property specifies maximal size of output file. The
  164. * value is in bytes. It is possible to use <tt>MB</tt> and
  165. * <tt>KB</tt> suffixes to specify the value in megabytes or
  166. * kilobytes instead.</dd>
  167. *
  168. * <dt><tt>MaxBackupIndex</tt></dt>
  169. * <dd>This property limits the number of backup output
  170. * files; e.g. how many <tt>log.1</tt>, <tt>log.2</tt> etc. files
  171. * will be kept.</dd>
  172. * </dl>
  173. */
  174. class LOG4CPLUS_EXPORT RollingFileAppender : public FileAppender {
  175. public:
  176. // Ctors
  177. RollingFileAppender(const log4cplus::tstring& filename,
  178. long maxFileSize = 10*1024*1024, // 10 MB
  179. int maxBackupIndex = 1,
  180. bool immediateFlush = true);
  181. RollingFileAppender(const log4cplus::helpers::Properties& properties);
  182. // Dtor
  183. virtual ~RollingFileAppender();
  184. protected:
  185. virtual void append(const spi::InternalLoggingEvent& event);
  186. void rollover(bool alreadyLocked = false);
  187. // Data
  188. long maxFileSize;
  189. int maxBackupIndex;
  190. private:
  191. LOG4CPLUS_PRIVATE void init(long maxFileSize, int maxBackupIndex);
  192. };
  193. enum DailyRollingFileSchedule { MONTHLY, WEEKLY, DAILY,
  194. TWICE_DAILY, HOURLY, MINUTELY};
  195. /**
  196. * DailyRollingFileAppender extends {@link FileAppender} so that the
  197. * underlying file is rolled over at a user chosen frequency.
  198. *
  199. * <h3>Properties</h3>
  200. * <p>Properties additional to {@link FileAppender}'s properties:
  201. *
  202. * <dl>
  203. * <dt><tt>Schedule</tt></dt>
  204. * <dd>This property specifies rollover schedule. The possible
  205. * values are <tt>MONTHLY</tt>, <tt>WEEKLY</tt>, <tt>DAILY</tt>,
  206. * <tt>TWICE_DAILY</tt>, <tt>HOURLY</tt> and
  207. * <tt>MINUTELY</tt>.</dd>
  208. *
  209. * <dt><tt>MaxBackupIndex</tt></dt>
  210. * <dd>This property limits how many backup files are kept per
  211. * single logging period; e.g. how many <tt>log.2009-11-07.1</tt>,
  212. * <tt>log.2009-11-07.2</tt> etc. files are kept.</dd>
  213. *
  214. * </dl>
  215. */
  216. class LOG4CPLUS_EXPORT DailyRollingFileAppender : public FileAppender {
  217. public:
  218. // Ctors
  219. DailyRollingFileAppender(const log4cplus::tstring& filename,
  220. DailyRollingFileSchedule schedule = DAILY,
  221. bool immediateFlush = true,
  222. int maxBackupIndex = 10);
  223. DailyRollingFileAppender(const log4cplus::helpers::Properties& properties);
  224. // Dtor
  225. virtual ~DailyRollingFileAppender();
  226. // Methods
  227. virtual void close();
  228. protected:
  229. virtual void append(const spi::InternalLoggingEvent& event);
  230. void rollover(bool alreadyLocked = false);
  231. log4cplus::helpers::Time calculateNextRolloverTime(const log4cplus::helpers::Time& t) const;
  232. log4cplus::tstring getFilename(const log4cplus::helpers::Time& t) const;
  233. // Data
  234. DailyRollingFileSchedule schedule;
  235. log4cplus::tstring scheduledFilename;
  236. log4cplus::helpers::Time nextRolloverTime;
  237. int maxBackupIndex;
  238. private:
  239. LOG4CPLUS_PRIVATE void init(DailyRollingFileSchedule schedule);
  240. };
  241. } // end namespace log4cplus
  242. #endif // LOG4CPLUS_FILE_APPENDER_HEADER_