/work/dcmtk-install/include/dcmtk/oflog/appender.h
Line | Count | Source |
1 | | // -*- C++ -*- |
2 | | // Module: Log4CPLUS |
3 | | // File: appender.h |
4 | | // Created: 6/2001 |
5 | | // Author: Tad E. Smith |
6 | | // |
7 | | // |
8 | | // Copyright 2001-2010 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 | | |
22 | | /** @file */ |
23 | | |
24 | | #ifndef DCMTK_LOG4CPLUS_APPENDER_HEADER_ |
25 | | #define DCMTK_LOG4CPLUS_APPENDER_HEADER_ |
26 | | |
27 | | #include "dcmtk/oflog/config.h" |
28 | | |
29 | | #if defined (DCMTK_LOG4CPLUS_HAVE_PRAGMA_ONCE) |
30 | | #pragma once |
31 | | #endif |
32 | | |
33 | | #include "dcmtk/ofstd/ofmem.h" |
34 | | #include "dcmtk/oflog/layout.h" |
35 | | #include "dcmtk/oflog/loglevel.h" |
36 | | #include "dcmtk/oflog/tstring.h" |
37 | | #include "dcmtk/oflog/helpers/pointer.h" |
38 | | #include "dcmtk/oflog/spi/filter.h" |
39 | | #include "dcmtk/oflog/helpers/lockfile.h" |
40 | | |
41 | | #include <memory> |
42 | | |
43 | | |
44 | | namespace dcmtk { |
45 | | namespace log4cplus { |
46 | | |
47 | | |
48 | | namespace helpers |
49 | | { |
50 | | |
51 | | class Properties; |
52 | | |
53 | | } |
54 | | |
55 | | |
56 | | /** |
57 | | * This class is used to "handle" errors encountered in an {@link |
58 | | * log4cplus::Appender}. |
59 | | */ |
60 | | class DCMTK_LOG4CPLUS_EXPORT ErrorHandler |
61 | | { |
62 | | public: |
63 | | ErrorHandler (); |
64 | | virtual ~ErrorHandler() = 0; |
65 | | virtual void error(const log4cplus::tstring& err) = 0; |
66 | | virtual void reset() = 0; |
67 | | }; |
68 | | |
69 | | |
70 | | class DCMTK_LOG4CPLUS_EXPORT OnlyOnceErrorHandler |
71 | | : public ErrorHandler |
72 | | { |
73 | | public: |
74 | | // Ctor |
75 | | OnlyOnceErrorHandler(); |
76 | | virtual ~OnlyOnceErrorHandler (); |
77 | | virtual void error(const log4cplus::tstring& err); |
78 | | virtual void reset(); |
79 | | |
80 | | private: |
81 | | bool firstTime; |
82 | | }; |
83 | | |
84 | | |
85 | | /** |
86 | | * Extend this class for implementing your own strategies for printing log |
87 | | * statements. |
88 | | * |
89 | | * <h3>Properties</h3> |
90 | | * <dl> |
91 | | * <dt><tt>UseLockFile</tt></dt> |
92 | | * <dd>Set this property to <tt>true</tt> if you want your output |
93 | | * through this appender to be synchronized between multiple |
94 | | * processes. When this property is set to true then log4cplus |
95 | | * uses OS specific facilities (e.g., <code>lockf()</code>) to |
96 | | * provide inter-process locking. With the exception of |
97 | | * FileAppender and its derived classes, it is also necessary to |
98 | | * provide path to a lock file using the <tt>LockFile</tt> |
99 | | * property. |
100 | | * \sa FileAppender |
101 | | * </dd> |
102 | | * |
103 | | * <dt><tt>LockFile</tt></dt> |
104 | | * <dd>This property specifies lock file, file used for |
105 | | * inter-process synchronization of log file access. The property |
106 | | * is only used when <tt>UseLockFile</tt> is set to true. Then it |
107 | | * is mandatory. |
108 | | * \sa FileAppender |
109 | | * </dd> |
110 | | * </dl> |
111 | | */ |
112 | | class DCMTK_LOG4CPLUS_EXPORT Appender |
113 | | : public virtual log4cplus::helpers::SharedObject |
114 | | { |
115 | | public: |
116 | | // Ctor |
117 | | Appender(); |
118 | | Appender(const log4cplus::helpers::Properties & properties); |
119 | | |
120 | | // Dtor |
121 | | virtual ~Appender(); |
122 | | |
123 | | void destructorImpl(); |
124 | | |
125 | | // Methods |
126 | | /** |
127 | | * Release any resources allocated within the appender such as file |
128 | | * handles, network connections, etc. |
129 | | * |
130 | | * It is a programming error to append to a closed appender. |
131 | | */ |
132 | | virtual void close() = 0; |
133 | | |
134 | | /** |
135 | | * This method performs threshold checks and invokes filters before |
136 | | * delegating actual logging to the subclasses specific {@link |
137 | | * #append} method. |
138 | | */ |
139 | | void doAppend(const log4cplus::spi::InternalLoggingEvent& event); |
140 | | |
141 | | /** |
142 | | * Get the name of this appender. The name uniquely identifies the |
143 | | * appender. |
144 | | */ |
145 | | virtual log4cplus::tstring getName(); |
146 | | |
147 | | /** |
148 | | * Set the name of this appender. The name is used by other |
149 | | * components to identify this appender. |
150 | | */ |
151 | | virtual void setName(const log4cplus::tstring& name); |
152 | | |
153 | | /** |
154 | | * Set the {@link ErrorHandler} for this Appender. |
155 | | */ |
156 | | virtual void setErrorHandler(OFunique_ptr<ErrorHandler> eh); |
157 | | |
158 | | /** |
159 | | * Return the currently set {@link ErrorHandler} for this |
160 | | * Appender. |
161 | | */ |
162 | | virtual ErrorHandler* getErrorHandler(); |
163 | | |
164 | | /** |
165 | | * Set the layout for this appender. Note that some appenders have |
166 | | * their own (fixed) layouts or do not use one. For example, the |
167 | | * SocketAppender ignores the layout set here. |
168 | | */ |
169 | | virtual void setLayout(OFunique_ptr<Layout> layout); |
170 | | |
171 | | /** |
172 | | * Returns the layout of this appender. The value may be NULL. |
173 | | * |
174 | | * This class owns the returned pointer. |
175 | | */ |
176 | | virtual Layout* getLayout(); |
177 | | |
178 | | /** |
179 | | * Set the filter chain on this Appender. |
180 | | */ |
181 | | void setFilter(log4cplus::spi::FilterPtr f) { filter = f; } |
182 | | |
183 | | /** |
184 | | * Get the filter chain on this Appender. |
185 | | */ |
186 | 0 | log4cplus::spi::FilterPtr getFilter() const { return filter; } |
187 | | |
188 | | /** |
189 | | * Returns this appenders threshold LogLevel. See the {@link |
190 | | * #setThreshold} method for the meaning of this option. |
191 | | */ |
192 | 0 | LogLevel getThreshold() const { return threshold; } |
193 | | |
194 | | /** |
195 | | * Set the threshold LogLevel. All log events with lower LogLevel |
196 | | * than the threshold LogLevel are ignored by the appender. |
197 | | * |
198 | | * In configuration files this option is specified by setting the |
199 | | * value of the <b>Threshold</b> option to a LogLevel |
200 | | * string, such as "DEBUG", "INFO" and so on. |
201 | | */ |
202 | 0 | void setThreshold(LogLevel th) { threshold = th; } |
203 | | |
204 | | /** |
205 | | * Check whether the message LogLevel is below the appender's |
206 | | * threshold. If there is no threshold set, then the return value is |
207 | | * always <code>true</code>. |
208 | | */ |
209 | | bool isAsSevereAsThreshold(LogLevel ll) const { |
210 | | return ((ll != NOT_SET_LOG_LEVEL) && (ll >= threshold)); |
211 | | } |
212 | | |
213 | | protected: |
214 | | // Methods |
215 | | /** |
216 | | * Subclasses of <code>Appender</code> should implement this |
217 | | * method to perform actual logging. |
218 | | * @see doAppend method. |
219 | | */ |
220 | | virtual void append(const log4cplus::spi::InternalLoggingEvent& event) = 0; |
221 | | |
222 | | tstring & formatEvent (const log4cplus::spi::InternalLoggingEvent& event) const; |
223 | | |
224 | | // Data |
225 | | /** The layout variable does not need to be set if the appender |
226 | | * implementation has its own layout. */ |
227 | | OFunique_ptr<Layout> layout; |
228 | | |
229 | | /** Appenders are named. */ |
230 | | log4cplus::tstring name; |
231 | | |
232 | | /** There is no LogLevel threshold filtering by default. */ |
233 | | LogLevel threshold; |
234 | | |
235 | | /** The first filter in the filter chain. Set to <code>null</code> |
236 | | * initially. */ |
237 | | log4cplus::spi::FilterPtr filter; |
238 | | |
239 | | /** It is assumed and enforced that errorHandler is never null. */ |
240 | | OFunique_ptr<ErrorHandler> errorHandler; |
241 | | |
242 | | //! Optional system wide synchronization lock. |
243 | | OFunique_ptr<helpers::LockFile> lockFile; |
244 | | |
245 | | //! Use lock file for inter-process synchronization of access |
246 | | //! to log file. |
247 | | bool useLockFile; |
248 | | |
249 | | /** Is this appender closed? */ |
250 | | bool closed; |
251 | | }; |
252 | | |
253 | | /** This is a pointer to an Appender. */ |
254 | | typedef helpers::SharedObjectPtr<Appender> SharedAppenderPtr; |
255 | | |
256 | | } // end namespace log4cplus |
257 | | } // end namespace dcmtk |
258 | | |
259 | | #endif // DCMTK_LOG4CPLUS_APPENDER_HEADER_ |
260 | | |