log4cplus 2.0.8
factory.h
Go to the documentation of this file.
1// -*- C++ -*-
2// Module: Log4CPLUS
3// File: factory.h
4// Created: 2/2002
5// Author: Tad E. Smith
6//
7//
8// Copyright 2002-2017 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
24#ifndef LOG4CPLUS_SPI_FACTORY_HEADER_
25#define LOG4CPLUS_SPI_FACTORY_HEADER_
26
27#include <log4cplus/config.hxx>
28
29#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
30#pragma once
31#endif
32
33#include <log4cplus/appender.h>
34#include <log4cplus/layout.h>
35#include <log4cplus/tstring.h>
38#include <memory>
39#include <vector>
40#include <locale>
41
42
43namespace log4cplus {
44 namespace spi {
45
50 public:
51 virtual ~BaseFactory() = 0;
52
56 virtual log4cplus::tstring const & getTypeName() const = 0;
57 };
58
59
65 public:
68
70 virtual ~AppenderFactory() = 0;
71
76 };
77
78
79
85 public:
87 typedef std::unique_ptr<Layout> ProductPtr;
88
90 virtual ~LayoutFactory() = 0;
91
95 virtual std::unique_ptr<Layout> createObject(const log4cplus::helpers::Properties& props) = 0;
96 };
97
98
99
105 public:
108
110 virtual ~FilterFactory() = 0;
111
116 };
117
118
124 : public BaseFactory
125 {
126 public:
127 typedef std::locale ProductType;
128 typedef std::locale ProductPtr;
129
131 virtual ~LocaleFactory() = 0;
132
135 const log4cplus::helpers::Properties & props) = 0;
136 };
137
138
148 template<class T>
150 : public ObjectRegistryBase
151 {
152 public:
153 typedef T product_type;
154
156 clear();
157 }
158
159 // public methods
164 bool put(std::unique_ptr<T> object) {
165 bool putValResult = putVal(object->getTypeName(), object.get());
166 object.release();
167 return putValResult;
168 }
169
174 T* get(const log4cplus::tstring& name) const {
175 return static_cast<T*>(getVal(name));
176 }
177
178 protected:
179 virtual void deleteObject(void *object) const {
180 delete static_cast<T*>(object);
181 }
182 };
183
184
189
190
195
200
205
210
211
212 template <typename ProductFactoryBase>
214 : public ProductFactoryBase
215 {
216 public:
218 : name (n)
219 { }
220
221 virtual log4cplus::tstring const & getTypeName() const
222 {
223 return name;
224 }
225
226 private:
228 };
229
230
231 template <typename LocalProduct, typename ProductFactoryBase>
233 : public LocalFactoryBase<ProductFactoryBase>
234 {
235 public:
236 typedef typename ProductFactoryBase::ProductPtr ProductPtr;
237
238 FactoryTempl (tchar const * n)
239 : LocalFactoryBase<ProductFactoryBase> (n)
240 { }
241
243 {
244 return ProductPtr (new LocalProduct (props));
245 }
246 };
247
248
249 #define LOG4CPLUS_REG_PRODUCT(reg, productprefix, productname, productns, productfact) \
250 reg.put ( \
251 std::unique_ptr<productfact> ( \
252 new log4cplus::spi::FactoryTempl<productns productname, productfact> ( \
253 LOG4CPLUS_TEXT(productprefix) \
254 LOG4CPLUS_TEXT(#productname))))
255
256 #define LOG4CPLUS_REG_APPENDER(reg, appendername) \
257 LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::", appendername, log4cplus::, \
258 log4cplus::spi::AppenderFactory)
259
260 #define LOG4CPLUS_REG_LAYOUT(reg, layoutname) \
261 LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::", layoutname, log4cplus::, \
262 log4cplus::spi::LayoutFactory)
263
264 #define LOG4CPLUS_REG_FILTER(reg, filtername) \
265 LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::spi::", filtername, log4cplus::spi::, \
266 log4cplus::spi::FilterFactory)
267
268 #define LOG4CPLUS_REG_LOCALE(reg, name, factory) \
269 reg.put (std::unique_ptr<log4cplus::spi::LocaleFactory> ( \
270 new factory (name)))
271 } // namespace spi
272}
273
274
275#endif // LOG4CPLUS_SPI_FACTORY_HEADER_
Extend this class for implementing your own strategies for printing log statements.
Definition: appender.h:139
This class is used to layout strings sent to an log4cplus::Appender.
Definition: layout.h:74
This abstract class defines the "Factory" interface to create "Appender" objects.
Definition: factory.h:64
SharedAppenderPtr ProductPtr
Definition: factory.h:67
virtual SharedAppenderPtr createObject(const log4cplus::helpers::Properties &props)=0
Create an "Appender" object.
This is the base class for all factories.
Definition: factory.h:49
virtual log4cplus::tstring const & getTypeName() const =0
Returns the typename of the objects this factory creates.
This template class is used as a "Factory Registry".
Definition: factory.h:151
T * get(const log4cplus::tstring &name) const
Used to retrieve an object from the registry.
Definition: factory.h:174
bool put(std::unique_ptr< T > object)
Used to enter an object into the registry.
Definition: factory.h:164
virtual void deleteObject(void *object) const
Deletes object.
Definition: factory.h:179
FactoryTempl(tchar const *n)
Definition: factory.h:238
ProductFactoryBase::ProductPtr ProductPtr
Definition: factory.h:236
virtual ProductPtr createObject(helpers::Properties const &props)
Definition: factory.h:242
This abstract class defines the "Factory" interface to create "Appender" objects.
Definition: factory.h:104
virtual FilterPtr createObject(const log4cplus::helpers::Properties &props)=0
Create a "Filter" object.
Users should extend this class to implement customized logging event filtering.
Definition: filter.h:109
This abstract class defines the "Factory" interface to create "Layout" objects.
Definition: factory.h:84
std::unique_ptr< Layout > ProductPtr
Definition: factory.h:87
virtual std::unique_ptr< Layout > createObject(const log4cplus::helpers::Properties &props)=0
Create a "Layout" object.
LocalFactoryBase(tchar const *n)
Definition: factory.h:217
virtual log4cplus::tstring const & getTypeName() const
Definition: factory.h:221
This abstract class defines the "Factory" interface to create std::locale instances.
Definition: factory.h:125
virtual ProductPtr createObject(const log4cplus::helpers::Properties &props)=0
This is the base class used to implement the functionality required by the ObjectRegistry template cl...
This header defines Filter and all of it's subclasses.
FactoryRegistry< FilterFactory > FilterFactoryRegistry
Definition: factory.h:187
LOG4CPLUS_EXPORT LayoutFactoryRegistry & getLayoutFactoryRegistry()
Returns the "singleton" LayoutFactoryRegistry.
LOG4CPLUS_EXPORT LocaleFactoryRegistry & getLocaleFactoryRegistry()
Returns the "singleton" LocaleFactoryRegistry.
LOG4CPLUS_EXPORT AppenderFactoryRegistry & getAppenderFactoryRegistry()
Returns the "singleton" AppenderFactoryRegistry.
FactoryRegistry< AppenderFactory > AppenderFactoryRegistry
Definition: factory.h:185
LOG4CPLUS_EXPORT FilterFactoryRegistry & getFilterFactoryRegistry()
Returns the "singleton" FilterFactoryRegistry.
FactoryRegistry< LocaleFactory > LocaleFactoryRegistry
Definition: factory.h:188
FactoryRegistry< LayoutFactory > LayoutFactoryRegistry
Definition: factory.h:186
std::basic_string< tchar > tstring
Definition: tstring.h:39
char tchar
Definition: tchar.h:56
#define LOG4CPLUS_EXPORT
Definition: win32.h:141