Apache log4cxx  Version 0.13.0
odbcappender.h
Go to the documentation of this file.
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 
32 #include <list>
33 
34 namespace log4cxx
35 {
36 namespace db
37 {
38 class LOG4CXX_EXPORT SQLException : public log4cxx::helpers::Exception
39 {
40  public:
41  SQLException(short fHandleType,
42  void* hInput, const char* prolog,
44  SQLException(const char* msg);
45  SQLException(const SQLException& src);
46  private:
47  const char* formatMessage(short fHandleType,
48  void* hInput, const char* prolog,
50 };
51 
98 class LOG4CXX_EXPORT ODBCAppender : public AppenderSkeleton
99 {
100  protected:
105 
110 
115 
116  typedef void* SQLHDBC;
117  typedef void* SQLHENV;
118  typedef void* SQLHANDLE;
119  typedef short SQLSMALLINT;
120 
128  SQLHDBC connection;
129  SQLHENV env;
130 
141 
146  size_t bufferSize;
147 
151  std::list<spi::LoggingEventPtr> buffer;
152 
153  public:
159 
160  ODBCAppender();
161  virtual ~ODBCAppender();
162 
166  virtual void setOption(const LogString& option, const LogString& value);
167 
171  virtual void activateOptions(log4cxx::helpers::Pool& p);
172 
176  void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool&);
177 
186  protected:
187  LogString getLogStatement(const spi::LoggingEventPtr& event,
188  helpers::Pool& p) const;
189 
198  virtual void execute(const LogString& sql,
199  log4cxx::helpers::Pool& p) /*throw(SQLException)*/;
200 
208  virtual void closeConnection(SQLHDBC con);
209 
216  virtual SQLHDBC getConnection(log4cxx::helpers::Pool& p) /*throw(SQLException)*/;
217 
222  public:
223  virtual void close();
224 
232  virtual void flushBuffer(log4cxx::helpers::Pool& p);
233 
237  virtual bool requiresLayout() const
238  {
239  return true;
240  }
241 
245  void setSql(const LogString& s);
246 
250  inline const LogString& getSql() const
251  {
252  return sqlStatement;
253  }
254 
255 
256  inline void setUser(const LogString& user)
257  {
258  databaseUser = user;
259  }
260 
261 
262  inline void setURL(const LogString& url)
263  {
264  databaseURL = url;
265  }
266 
267 
268  inline void setPassword(const LogString& password)
269  {
270  databasePassword = password;
271  }
272 
273 
274  inline void setBufferSize(size_t newBufferSize)
275  {
276  bufferSize = newBufferSize;
277  }
278 
279  inline const LogString& getUser() const
280  {
281  return databaseUser;
282  }
283 
284 
285  inline const LogString& getURL() const
286  {
287  return databaseURL;
288  }
289 
290 
291  inline const LogString& getPassword() const
292  {
293  return databasePassword;
294  }
295 
296  inline size_t getBufferSize() const
297  {
298  return bufferSize;
299  }
300  private:
301  ODBCAppender(const ODBCAppender&);
302  ODBCAppender& operator=(const ODBCAppender&);
303 #if LOG4CXX_WCHAR_T_API || LOG4CXX_LOGCHAR_IS_WCHAR_T || defined(WIN32) || defined(_WIN32)
304  static void encode(wchar_t** dest, const LogString& src,
306 #endif
307  static void encode(unsigned short** dest, const LogString& src,
309 }; // class ODBCAppender
311 
312 } // namespace db
313 } // namespace log4cxx
314 
315 #if defined(_MSC_VER)
316  #pragma warning ( pop )
317 #endif
318 
319 #endif // _LOG4CXX_DB_ODBC_APPENDER_H
#define LOG4CXX_CAST_ENTRY(Interface)
Definition: object.h:153
std::list< spi::LoggingEventPtr > buffer
ArrayList holding the buffer of Logging Events.
Definition: odbcappender.h:151
SQLHENV env
Definition: odbcappender.h:129
const LogString & getSql() const
Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")
Definition: odbcappender.h:250
void * SQLHDBC
Definition: odbcappender.h:116
#define END_LOG4CXX_CAST_MAP()
Definition: object.h:147
Implementation base class for all appenders.
Definition: appenderskeleton.h:43
std::shared_ptr< LoggingEvent > LoggingEventPtr
Definition: appender.h:37
#define BEGIN_LOG4CXX_CAST_MAP()
Definition: object.h:141
const LogString & getURL() const
Definition: odbcappender.h:285
Definition: odbcappender.h:38
void setBufferSize(size_t newBufferSize)
Definition: odbcappender.h:274
LogString databaseUser
User to connect as for default connection handling.
Definition: odbcappender.h:109
SQLHDBC connection
Connection used by default.
Definition: odbcappender.h:128
void setURL(const LogString &url)
Definition: odbcappender.h:262
void setUser(const LogString &user)
Definition: odbcappender.h:256
#define DECLARE_LOG4CXX_OBJECT(object)
Definition: object.h:39
LogString sqlStatement
Stores the string given to the pattern layout for conversion into a SQL statement, eg: insert into LogTable (Thread, File, Message) values ("%t", "%F", "%m")
Definition: odbcappender.h:140
const LogString & getUser() const
Definition: odbcappender.h:279
void setPassword(const LogString &password)
Definition: odbcappender.h:268
short SQLSMALLINT
Definition: odbcappender.h:119
const LogString & getPassword() const
Definition: odbcappender.h:291
Definition: pool.h:32
LOG4CXX_PTR_DEF(ODBCAppender)
#define LOG4CXX_CAST_ENTRY_CHAIN(Interface)
Definition: object.h:159
Definition: odbcappender.h:98
LogString databaseURL
URL of the DB for default connection handling.
Definition: odbcappender.h:104
size_t getBufferSize() const
Definition: odbcappender.h:296
size_t bufferSize
size of LoggingEvent buffer before writing to the database.
Definition: odbcappender.h:146
Definition: appender.h:32
std::basic_string< logchar > LogString
Definition: logstring.h:66
The class Exception and its subclasses indicate conditions that a reasonable application might want t...
Definition: exception.h:37
void * SQLHANDLE
Definition: odbcappender.h:118
LogString databasePassword
User to use for default connection handling.
Definition: odbcappender.h:114
void * SQLHENV
Definition: odbcappender.h:117