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
27namespace Poco {
28
29
30class LogFile;
31
32
33class 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{
80public:
81 SimpleFileChannel();
82 /// Creates the FileChannel.
83
84 SimpleFileChannel(const std::string& path);
85 /// Creates the FileChannel for a file with the given path.
86
87 void open();
88 /// Opens the FileChannel and creates the log file if necessary.
89
90 void close();
91 /// Closes the FileChannel.
92
93 void log(const Message& msg);
94 /// Logs the given message to the file.
95
96 void setProperty(const std::string& name, const std::string& value);
97 /// Sets the property with the given name.
98 ///
99 /// The following properties are supported:
100 /// * path: The primary log file's path.
101 /// * secondaryPath: The secondary log file's path.
102 /// * rotation: The log file's rotation mode. See the
103 /// SimpleFileChannel class for details.
104 /// * flush: Specifies whether messages are immediately
105 /// flushed to the log file. See the SimpleFileChannel
106 /// class for details.
107
108 std::string getProperty(const std::string& name) const;
109 /// Returns the value of the property with the given name.
110 /// See setProperty() for a description of the supported
111 /// properties.
112
113 Timestamp creationDate() const;
114 /// Returns the log file's creation date.
115
116 UInt64 size() const;
117 /// Returns the log file's current size in bytes.
118
119 const std::string& path() const;
120 /// Returns the log file's primary path.
121
122 const std::string& secondaryPath() const;
123 /// Returns the log file's secondary path.
124
125 static const std::string PROP_PATH;
126 static const std::string PROP_SECONDARYPATH;
127 static const std::string PROP_ROTATION;
128 static const std::string PROP_FLUSH;
129
130protected:
131 ~SimpleFileChannel();
132 void setRotation(const std::string& rotation);
133 void setFlush(const std::string& flush);
134 void rotate();
135
136private:
137 std::string _path;
138 std::string _secondaryPath;
139 std::string _rotation;
140 UInt64 _limit;
141 bool _flush;
142 LogFile* _pFile;
143 FastMutex _mutex;
144};
145
146
147} // namespace Poco
148
149
150#endif // Foundation_SimpleFileChannel_INCLUDED
151