SimpleFileChannel.h 4.4 KB

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