AppendersFactory.cpp
Go to the documentation of this file.
00001 #include <log4cpp/AppendersFactory.hh>
00002 #include <stdexcept>
00003 
00004 namespace log4cpp
00005 {
00006    static AppendersFactory* appenders_factory_ = 0;
00007 
00008    std::auto_ptr<Appender> create_file_appender(const FactoryParams&);
00009    std::auto_ptr<Appender> create_roll_file_appender(const FactoryParams&);
00010    std::auto_ptr<Appender> create_idsa_appender(const FactoryParams&);
00011    std::auto_ptr<Appender> create_nt_event_log_appender(const FactoryParams&);
00012    std::auto_ptr<Appender> create_remote_syslog_appender(const FactoryParams&);
00013    std::auto_ptr<Appender> create_syslog_appender(const FactoryParams&);
00014    std::auto_ptr<Appender> create_win32_debug_appender(const FactoryParams&);
00015    std::auto_ptr<Appender> create_abort_appender(const FactoryParams&);
00016    std::auto_ptr<Appender> create_smtp_appender(const FactoryParams&);
00017 
00018    AppendersFactory& AppendersFactory::getInstance()
00019    {
00020       if (!appenders_factory_)
00021       {
00022          std::auto_ptr<AppendersFactory> af(new AppendersFactory);
00023          
00024          af->registerCreator("file", &create_file_appender);
00025          af->registerCreator("roll file", &create_roll_file_appender);
00026          af->registerCreator("remote syslog", &create_remote_syslog_appender);
00027          af->registerCreator("abort", &create_abort_appender);
00028 
00029 #if defined(LOG4CPP_HAVE_LIBIDSA)
00030          af->registerCreator("idsa", &create_idsa_appender);
00031 #endif
00032 
00033 #if defined(LOG4CPP_HAVE_SYSLOG)
00034          af->registerCreator("syslog", &create_syslog_appender);
00035 #endif
00036 
00037 #if defined(WIN32)
00038          af->registerCreator("win32 debug", &create_win32_debug_appender);
00039          af->registerCreator("nt event log", &create_nt_event_log_appender);
00040 #endif
00041 
00042 #if defined(LOG4CPP_HAVE_BOOST)
00043 #include <boost/version.hpp>
00044 #if BOOST_VERSION >= 103500
00045          af->registerCreator("smtp", &create_smtp_appender);
00046 #endif // LOG4CPP_HAVE_BOOST
00047 #endif // BOOST_VERSION >= 103500
00048 
00049          appenders_factory_ = af.release();
00050       }
00051 
00052       return *appenders_factory_;
00053    }
00054 
00055    void AppendersFactory::registerCreator(const std::string& class_name, create_function_t create_function)
00056    {
00057       const_iterator i = creators_.find(class_name);
00058       if (i != creators_.end())
00059          throw std::invalid_argument("Appender creator for type name '" + class_name + "' allready registered");
00060 
00061       creators_[class_name] = create_function;
00062    }
00063 
00064    std::auto_ptr<Appender> AppendersFactory::create(const std::string& class_name, const params_t& params)
00065    {
00066       const_iterator i = creators_.find(class_name);
00067       if (i == creators_.end())
00068          throw std::invalid_argument("There is no appender with type name '" + class_name + "'");
00069 
00070       return (*i->second)(params);
00071    }
00072 
00073    bool AppendersFactory::registered(const std::string& class_name) const
00074    {
00075       return creators_.end() != creators_.find(class_name);
00076    }
00077 }


log4cpp
Author(s): Stephen Roderick
autogenerated on Mon Oct 6 2014 03:13:14