source: pacpusframework/trunk/3rd/apache-log4cxx/include/log4cxx/db/odbcappender.h@ 12

Last change on this file since 12 was 2, checked in by sgosseli, 12 years ago

Add 3rd party libraries (log4cxx).

File size: 11.9 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef _LOG4CXX_DB_ODBC_APPENDER_H
19#define _LOG4CXX_DB_ODBC_APPENDER_H
20
21#if defined(_MSC_VER)
22#pragma warning ( push )
23#pragma warning ( disable: 4231 4251 4275 4786 )
24#endif
25
26
27#include <log4cxx/log4cxx.h>
28
29#include <log4cxx/helpers/exception.h>
30#include <log4cxx/appenderskeleton.h>
31#include <log4cxx/spi/loggingevent.h>
32#include <list>
33
34namespace log4cxx
35{
36 namespace db
37 {
38 class LOG4CXX_EXPORT SQLException : public log4cxx::helpers::Exception {
39 public:
40 SQLException(short fHandleType,
41 void* hInput, const char* prolog,
42 log4cxx::helpers::Pool& p);
43 SQLException(const char* msg);
44 SQLException(const SQLException& src);
45 private:
46 const char* formatMessage(short fHandleType,
47 void* hInput, const char* prolog,
48 log4cxx::helpers::Pool& p);
49 };
50
51 /**
52 <p><b>WARNING: This version of ODBCAppender
53 is very likely to be completely replaced in the future. Moreoever,
54 it does not log exceptions.</b> </p>
55
56 The ODBCAppender provides for sending log events to a database.
57
58
59 <p>Each append call adds to an <code>ArrayList</code> buffer. When
60 the buffer is filled each log event is placed in a sql statement
61 (configurable) and executed.
62
63 <b>BufferSize</b>, <b>db URL</b>, <b>User</b>, & <b>Password</b> are
64 configurable options in the standard log4j ways.
65
66 <p>The <code>setSql(String sql)</code> sets the SQL statement to be
67 used for logging -- this statement is sent to a
68 <code>PatternLayout</code> (either created automaticly by the
69 appender or added by the user). Therefore by default all the
70 conversion patterns in <code>PatternLayout</code> can be used
71 inside of the statement. (see the test cases for examples)
72
73 <p>Overriding the {@link #getLogStatement} method allows more
74 explicit control of the statement used for logging.
75
76 <p>For use as a base class:
77
78 <ul>
79
80 <li>Override getConnection() to pass any connection
81 you want. Typically this is used to enable application wide
82 connection pooling.
83
84 <li>Override closeConnection -- if
85 you override getConnection make sure to implement
86 <code>closeConnection</code> to handle the connection you
87 generated. Typically this would return the connection to the
88 pool it came from.
89
90 <li>Override getLogStatement to
91 produce specialized or dynamic statements. The default uses the
92 sql option value.
93
94 </ul>
95 */
96
97 class LOG4CXX_EXPORT ODBCAppender : public AppenderSkeleton
98 {
99 protected:
100 /**
101 * URL of the DB for default connection handling
102 */
103 LogString databaseURL;
104
105 /**
106 * User to connect as for default connection handling
107 */
108 LogString databaseUser;
109
110 /**
111 * User to use for default connection handling
112 */
113 LogString databasePassword;
114
115 typedef void* SQLHDBC;
116 typedef void* SQLHENV;
117 typedef void* SQLHANDLE;
118 typedef short SQLSMALLINT;
119
120 /**
121 * Connection used by default. The connection is opened the first time it
122 * is needed and then held open until the appender is closed (usually at
123 * garbage collection). This behavior is best modified by creating a
124 * sub-class and overriding the <code>getConnection</code> and
125 * <code>closeConnection</code> methods.
126 */
127 SQLHDBC connection;
128 SQLHENV env;
129
130 /**
131 * Stores the string given to the pattern layout for conversion into a SQL
132 * statement, eg: insert into LogTable (Thread, File, Message) values
133 * ("%t", "%F", "%m")
134 *
135 * Be careful of quotes in your messages!
136 *
137 * Also see PatternLayout.
138 */
139 LogString sqlStatement;
140
141 /**
142 * size of LoggingEvent buffer before writing to the database.
143 * Default is 1.
144 */
145 size_t bufferSize;
146
147 /**
148 * ArrayList holding the buffer of Logging Events.
149 */
150 std::list<spi::LoggingEventPtr> buffer;
151
152 public:
153 DECLARE_LOG4CXX_OBJECT(ODBCAppender)
154 BEGIN_LOG4CXX_CAST_MAP()
155 LOG4CXX_CAST_ENTRY(ODBCAppender)
156 LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
157 END_LOG4CXX_CAST_MAP()
158
159 ODBCAppender();
160 virtual ~ODBCAppender();
161
162 /**
163 Set options
164 */
165 virtual void setOption(const LogString& option, const LogString& value);
166
167 /**
168 Activate the specified options.
169 */
170 virtual void activateOptions(log4cxx::helpers::Pool& p);
171
172 /**
173 * Adds the event to the buffer. When full the buffer is flushed.
174 */
175 void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool&);
176
177 /**
178 * By default getLogStatement sends the event to the required Layout object.
179 * The layout will format the given pattern into a workable SQL string.
180 *
181 * Overriding this provides direct access to the LoggingEvent
182 * when constructing the logging statement.
183 *
184 */
185 protected:
186 LogString getLogStatement(const spi::LoggingEventPtr& event,
187 helpers::Pool& p) const;
188
189 /**
190 *
191 * Override this to provide an alertnate method of getting
192 * connections (such as caching). One method to fix this is to open
193 * connections at the start of flushBuffer() and close them at the
194 * end. I use a connection pool outside of ODBCAppender which is
195 * accessed in an override of this method.
196 * */
197 virtual void execute(const LogString& sql,
198 log4cxx::helpers::Pool& p) /*throw(SQLException)*/;
199
200 /**
201 * Override this to return the connection to a pool, or to clean up the
202 * resource.
203 *
204 * The default behavior holds a single connection open until the appender
205 * is closed (typically when garbage collected).
206 */
207 virtual void closeConnection(SQLHDBC con);
208
209 /**
210 * Override this to link with your connection pooling system.
211 *
212 * By default this creates a single connection which is held open
213 * until the object is garbage collected.
214 */
215 virtual SQLHDBC getConnection(log4cxx::helpers::Pool& p) /*throw(SQLException)*/;
216
217 /**
218 * Closes the appender, flushing the buffer first then closing the default
219 * connection if it is open.
220 */
221 public:
222 virtual void close();
223
224 /**
225 * loops through the buffer of LoggingEvents, gets a
226 * sql string from getLogStatement() and sends it to execute().
227 * Errors are sent to the errorHandler.
228 *
229 * If a statement fails the LoggingEvent stays in the buffer!
230 */
231 virtual void flushBuffer(log4cxx::helpers::Pool& p);
232
233 /**
234 * ODBCAppender requires a layout.
235 * */
236 virtual bool requiresLayout() const
237 { return true; }
238
239 /**
240 * Set pre-formated statement eg: insert into LogTable (msg) values ("%m")
241 */
242 void setSql(const LogString& s);
243
244 /**
245 * Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")
246 */
247 inline const LogString& getSql() const
248 { return sqlStatement; }
249
250
251 inline void setUser(const LogString& user)
252 { databaseUser = user; }
253
254
255 inline void setURL(const LogString& url)
256 { databaseURL = url; }
257
258
259 inline void setPassword(const LogString& password)
260 { databasePassword = password; }
261
262
263 inline void setBufferSize(size_t newBufferSize)
264 { bufferSize = newBufferSize; }
265
266 inline const LogString& getUser() const
267 { return databaseUser; }
268
269
270 inline const LogString& getURL() const
271 { return databaseURL; }
272
273
274 inline const LogString& getPassword() const
275 { return databasePassword; }
276
277 inline size_t getBufferSize() const
278 { return bufferSize; }
279 private:
280 ODBCAppender(const ODBCAppender&);
281 ODBCAppender& operator=(const ODBCAppender&);
282 }; // class ODBCAppender
283 LOG4CXX_PTR_DEF(ODBCAppender);
284
285 } // namespace db
286} // namespace log4cxx
287
288#if defined(_MSC_VER)
289#pragma warning ( pop )
290#endif
291
292#endif // _LOG4CXX_DB_ODBC_APPENDER_H
Note: See TracBrowser for help on using the repository browser.