log4cplus 2.0.8
deviceappender.hxx
Go to the documentation of this file.
1// Copyright (C) 2009-2017, Vaclav Haisman. All rights reserved.
2//
3// Redistribution and use in source and binary forms, with or without modifica-
4// tion, are permitted provided that the following conditions are met:
5//
6// 1. Redistributions of source code must retain the above copyright notice,
7// this list of conditions and the following disclaimer.
8//
9// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
16// APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
18// DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
19// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24#ifndef LOG4CPLUS_BOOST_DEVICEAPPENDER_HXX
25#define LOG4CPLUS_BOOST_DEVICEAPPENDER_HXX
26
27#include <log4cplus/config.hxx>
28
29#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
30#pragma once
31#endif
32
33#include <boost/utility/enable_if.hpp>
34#include <boost/type_traits/is_same.hpp>
35#include <boost/mpl/not.hpp>
36#include <boost/iostreams/operations.hpp>
37#include <boost/shared_ptr.hpp>
38#include <log4cplus/appender.h>
39
40
41namespace log4cplus
42{
43
44
45namespace device_appender_detail
46{
47
48
49template <typename T>
51{
52 typedef T & device_type;
53
54 static
57 {
58 return x;
59 }
60};
61
62
63template <typename T>
64struct device_type_traits<boost::shared_ptr<T> >
65{
66 typedef boost::shared_ptr<T> device_type;
67
68 static
69 T &
70 unwrap (device_type const & ptr)
71 {
72 return *ptr;
73 }
74};
75
76
77} // namespace device_appender_detail
78
79
80template <typename Device>
82 : public Appender
83{
84public:
87
88 template <typename D>
89 DeviceAppender (D & d, bool close_device = true)
90 : device (d)
91 , close_flag (close_device)
92 { }
93
94 template <typename D>
95 DeviceAppender (boost::shared_ptr<D> const & d, bool close_device = true)
96 : device (d)
97 , close_flag (close_device)
98 { }
99
100 template <typename D>
101 DeviceAppender (D & d, const helpers::Properties & props)
102 : Appender (props)
103 , device (d)
104 {
105 if (props.exists (LOG4CPLUS_TEXT ("CloseDevice")))
106 close_flag = true;
107 else
108 close_flag = false;
109 }
110
111 template <typename D>
112 DeviceAppender (boost::shared_ptr<D> const & d,
113 const helpers::Properties & props)
114 : Appender (props)
115 , device (d)
116 {
117 if (props.exists (LOG4CPLUS_TEXT ("CloseDevice")))
118 close_flag = true;
119 else
120 close_flag = false;
121 }
122
123 virtual
125 { }
126
127 virtual
128 void
130 {
131 if (close_flag)
132 boost::iostreams::close (device_traits::unwrap (device));
133 }
134
135protected:
136 virtual
137 void
139 {
140 tstring & str = formatEvent (event);
142 str.c_str (), str.size ());
143 }
144
147
148private:
150 DeviceAppender & operator = (DeviceAppender const &);
151};
152
153
154template <typename T>
155inline
157make_device_appender (T & d, bool close_device = true)
158{
159 SharedAppenderPtr app (new DeviceAppender<T> (d, close_device));
160 return app;
161}
162
163
164template <typename T>
165inline
168{
169 SharedAppenderPtr app (new DeviceAppender<T> (d, props));
170 return app;
171}
172
173
174template <typename T>
175inline
177make_device_appender_sp (boost::shared_ptr<T> const & p,
178 bool close_device = true)
179{
181 new DeviceAppender<boost::shared_ptr<T> > (p, close_device));
182 return app;
183}
184
185
186template <typename T>
187inline
189make_device_appender_sp (boost::shared_ptr<T> const & p,
190 const helpers::Properties & props)
191{
193 new DeviceAppender<boost::shared_ptr<T> > (p, props));
194 return app;
195}
196
197
198} // namespace log4cplus
199
200
201#endif // LOG4CPLUS_BOOST_DEVICEAPPENDER_HXX
Extend this class for implementing your own strategies for printing log statements.
Definition: appender.h:139
tstring & formatEvent(const log4cplus::spi::InternalLoggingEvent &event) const
DeviceAppender(D &d, const helpers::Properties &props)
device_traits::device_type device_type
DeviceAppender(boost::shared_ptr< D > const &d, bool close_device=true)
virtual void append(log4cplus::spi::InternalLoggingEvent const &event)
Subclasses of Appender should implement this method to perform actual logging.
device_appender_detail::device_type_traits< Device > device_traits
virtual void close()
Release any resources allocated within the appender such as file handles, network connections,...
DeviceAppender(D &d, bool close_device=true)
DeviceAppender(boost::shared_ptr< D > const &d, const helpers::Properties &props)
bool exists(const log4cplus::tstring &key) const
Tests to see if key can be found in this map.
The internal representation of logging events.
Definition: loggingevent.h:51
#define LOG4CPLUS_TEXT(STRING)
Definition: clogger.h:72
LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock, const SocketBuffer &buffer)
helpers::SharedObjectPtr< Appender > SharedAppenderPtr
This is a pointer to an Appender.
Definition: appender.h:336
SharedAppenderPtr make_device_appender_sp(boost::shared_ptr< T > const &p, bool close_device=true)
std::basic_string< tchar > tstring
Definition: tstring.h:39
SharedAppenderPtr make_device_appender(T &d, bool close_device=true)