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