SimpleFileChannel.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // SimpleFileChannel.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/SimpleFileChannel.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: SimpleFileChannel
  9. //
  10. // Definition of the SimpleFileChannel class.
  11. //
  12. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_SimpleFileChannel_INCLUDED
  18. #define Foundation_SimpleFileChannel_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 Foundation_API SimpleFileChannel: public Channel
  26. /// A Channel that writes to a file. This class only
  27. /// supports simple log file rotation.
  28. ///
  29. /// For more features, see the FileChannel class.
  30. ///
  31. /// Only the message's text is written, followed
  32. /// by a newline.
  33. ///
  34. /// Chain this channel to a FormattingChannel with an
  35. /// appropriate Formatter to control what is in the text.
  36. ///
  37. /// Log file rotation based on log file size is supported.
  38. ///
  39. /// If rotation is enabled, the SimpleFileChannel will
  40. /// alternate between two log files. If the size of
  41. /// the primary log file exceeds a specified limit,
  42. /// the secondary log file will be used, and vice
  43. /// versa.
  44. ///
  45. /// Log rotation is configured with the "rotation"
  46. /// property, which supports the following values:
  47. /// * never: no log rotation
  48. /// * <n>: the file is rotated when its size exceeds
  49. /// <n> bytes.
  50. /// * <n> K: the file is rotated when its size exceeds
  51. /// <n> Kilobytes.
  52. /// * <n> M: the file is rotated when its size exceeds
  53. /// <n> Megabytes.
  54. ///
  55. /// The path of the (primary) log file can be specified with
  56. /// the "path" property. Optionally, the path of the secondary
  57. /// log file can be specified with the "secondaryPath" property.
  58. ///
  59. /// If no secondary path is specified, the secondary path will
  60. /// default to <primaryPath>.1.
  61. ///
  62. /// The flush property specifies whether each log message is flushed
  63. /// immediately to the log file (which may hurt application performance,
  64. /// but ensures that everything is in the log in case of a system crash),
  65. // or whether it's allowed to stay in the system's file buffer for some time.
  66. /// Valid values are:
  67. ///
  68. /// * true: Every essages is immediately flushed to the log file (default).
  69. /// * false: Messages are not immediately flushed to the log file.
  70. ///
  71. {
  72. public:
  73. SimpleFileChannel();
  74. /// Creates the FileChannel.
  75. SimpleFileChannel(const std::string& path);
  76. /// Creates the FileChannel for a file with the given path.
  77. void open();
  78. /// Opens the FileChannel and creates the log file if necessary.
  79. void close();
  80. /// Closes the FileChannel.
  81. void log(const Message& msg);
  82. /// Logs the given message to the file.
  83. void setProperty(const std::string& name, const std::string& value);
  84. /// Sets the property with the given name.
  85. ///
  86. /// The following properties are supported:
  87. /// * path: The primary log file's path.
  88. /// * secondaryPath: The secondary log file's path.
  89. /// * rotation: The log file's rotation mode. See the
  90. /// SimpleFileChannel class for details.
  91. /// * flush: Specifies whether messages are immediately
  92. /// flushed to the log file. See the SimpleFileChannel
  93. /// class for details.
  94. std::string getProperty(const std::string& name) const;
  95. /// Returns the value of the property with the given name.
  96. /// See setProperty() for a description of the supported
  97. /// properties.
  98. Timestamp creationDate() const;
  99. /// Returns the log file's creation date.
  100. UInt64 size() const;
  101. /// Returns the log file's current size in bytes.
  102. const std::string& path() const;
  103. /// Returns the log file's primary path.
  104. const std::string& secondaryPath() const;
  105. /// Returns the log file's secondary path.
  106. static const std::string PROP_PATH;
  107. static const std::string PROP_SECONDARYPATH;
  108. static const std::string PROP_ROTATION;
  109. static const std::string PROP_FLUSH;
  110. protected:
  111. ~SimpleFileChannel();
  112. void setRotation(const std::string& rotation);
  113. void setFlush(const std::string& flush);
  114. void rotate();
  115. private:
  116. std::string _path;
  117. std::string _secondaryPath;
  118. std::string _rotation;
  119. UInt64 _limit;
  120. bool _flush;
  121. LogFile* _pFile;
  122. FastMutex _mutex;
  123. };
  124. } // namespace Poco
  125. #endif // Foundation_SimpleFileChannel_INCLUDED