From b54e25755d60a103614bee1398fc3eb020025659 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 6 Oct 2015 15:35:04 +0100 Subject: Add support for logging to the IceBox Logger instance --- project2/common/loggerFactory.h | 3 ++- project2/ice/iceboxDaemon.cpp | 3 ++- project2/ice/iceboxLogger.cpp | 58 +++++++++++++++++++++++++++++++++++++++++ project2/ice/iceboxLogger.h | 37 ++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 project2/ice/iceboxLogger.cpp create mode 100644 project2/ice/iceboxLogger.h diff --git a/project2/common/loggerFactory.h b/project2/common/loggerFactory.h index 056f5e3..e620e0c 100644 --- a/project2/common/loggerFactory.h +++ b/project2/common/loggerFactory.h @@ -30,7 +30,8 @@ class LogDriverFactoryImpl : public LogDriverFactory, public LifeCycle { int loggerLevel() const override; const int & level; - private: + + protected: mutable boost::intrusive_ptr instance; }; diff --git a/project2/ice/iceboxDaemon.cpp b/project2/ice/iceboxDaemon.cpp index ccc69f7..8aa3582 100644 --- a/project2/ice/iceboxDaemon.cpp +++ b/project2/ice/iceboxDaemon.cpp @@ -8,6 +8,7 @@ #include #include #include "iceboxOptionsSource.h" +#include "iceboxLogger.h" class IceBoxDaemon : public IceBox::Service, public AppInstance { public: @@ -64,12 +65,12 @@ DECLARE_OPTIONS(IceBoxDaemon, "Project2 IceBox options") "Delay between occurrences of component periodic calls (default 60s)") END_OPTIONS(IceBoxDaemon); - extern "C" { IceBox::Service * createProject2Daemon(Ice::CommunicatorPtr ic) { AdHoc::PluginManager::getDefault()->add(new IceBoxOptionsSource(ic), "admin", __FILE__, __LINE__); + AdHoc::PluginManager::getDefault()->add(new IceBoxLoggerFactory(ic->getLogger()), "icebox", __FILE__, __LINE__); return new IceBoxDaemon(); } } diff --git a/project2/ice/iceboxLogger.cpp b/project2/ice/iceboxLogger.cpp new file mode 100644 index 0000000..7e1cdd0 --- /dev/null +++ b/project2/ice/iceboxLogger.cpp @@ -0,0 +1,58 @@ +#include +#include "iceboxLogger.h" + +IceBoxLogDriver::IceBoxLogDriver(Ice::LoggerPtr l) : + logger(l) +{ +} + +void +IceBoxLogDriver::message(int priority, const char * msg) const +{ + if (priority <= level) { + if (priority >= LOG_DEBUG) { + logger->trace(debugCategory, msg); + } + else if (priority >= LOG_NOTICE) { + logger->print(msg); + } + else if (priority >= LOG_WARNING) { + logger->warning(msg); + } + else { + logger->error(msg); + } + } +} + +IceBoxLoggerFactory::IceBoxLoggerFactory(Ice::LoggerPtr l) : + logger(l) +{ +} + +IceBoxLogDriver * +IceBoxLoggerFactory::create() const +{ + if (!instance) { + instance = new IceBoxLogDriver(logger); + } + return instance.get(); +} + +DECLARE_OPTIONS(IceBoxLogDriver, "IceBox log options") +("icebox.log.level", Options::value(&level, LOG_WARNING), + "Log to IceBox with level (default LOG_WARNING)")("s") +("icebox.log.debugcategory", Options::value(&debugCategory, "Project2"), + "Category to use when writing to trace (default Project2)") +END_OPTIONS(IceBoxLogDriver); + +int IceBoxLogDriver::level; +std::string IceBoxLogDriver::debugCategory; + +template<> +IceBoxLogDriver * +LogDriverFactoryImpl::create() const +{ + return nullptr; +} + diff --git a/project2/ice/iceboxLogger.h b/project2/ice/iceboxLogger.h new file mode 100644 index 0000000..d306419 --- /dev/null +++ b/project2/ice/iceboxLogger.h @@ -0,0 +1,37 @@ +#ifndef ICEBOXLOGDRIVER_H +#define ICEBOXLOGDRIVER_H + +#include +#include +#include + +class IceBoxLogDriver : public LogDriverBase { + public: + IceBoxLogDriver(Ice::LoggerPtr l); + + void message(int priority, const char * msg) const override; + + INITOPTIONS; + static int level; + private: + Ice::LoggerPtr logger; + static std::string debugCategory; +}; + +class IceBoxLoggerFactory : public LogDriverFactoryImpl +{ + public: + IceBoxLoggerFactory(Ice::LoggerPtr l); + + IceBoxLogDriver * create() const override; + + private: + const Ice::LoggerPtr logger; +}; + +// Specialised as IceBoxLogDriver can't be defualt constructed. +template<> +IceBoxLogDriver * LogDriverFactoryImpl::create() const; + +#endif + -- cgit v1.2.3