factory.h
Go to the documentation of this file.
// -*- C++ -*-
// Module: Log4CPLUS
// File: factory.h
// Created: 2/2002
// Author: Tad E. Smith
//
//
// Copyright 2002-2017 Tad E. Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef LOG4CPLUS_SPI_FACTORY_HEADER_
#define LOG4CPLUS_SPI_FACTORY_HEADER_
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/appender.h>
#include <log4cplus/layout.h>
#include <log4cplus/tstring.h>
#include <log4cplus/spi/filter.h>
#include <log4cplus/spi/objectregistry.h>
#include <memory>
#include <vector>
#include <locale>
namespace log4cplus {
namespace spi {
class LOG4CPLUS_EXPORT BaseFactory {
public:
virtual ~BaseFactory() = 0;
virtual log4cplus::tstring const & getTypeName() const = 0;
};
class LOG4CPLUS_EXPORT AppenderFactory : public BaseFactory {
public:
typedef Appender ProductType;
typedef SharedAppenderPtr ProductPtr;
AppenderFactory();
virtual ~AppenderFactory() = 0;
virtual SharedAppenderPtr createObject(const log4cplus::helpers::Properties& props) = 0;
};
class LOG4CPLUS_EXPORT LayoutFactory : public BaseFactory {
public:
typedef Layout ProductType;
typedef std::unique_ptr<Layout> ProductPtr;
LayoutFactory();
virtual ~LayoutFactory() = 0;
virtual std::unique_ptr<Layout> createObject(const log4cplus::helpers::Properties& props) = 0;
};
class LOG4CPLUS_EXPORT FilterFactory : public BaseFactory {
public:
typedef Filter ProductType;
typedef FilterPtr ProductPtr;
FilterFactory();
virtual ~FilterFactory() = 0;
virtual FilterPtr createObject(const log4cplus::helpers::Properties& props) = 0;
};
class LOG4CPLUS_EXPORT LocaleFactory
: public BaseFactory
{
public:
typedef std::locale ProductType;
typedef std::locale ProductPtr;
LocaleFactory();
virtual ~LocaleFactory() = 0;
virtual ProductPtr createObject (
const log4cplus::helpers::Properties & props) = 0;
};
template<class T>
class LOG4CPLUS_EXPORT FactoryRegistry
: public ObjectRegistryBase
{
public:
typedef T product_type;
virtual ~FactoryRegistry() {
clear();
}
// public methods
bool put(std::unique_ptr<T> object) {
bool putValResult = putVal(object->getTypeName(), object.get());
object.release();
return putValResult;
}
T* get(const log4cplus::tstring& name) const {
return static_cast<T*>(getVal(name));
}
protected:
virtual void deleteObject(void *object) const {
delete static_cast<T*>(object);
}
};
typedef FactoryRegistry<AppenderFactory> AppenderFactoryRegistry;
typedef FactoryRegistry<LayoutFactory> LayoutFactoryRegistry;
typedef FactoryRegistry<FilterFactory> FilterFactoryRegistry;
typedef FactoryRegistry<LocaleFactory> LocaleFactoryRegistry;
LOG4CPLUS_EXPORT AppenderFactoryRegistry& getAppenderFactoryRegistry();
LOG4CPLUS_EXPORT LayoutFactoryRegistry& getLayoutFactoryRegistry();
LOG4CPLUS_EXPORT FilterFactoryRegistry& getFilterFactoryRegistry();
LOG4CPLUS_EXPORT LocaleFactoryRegistry& getLocaleFactoryRegistry();
template <typename ProductFactoryBase>
class LocalFactoryBase
: public ProductFactoryBase
{
public:
LocalFactoryBase (tchar const * n)
: name (n)
{ }
virtual log4cplus::tstring const & getTypeName() const
{
return name;
}
private:
log4cplus::tstring name;
};
template <typename LocalProduct, typename ProductFactoryBase>
class FactoryTempl
: public LocalFactoryBase<ProductFactoryBase>
{
public:
typedef typename ProductFactoryBase::ProductPtr ProductPtr;
FactoryTempl (tchar const * n)
: LocalFactoryBase<ProductFactoryBase> (n)
{ }
virtual ProductPtr createObject (helpers::Properties const & props)
{
return ProductPtr (new LocalProduct (props));
}
};
#define LOG4CPLUS_REG_PRODUCT(reg, productprefix, productname, productns, productfact) \
reg.put ( \
std::unique_ptr<productfact> ( \
new log4cplus::spi::FactoryTempl<productns productname, productfact> ( \
LOG4CPLUS_TEXT(productprefix) \
LOG4CPLUS_TEXT(#productname))))
#define LOG4CPLUS_REG_APPENDER(reg, appendername) \
LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::", appendername, log4cplus::, \
log4cplus::spi::AppenderFactory)
#define LOG4CPLUS_REG_LAYOUT(reg, layoutname) \
LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::", layoutname, log4cplus::, \
log4cplus::spi::LayoutFactory)
#define LOG4CPLUS_REG_FILTER(reg, filtername) \
LOG4CPLUS_REG_PRODUCT (reg, "log4cplus::spi::", filtername, log4cplus::spi::, \
log4cplus::spi::FilterFactory)
#define LOG4CPLUS_REG_LOCALE(reg, name, factory) \
reg.put (std::unique_ptr<log4cplus::spi::LocaleFactory> ( \
new factory (name)))
} // namespace spi
}
#endif // LOG4CPLUS_SPI_FACTORY_HEADER_
appender.h
log4cplus::Appender
Extend this class for implementing your own strategies for printing log statements.
Definition appender.h:139
log4cplus::Layout
This class is used to layout strings sent to an log4cplus::Appender.
Definition layout.h:74
log4cplus::helpers::Properties
Definition property.h:43
log4cplus::spi::AppenderFactory::ProductPtr
SharedAppenderPtr ProductPtr
Definition factory.h:67
log4cplus::spi::AppenderFactory::~AppenderFactory
virtual ~AppenderFactory()=0
log4cplus::spi::AppenderFactory::AppenderFactory
AppenderFactory()
log4cplus::spi::AppenderFactory::createObject
virtual SharedAppenderPtr createObject(const log4cplus::helpers::Properties &props)=0
Create an "Appender" object.
log4cplus::spi::AppenderFactory::ProductType
Appender ProductType
Definition factory.h:66
log4cplus::spi::BaseFactory
This is the base class for all factories.
Definition factory.h:49
log4cplus::spi::BaseFactory::~BaseFactory
virtual ~BaseFactory()=0
log4cplus::spi::BaseFactory::getTypeName
virtual log4cplus::tstring const & getTypeName() const =0
Returns the typename of the objects this factory creates.
log4cplus::spi::FactoryRegistry
This template class is used as a "Factory Registry".
Definition factory.h:151
log4cplus::spi::FactoryRegistry::get
T * get(const log4cplus::tstring &name) const
Used to retrieve an object from the registry.
Definition factory.h:174
log4cplus::spi::FactoryRegistry::put
bool put(std::unique_ptr< T > object)
Used to enter an object into the registry.
Definition factory.h:164
log4cplus::spi::FactoryRegistry::~FactoryRegistry
virtual ~FactoryRegistry()
Definition factory.h:155
log4cplus::spi::FactoryRegistry::product_type
T product_type
Definition factory.h:153
log4cplus::spi::FactoryRegistry::deleteObject
virtual void deleteObject(void *object) const
Deletes object.
Definition factory.h:179
log4cplus::spi::FactoryTempl::FactoryTempl
FactoryTempl(tchar const *n)
Definition factory.h:238
log4cplus::spi::FactoryTempl::ProductPtr
ProductFactoryBase::ProductPtr ProductPtr
Definition factory.h:236
log4cplus::spi::FactoryTempl::createObject
virtual ProductPtr createObject(helpers::Properties const &props)
Definition factory.h:242
log4cplus::spi::FilterFactory::ProductType
Filter ProductType
Definition factory.h:106
log4cplus::spi::FilterFactory::createObject
virtual FilterPtr createObject(const log4cplus::helpers::Properties &props)=0
Create a "Filter" object.
log4cplus::spi::FilterFactory::~FilterFactory
virtual ~FilterFactory()=0
log4cplus::spi::FilterFactory::ProductPtr
FilterPtr ProductPtr
Definition factory.h:107
log4cplus::spi::FilterFactory::FilterFactory
FilterFactory()
log4cplus::spi::Filter
Users should extend this class to implement customized logging event filtering.
Definition filter.h:109
log4cplus::spi::LayoutFactory::ProductType
Layout ProductType
Definition factory.h:86
log4cplus::spi::LayoutFactory::LayoutFactory
LayoutFactory()
log4cplus::spi::LayoutFactory::ProductPtr
std::unique_ptr< Layout > ProductPtr
Definition factory.h:87
log4cplus::spi::LayoutFactory::createObject
virtual std::unique_ptr< Layout > createObject(const log4cplus::helpers::Properties &props)=0
Create a "Layout" object.
log4cplus::spi::LayoutFactory::~LayoutFactory
virtual ~LayoutFactory()=0
log4cplus::spi::LocalFactoryBase::LocalFactoryBase
LocalFactoryBase(tchar const *n)
Definition factory.h:217
log4cplus::spi::LocalFactoryBase::getTypeName
virtual log4cplus::tstring const & getTypeName() const
Definition factory.h:221
log4cplus::spi::LocaleFactory::LocaleFactory
LocaleFactory()
log4cplus::spi::LocaleFactory::~LocaleFactory
virtual ~LocaleFactory()=0
log4cplus::spi::LocaleFactory::createObject
virtual ProductPtr createObject(const log4cplus::helpers::Properties &props)=0
log4cplus::spi::LocaleFactory::ProductPtr
std::locale ProductPtr
Definition factory.h:128
log4cplus::spi::LocaleFactory::ProductType
std::locale ProductType
Definition factory.h:127
log4cplus::spi::ObjectRegistryBase::ObjectRegistryBase
ObjectRegistryBase()
log4cplus::spi::ObjectRegistryBase::putVal
bool putVal(const log4cplus::tstring &name, void *object)
Used to enter an object into the registry.
log4cplus::spi::ObjectRegistryBase::getVal
void * getVal(const log4cplus::tstring &name) const
Used to retrieve an object from the registry.
log4cplus::spi::ObjectRegistryBase::clear
virtual void clear()
Deletes all objects from this registry.
config.hxx
filter.h
This header defines Filter and all of it's subclasses.
layout.h
log4cplus::spi
Definition layout.h:62
log4cplus::spi::FilterFactoryRegistry
FactoryRegistry< FilterFactory > FilterFactoryRegistry
Definition factory.h:187
log4cplus::spi::getLayoutFactoryRegistry
LOG4CPLUS_EXPORT LayoutFactoryRegistry & getLayoutFactoryRegistry()
Returns the "singleton" LayoutFactoryRegistry.
log4cplus::spi::getLocaleFactoryRegistry
LOG4CPLUS_EXPORT LocaleFactoryRegistry & getLocaleFactoryRegistry()
Returns the "singleton" LocaleFactoryRegistry.
log4cplus::spi::getAppenderFactoryRegistry
LOG4CPLUS_EXPORT AppenderFactoryRegistry & getAppenderFactoryRegistry()
Returns the "singleton" AppenderFactoryRegistry.
log4cplus::spi::AppenderFactoryRegistry
FactoryRegistry< AppenderFactory > AppenderFactoryRegistry
Definition factory.h:185
log4cplus::spi::getFilterFactoryRegistry
LOG4CPLUS_EXPORT FilterFactoryRegistry & getFilterFactoryRegistry()
Returns the "singleton" FilterFactoryRegistry.
log4cplus::spi::FilterPtr
helpers::SharedObjectPtr< Filter > FilterPtr
Definition filter.h:77
log4cplus::spi::LocaleFactoryRegistry
FactoryRegistry< LocaleFactory > LocaleFactoryRegistry
Definition factory.h:188
log4cplus::spi::LayoutFactoryRegistry
FactoryRegistry< LayoutFactory > LayoutFactoryRegistry
Definition factory.h:186
log4cplus
Definition appender.h:46
log4cplus::SharedAppenderPtr
helpers::SharedObjectPtr< Appender > SharedAppenderPtr
This is a pointer to an Appender.
Definition appender.h:336
log4cplus::tstring
std::basic_string< tchar > tstring
Definition tstring.h:39
log4cplus::tchar
char tchar
Definition tchar.h:56
objectregistry.h
tstring.h
LOG4CPLUS_EXPORT
#define LOG4CPLUS_EXPORT
Definition win32.h:141