From: Gunnar Beutner Date: Thu, 6 Jun 2013 09:26:30 +0000 (+0200) Subject: Remove the ILogger interface. X-Git-Tag: v0.0.2~96 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4fadd3bfa01d545a87292ee53558c3222152b37e;p=icinga2 Remove the ILogger interface. Fixes #3860 --- diff --git a/lib/base/Makefile.am b/lib/base/Makefile.am index b948f6623..c7284f9c2 100644 --- a/lib/base/Makefile.am +++ b/lib/base/Makefile.am @@ -13,6 +13,8 @@ libbase_la_SOURCES = \ attribute.h \ bufferedstream.cpp \ bufferedstream.h \ + consolelogger.cpp \ + consolelogger.h \ convert.cpp \ convert.h \ dictionary.cpp \ @@ -25,6 +27,8 @@ libbase_la_SOURCES = \ exception.h \ fifo.cpp \ fifo.h \ + filelogger.cpp \ + filelogger.h \ i2-base.h \ logger.cpp \ logger.h \ diff --git a/lib/base/consolelogger.cpp b/lib/base/consolelogger.cpp index a08c5ea16..ac3630d6a 100644 --- a/lib/base/consolelogger.cpp +++ b/lib/base/consolelogger.cpp @@ -17,42 +17,19 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#include "base/sysloglogger.h" +#include "base/consolelogger.h" +#include "base/dynamictype.h" +#include -#ifndef _WIN32 using namespace icinga; -/** - * Constructor for the SyslogLogger class. - */ -SyslogLogger::SyslogLogger(const Dictionary::Ptr& serializedUpdate) - : Logger(serializedUpdate) -{ } +REGISTER_TYPE(ConsoleLogger); /** - * Processes a log entry and outputs it to syslog. - * - * @param entry The log entry. + * Constructor for the ConsoleLogger class. */ -void SyslogLogger::ProcessLogEntry(const LogEntry& entry) +ConsoleLogger::ConsoleLogger(const Dictionary::Ptr& serializedUpdate) + : StreamLogger(serializedUpdate) { - int severity; - switch (entry.Severity) { - case LogDebug: - severity = LOG_DEBUG; - break; - case LogWarning: - severity = LOG_WARNING; - break; - case LogCritical: - severity = LOG_CRIT; - break; - case LogInformation: - default: - severity = LOG_INFO; - break; - } - - syslog(severity | LOG_USER, "%s", entry.Message.CStr()); + BindStream(&std::cout, false); } -#endif /* _WIN32 */ diff --git a/lib/base/consolelogger.h b/lib/base/consolelogger.h index 5d3860ec4..973d5fd21 100644 --- a/lib/base/consolelogger.h +++ b/lib/base/consolelogger.h @@ -17,34 +17,29 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#ifndef SYSLOGLOGGER_H -#define SYSLOGLOGGER_H +#ifndef CONSOLELOGGER_H +#define CONSOLELOGGER_H #include "base/i2-base.h" -#include "base/logger.h" +#include "base/streamlogger.h" -#ifndef _WIN32 namespace icinga { /** - * A logger that logs to syslog. + * A logger that logs to the console. * * @ingroup base */ -class I2_BASE_API SyslogLogger : public Logger +class I2_BASE_API ConsoleLogger : public StreamLogger { public: - typedef shared_ptr Ptr; - typedef weak_ptr WeakPtr; + typedef shared_ptr Ptr; + typedef weak_ptr WeakPtr; - explicit SyslogLogger(const Dictionary::Ptr& serializedUpdate); - -protected: - virtual void ProcessLogEntry(const LogEntry& entry); + explicit ConsoleLogger(const Dictionary::Ptr& serializedUpdate); }; } -#endif /* _WIN32 */ -#endif /* SYSLOGLOGGER_H */ +#endif /* CONSOLELOGGER_H */ diff --git a/lib/base/filelogger.cpp b/lib/base/filelogger.cpp index a08c5ea16..f4e15bbe5 100644 --- a/lib/base/filelogger.cpp +++ b/lib/base/filelogger.cpp @@ -17,42 +17,35 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#include "base/sysloglogger.h" +#include "base/filelogger.h" +#include "base/dynamictype.h" +#include -#ifndef _WIN32 using namespace icinga; -/** - * Constructor for the SyslogLogger class. - */ -SyslogLogger::SyslogLogger(const Dictionary::Ptr& serializedUpdate) - : Logger(serializedUpdate) -{ } +REGISTER_TYPE(FileLogger); /** - * Processes a log entry and outputs it to syslog. - * - * @param entry The log entry. + * Constructor for the FileLogger class. */ -void SyslogLogger::ProcessLogEntry(const LogEntry& entry) +FileLogger::FileLogger(const Dictionary::Ptr& serializedUpdate) + : StreamLogger(serializedUpdate) { - int severity; - switch (entry.Severity) { - case LogDebug: - severity = LOG_DEBUG; - break; - case LogWarning: - severity = LOG_WARNING; - break; - case LogCritical: - severity = LOG_CRIT; - break; - case LogInformation: - default: - severity = LOG_INFO; - break; + RegisterAttribute("path", Attribute_Config, &m_Path); + + std::ofstream *stream = new std::ofstream(); + + String path = m_Path; + + try { + stream->open(path.CStr(), std::fstream::out | std::fstream::trunc); + + if (!stream->good()) + BOOST_THROW_EXCEPTION(std::runtime_error("Could not open logfile '" + path + "'")); + } catch (...) { + delete stream; + throw; } - syslog(severity | LOG_USER, "%s", entry.Message.CStr()); -} -#endif /* _WIN32 */ + BindStream(stream, true); +} \ No newline at end of file diff --git a/lib/base/filelogger.h b/lib/base/filelogger.h index 5d3860ec4..9c872b313 100644 --- a/lib/base/filelogger.h +++ b/lib/base/filelogger.h @@ -17,34 +17,32 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#ifndef SYSLOGLOGGER_H -#define SYSLOGLOGGER_H +#ifndef FILELOGGER_H +#define FILELOGGER_H #include "base/i2-base.h" -#include "base/logger.h" +#include "base/streamlogger.h" -#ifndef _WIN32 namespace icinga { /** - * A logger that logs to syslog. + * A logger that logs to a file. * * @ingroup base */ -class I2_BASE_API SyslogLogger : public Logger +class I2_BASE_API FileLogger : public StreamLogger { public: - typedef shared_ptr Ptr; - typedef weak_ptr WeakPtr; + typedef shared_ptr Ptr; + typedef weak_ptr WeakPtr; - explicit SyslogLogger(const Dictionary::Ptr& serializedUpdate); + explicit FileLogger(const Dictionary::Ptr& serializedUpdate); -protected: - virtual void ProcessLogEntry(const LogEntry& entry); +private: + Attribute m_Path; }; } -#endif /* _WIN32 */ -#endif /* SYSLOGLOGGER_H */ +#endif /* FILELOGGER_H */ diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp index 718eac33c..fa6a799a0 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -29,7 +29,8 @@ using namespace icinga; -REGISTER_TYPE(Logger); +std::set Logger::m_Loggers; +boost::mutex Logger::m_Mutex; /** * Constructor for the Logger class. @@ -39,8 +40,6 @@ REGISTER_TYPE(Logger); Logger::Logger(const Dictionary::Ptr& serializedUpdate) : DynamicObject(serializedUpdate) { - RegisterAttribute("type", Attribute_Config, &m_Type); - RegisterAttribute("path", Attribute_Config, &m_Path); RegisterAttribute("severity", Attribute_Config, &m_Severity); if (!IsLocal()) @@ -49,36 +48,20 @@ Logger::Logger(const Dictionary::Ptr& serializedUpdate) void Logger::Start(void) { - String type = m_Type; - if (type.IsEmpty()) - BOOST_THROW_EXCEPTION(std::runtime_error("Logger objects must have a 'type' property.")); - - ILogger::Ptr impl; - - if (type == "syslog") { -#ifndef _WIN32 - impl = boost::make_shared(); -#else /* _WIN32 */ - BOOST_THROW_EXCEPTION(std::invalid_argument("Syslog is not supported on Windows.")); -#endif /* _WIN32 */ - } else if (type == "file") { - String path = m_Path; - if (path.IsEmpty()) - BOOST_THROW_EXCEPTION(std::invalid_argument("'log' object of type 'file' must have a 'path' property")); - - StreamLogger::Ptr slogger = boost::make_shared(); - slogger->OpenFile(path); - - impl = slogger; - } else if (type == "console") { - impl = boost::make_shared(&std::cout); - } else { - BOOST_THROW_EXCEPTION(std::runtime_error("Unknown log type: " + type)); - } + boost::mutex::scoped_lock(m_Mutex); + m_Loggers.insert(GetSelf()); +} - impl->m_Config = GetSelf(); - m_Impl = impl; +void Logger::Stop(void) +{ + boost::mutex::scoped_lock(m_Mutex); + m_Loggers.erase(GetSelf()); +} +std::set Logger::GetLoggers(void) +{ + boost::mutex::scoped_lock(m_Mutex); + return m_Loggers; } /** @@ -97,40 +80,14 @@ void icinga::Log(LogSeverity severity, const String& facility, entry.Facility = facility; entry.Message = message; - Logger::ForwardLogEntry(entry); -} - -/** - * Retrieves the minimum severity for this logger. - * - * @returns The minimum severity. - */ -LogSeverity Logger::GetMinSeverity(void) const -{ - String severity = m_Severity; - if (severity.IsEmpty()) - return LogInformation; - else - return Logger::StringToSeverity(severity); -} - -/** - * Forwards a log entry to the registered loggers. - * - * @param entry The log entry. - */ -void Logger::ForwardLogEntry(const LogEntry& entry) -{ bool processed = false; - BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Logger")) { - Logger::Ptr logger = dynamic_pointer_cast(object); - + BOOST_FOREACH(const Logger::Ptr& logger, Logger::GetLoggers()) { { ObjectLock llock(logger); if (entry.Severity >= logger->GetMinSeverity()) - logger->m_Impl->ProcessLogEntry(entry); + logger->ProcessLogEntry(entry); } processed = true; @@ -150,6 +107,20 @@ void Logger::ForwardLogEntry(const LogEntry& entry) } } +/** + * Retrieves the minimum severity for this logger. + * + * @returns The minimum severity. + */ +LogSeverity Logger::GetMinSeverity(void) const +{ + String severity = m_Severity; + if (severity.IsEmpty()) + return LogInformation; + else + return Logger::StringToSeverity(severity); +} + /** * Converts a severity enum value to a string. * @@ -190,12 +161,12 @@ LogSeverity Logger::StringToSeverity(const String& severity) BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity: " + severity)); } -/** - * Retrieves the configuration object that belongs to this logger. - * - * @returns The configuration object. - */ -DynamicObject::Ptr ILogger::GetConfig(void) const -{ - return m_Config.lock(); -} +///** +// * Retrieves the configuration object that belongs to this logger. +// * +// * @returns The configuration object. +// */ +//DynamicObject::Ptr ILogger::GetConfig(void) const +//{ +// return m_Config.lock(); +//} diff --git a/lib/base/logger.h b/lib/base/logger.h index 504bb0edd..dc11f8109 100644 --- a/lib/base/logger.h +++ b/lib/base/logger.h @@ -23,6 +23,7 @@ #include "base/i2-base.h" #include "base/dynamicobject.h" #include "base/logger_fwd.h" +#include namespace icinga { @@ -40,35 +41,7 @@ struct LogEntry { }; /** - * Base class for all loggers. - * - * @ingroup base - */ -class I2_BASE_API ILogger : public Object -{ -public: - typedef shared_ptr Ptr; - typedef weak_ptr WeakPtr; - - /** - * Processes the log entry and writes it to the log that is - * represented by this ILogger object. - * - * @param entry The log entry that is to be processed. - */ - virtual void ProcessLogEntry(const LogEntry& entry) = 0; - -protected: - DynamicObject::Ptr GetConfig(void) const; - -private: - DynamicObject::WeakPtr m_Config; - - friend class Logger; -}; - -/** - * A log provider. Can be instantiated from the config. + * A log provider. * * @ingroup base */ @@ -85,18 +58,27 @@ public: LogSeverity GetMinSeverity(void) const; + /** + * Processes the log entry and writes it to the log that is + * represented by this ILogger object. + * + * @param entry The log entry that is to be processed. + */ + virtual void ProcessLogEntry(const LogEntry& entry) = 0; + + static std::set GetLoggers(void); + protected: virtual void Start(void); + virtual void Stop(void); private: - Attribute m_Type; - Attribute m_Path; Attribute m_Severity; LogSeverity m_MinSeverity; - ILogger::Ptr m_Impl; - static void ForwardLogEntry(const LogEntry& entry); + static boost::mutex m_Mutex; + static std::set m_Loggers; friend void Log(LogSeverity severity, const String& facility, const String& message); diff --git a/lib/base/streamlogger.cpp b/lib/base/streamlogger.cpp index 903965e37..f5821b951 100644 --- a/lib/base/streamlogger.cpp +++ b/lib/base/streamlogger.cpp @@ -31,17 +31,8 @@ boost::mutex StreamLogger::m_Mutex; /** * Constructor for the StreamLogger class. */ -StreamLogger::StreamLogger(void) - : ILogger(), m_Stream(NULL), m_OwnsStream(false), m_Tty(false) -{ } - -/** - * Constructor for the StreamLogger class. - * - * @param stream The stream. - */ -StreamLogger::StreamLogger(std::ostream *stream) - : ILogger(), m_Stream(stream), m_OwnsStream(false), m_Tty(IsTty(*stream)) +StreamLogger::StreamLogger(const Dictionary::Ptr& serializedUpdate) + : Logger(serializedUpdate), m_Stream(NULL), m_OwnsStream(false), m_Tty(false) { } /** @@ -53,25 +44,13 @@ StreamLogger::~StreamLogger(void) delete m_Stream; } -void StreamLogger::OpenFile(const String& filename) +void StreamLogger::BindStream(std::ostream *stream, bool ownsStream) { - std::ofstream *stream = new std::ofstream(); - - try { - stream->open(filename.CStr(), std::fstream::out | std::fstream::trunc); - - if (!stream->good()) - BOOST_THROW_EXCEPTION(std::runtime_error("Could not open logfile '" + filename + "'")); - } catch (...) { - delete stream; - throw; - } - ObjectLock olock(this); m_Stream = stream; - m_OwnsStream = true; - m_Tty = false; + m_OwnsStream = ownsStream; + m_Tty = IsTty(*stream); } /** @@ -117,8 +96,6 @@ void StreamLogger::ProcessLogEntry(std::ostream& stream, bool tty, const LogEntr */ void StreamLogger::ProcessLogEntry(const LogEntry& entry) { - ObjectLock olock(this); - ProcessLogEntry(*m_Stream, m_Tty, entry); } diff --git a/lib/base/streamlogger.h b/lib/base/streamlogger.h index d985ba959..1a7766d60 100644 --- a/lib/base/streamlogger.h +++ b/lib/base/streamlogger.h @@ -32,17 +32,16 @@ namespace icinga * * @ingroup base */ -class I2_BASE_API StreamLogger : public ILogger +class I2_BASE_API StreamLogger : public Logger { public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; - StreamLogger(void); - explicit StreamLogger(std::ostream *stream); + StreamLogger(const Dictionary::Ptr& serializedUpdate); ~StreamLogger(void); - void OpenFile(const String& filename); + void BindStream(std::ostream *stream, bool ownsStream); static void ProcessLogEntry(std::ostream& stream, bool tty, const LogEntry& entry); static bool IsTty(std::ostream& stream); diff --git a/lib/base/sysloglogger.cpp b/lib/base/sysloglogger.cpp index aa96a48de..8f56d1c45 100644 --- a/lib/base/sysloglogger.cpp +++ b/lib/base/sysloglogger.cpp @@ -18,10 +18,20 @@ ******************************************************************************/ #include "base/sysloglogger.h" +#include "base/dynamictype.h" #ifndef _WIN32 using namespace icinga; +REGISTER_TYPE(SyslogLogger); + +/** + * Constructor for the SyslogLogger class. + */ +SyslogLogger::SyslogLogger(const Dictionary::Ptr& serializedUpdate) + : Logger(serializedUpdate) +{ } + /** * Processes a log entry and outputs it to syslog. * diff --git a/lib/base/sysloglogger.h b/lib/base/sysloglogger.h index 4f3c9e811..5d3860ec4 100644 --- a/lib/base/sysloglogger.h +++ b/lib/base/sysloglogger.h @@ -32,12 +32,14 @@ namespace icinga * * @ingroup base */ -class I2_BASE_API SyslogLogger : public ILogger +class I2_BASE_API SyslogLogger : public Logger { public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; + explicit SyslogLogger(const Dictionary::Ptr& serializedUpdate); + protected: virtual void ProcessLogEntry(const LogEntry& entry); }; diff --git a/lib/config/base-type.conf b/lib/config/base-type.conf index 60b719d2f..4912f7a40 100644 --- a/lib/config/base-type.conf +++ b/lib/config/base-type.conf @@ -33,11 +33,20 @@ type DynamicObject { } type Logger { - %attribute string "type", - %attribute string "path", %attribute string "severity" } +type ConsoleLogger inherits Logger { +} + +type FileLogger inherits Logger { + %require "path", + %attribute string "path" +} + +type SyslogLogger inherits Logger { +} + type Script { %require "language", %attribute string "language",