diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-10-06 15:35:04 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-10-06 15:35:04 +0100 |
commit | b54e25755d60a103614bee1398fc3eb020025659 (patch) | |
tree | 7626af4e0ef3b26b323e9d95631a34f897640550 | |
parent | Compat fixes since adhocutil updates to master (diff) | |
download | project2-b54e25755d60a103614bee1398fc3eb020025659.tar.bz2 project2-b54e25755d60a103614bee1398fc3eb020025659.tar.xz project2-b54e25755d60a103614bee1398fc3eb020025659.zip |
Add support for logging to the IceBox Logger instance
-rw-r--r-- | project2/common/loggerFactory.h | 3 | ||||
-rw-r--r-- | project2/ice/iceboxDaemon.cpp | 3 | ||||
-rw-r--r-- | project2/ice/iceboxLogger.cpp | 58 | ||||
-rw-r--r-- | project2/ice/iceboxLogger.h | 37 |
4 files changed, 99 insertions, 2 deletions
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<LoggerType> 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 <thread> #include <appInstance.h> #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<OptionsSource>(new IceBoxOptionsSource(ic), "admin", __FILE__, __LINE__); + AdHoc::PluginManager::getDefault()->add<LogDriverFactory>(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 <loggerFactory.impl.h> +#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 <arg> (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<IceBoxLogDriver>::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 <Ice/Logger.h> +#include <loggerFactory.h> +#include <options.h> + +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<IceBoxLogDriver> +{ + 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<IceBoxLogDriver>::create() const; + +#endif + |