1 // Module: Log4CPLUS
2 // File: appender.h
3 // Created: 6/2001
4 // Author: Tad E. Smith
5 //
6 //
7 // Copyright (C) Tad E. Smith All rights reserved.
8 //
9 // This software is published under the terms of the Apache Software
10 // License version 1.1, a copy of which has been included with this
11 // distribution in the LICENSE.APL file.
12 //
13
14 /** @file */
15
16 #ifndef _LOG4CPLUS_APPENDER_HEADER_
17 #define _LOG4CPLUS_APPENDER_HEADER_
18
19 #include <log4cplus/config.h>
20 #include <log4cplus/layout.h>
21 #include <log4cplus/loglevel.h>
22 #include <log4cplus/tstring.h>
23 #include <log4cplus/helpers/logloguser.h>
24 #include <log4cplus/helpers/pointer.h>
25 #include <log4cplus/helpers/property.h>
26 #include <log4cplus/spi/filter.h>
27
28 #include <memory>
29
30
31 namespace log4cplus {
32
33 /**
34 * This class is used to "handle" errors encountered in an {@link
35 * #Appender}.
36 */
37 class LOG4CPLUS_EXPORT ErrorHandler {
38 public:
39 virtual ~ErrorHandler();
40 virtual void error(const log4cplus::tstring& err) = 0;
41 };
42
43
44
45 class LOG4CPLUS_EXPORT OnlyOnceErrorHandler : public ErrorHandler,
46 protected log4cplus::helpers::LogLogUser
47 {
48 public:
49 // Ctor
50 OnlyOnceErrorHandler() : firstTime(true){}
51
52 virtual void error(const log4cplus::tstring& err);
53
54 private:
55 bool firstTime;
56 };
57
58
59 /**
60 * Extend this class for implementing your own strategies for printing log
61 * statements.
62 */
63 class LOG4CPLUS_EXPORT Appender : public log4cplus::helpers::SharedObject,
64 protected log4cplus::helpers::LogLogUser
65
66 {
67 public:
68 // Ctor
69 Appender();
70 Appender(const log4cplus::helpers::Properties properties);
71
72 // Dtor
73 virtual ~Appender(){};
74
75 void destructorImpl();
76
77 // Methods
78 /**
79 * Release any resources allocated within the appender such as file
80 * handles, network connections, etc.
81 * <p>
82 * It is a programming error to append to a closed appender.
83 */
84 virtual void close() = 0;
85
86 /**
87 * This method performs threshold checks and invokes filters before
88 * delegating actual logging to the subclasses specific {@link
89 * AppenderSkeleton#append} method.
90 */
91 void doAppend(const log4cplus::spi::InternalLoggingEvent& event);
92
93 /**
94 * Get the name of this appender. The name uniquely identifies the
95 * appender.
96 */
97 virtual log4cplus::tstring getName();
98
99 /**
100 * Set the name of this appender. The name is used by other
101 * components to identify this appender.
102 */
103 virtual void setName(const log4cplus::tstring& name);
104
105 /**
106 * Set the {@link ErrorHandler} for this Appender.
107 */
108 virtual void setErrorHandler(std::auto_ptr<ErrorHandler> eh);
109
110 /**
111 * Return the currently set {@link ErrorHandler} for this
112 * Appender.
113 */
114 virtual ErrorHandler* getErrorHandler();
115
116 /**
117 * Set the layout for this appender. Note that some appenders have
118 * their own (fixed) layouts or do not use one. For example, the
119 * SocketAppender ignores the layout set here.
120 */
121 virtual void setLayout(std::auto_ptr<Layout> layout);
122
123 /**
124 * Returns the layout of this appender. The value may be NULL.
125 * <p>
126 * This class owns the returned pointer.
127 */
128 virtual Layout* getLayout();
129
130 /**
131 * Set the filter chain on this Appender.
132 */
133 void setFilter(log4cplus::spi::FilterPtr f) { filter = f; }
134
135 /**
136 * Get the filter chain on this Appender.
137 */
138 log4cplus::spi::FilterPtr getFilter() const { return filter; }
139
140 /**
141 * Returns this appenders threshold LogLevel. See the {@link
142 * #setThreshold} method for the meaning of this option.
143 */
144 LogLevel getThreshold() const { return threshold; }
145
146 /**
147 * Set the threshold LogLevel. All log events with lower LogLevel
148 * than the threshold LogLevel are ignored by the appender.
149 * <p>
150 * In configuration files this option is specified by setting the
151 * value of the <b>Threshold</b> option to a LogLevel
152 * string, such as "DEBUG", "INFO" and so on.
153 */
154 void setThreshold(LogLevel th) { threshold = th; }
155
156 /**
157 * Check whether the message LogLevel is below the appender's
158 * threshold. If there is no threshold set, then the return value is
159 * always <code>true</code>.
160 */
161 bool isAsSevereAsThreshold(LogLevel ll) const {
162 return ((ll != NOT_SET_LOG_LEVEL) && (ll >= threshold));
163 }
164
165 protected:
166 // Methods
167 /**
168 * Subclasses of <code>AppenderSkeleton</code> should implement this
169 * method to perform actual logging. See also {@link #doAppend
170 * AppenderSkeleton.doAppend} method.
171 */
172 virtual void append(const log4cplus::spi::InternalLoggingEvent& event) = 0;
173
174 // Data
175 /** The layout variable does not need to be set if the appender
176 * implementation has its own layout. */
177 std::auto_ptr<Layout> layout;
178
179 /** Appenders are named. */
180 log4cplus::tstring name;
181
182 /** There is no LogLevel threshold filtering by default. */
183 LogLevel threshold;
184
185 /** The first filter in the filter chain. Set to <code>null</code>
186 * initially. */
187 log4cplus::spi::FilterPtr filter;
188
189 /** It is assumed and enforced that errorHandler is never null. */
190 std::auto_ptr<ErrorHandler> errorHandler;
191
192 /** Is this appender closed? */
193 bool closed;
194 };
195
196 /** @var This is a pointer to an Appender. */
197 typedef helpers::SharedObjectPtr<Appender> SharedAppenderPtr;
198
199 } // end namespace log4cplus
200
201 #endif // _LOG4CPLUS_APPENDER_HEADER_
syntax highlighted by Code2HTML, v. 0.9.1