]> granicus.if.org Git - icinga2/commitdiff
Remove the ILogger interface.
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 6 Jun 2013 09:26:30 +0000 (11:26 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 6 Jun 2013 09:26:30 +0000 (11:26 +0200)
Fixes #3860

12 files changed:
lib/base/Makefile.am
lib/base/consolelogger.cpp
lib/base/consolelogger.h
lib/base/filelogger.cpp
lib/base/filelogger.h
lib/base/logger.cpp
lib/base/logger.h
lib/base/streamlogger.cpp
lib/base/streamlogger.h
lib/base/sysloglogger.cpp
lib/base/sysloglogger.h
lib/config/base-type.conf

index b948f66232ae40ceedf73184d811bb3103488699..c7284f9c24e75ae48fcbf73f37f38027d20635c4 100644 (file)
@@ -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 \
index a08c5ea162b14801ca670cc0cbad0e332bbb1ebe..ac3630d6afa63be304caadf7a6db9198f7dcfdaf 100644 (file)
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
  ******************************************************************************/
 
-#include "base/sysloglogger.h"
+#include "base/consolelogger.h"
+#include "base/dynamictype.h"
+#include <iostream>
 
-#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 */
index 5d3860ec4eccc6d177af9f817a7a5687f0e32f1f..973d5fd21b3797ca518504eefc32253c0d87c9b2 100644 (file)
  * 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<SyslogLogger> Ptr;
-       typedef weak_ptr<SyslogLogger> WeakPtr;
+       typedef shared_ptr<ConsoleLogger> Ptr;
+       typedef weak_ptr<ConsoleLogger> 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 */
index a08c5ea162b14801ca670cc0cbad0e332bbb1ebe..f4e15bbe504945cd01885d6ce9df4c226ccfa4a4 100644 (file)
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
  ******************************************************************************/
 
-#include "base/sysloglogger.h"
+#include "base/filelogger.h"
+#include "base/dynamictype.h"
+#include <fstream>
 
-#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
index 5d3860ec4eccc6d177af9f817a7a5687f0e32f1f..9c872b31349edcc32747f233f48bbbb96aeaef3d 100644 (file)
  * 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<SyslogLogger> Ptr;
-       typedef weak_ptr<SyslogLogger> WeakPtr;
+       typedef shared_ptr<FileLogger> Ptr;
+       typedef weak_ptr<FileLogger> WeakPtr;
 
-       explicit SyslogLogger(const Dictionary::Ptr& serializedUpdate);
+       explicit FileLogger(const Dictionary::Ptr& serializedUpdate);
 
-protected:
-       virtual void ProcessLogEntry(const LogEntry& entry);
+private:
+       Attribute<String> m_Path;
 };
 
 }
-#endif /* _WIN32 */
 
-#endif /* SYSLOGLOGGER_H */
+#endif /* FILELOGGER_H */
index 718eac33c7d798e2da48ea0823274f2bab95595a..fa6a799a029ac0c39bceeb6667675b49e61495b5 100644 (file)
@@ -29,7 +29,8 @@
 
 using namespace icinga;
 
-REGISTER_TYPE(Logger);
+std::set<Logger::Ptr> 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<SyslogLogger>();
-#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<StreamLogger>();
-               slogger->OpenFile(path);
-
-               impl = slogger;
-       } else if (type == "console") {
-               impl = boost::make_shared<StreamLogger>(&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::Ptr> 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<Logger>(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();
+//}
index 504bb0edd603c737a86f5283fe2e1b645fbe78a7..dc11f810949cb2f48fa44389be5101c3448d812a 100644 (file)
@@ -23,6 +23,7 @@
 #include "base/i2-base.h"
 #include "base/dynamicobject.h"
 #include "base/logger_fwd.h"
+#include <set>
 
 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<ILogger> Ptr;
-       typedef weak_ptr<ILogger> 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<Logger::Ptr> GetLoggers(void);
+
 protected:
        virtual void Start(void);
+       virtual void Stop(void);
 
 private:
-       Attribute<String> m_Type;
-       Attribute<String> m_Path;
        Attribute<String> m_Severity;
 
        LogSeverity m_MinSeverity;
-       ILogger::Ptr m_Impl;
 
-       static void ForwardLogEntry(const LogEntry& entry);
+       static boost::mutex m_Mutex;
+       static std::set<Logger::Ptr> m_Loggers;
 
        friend void Log(LogSeverity severity, const String& facility,
            const String& message);
index 903965e37b62aa7673f8f9ef3aa0b0b918d81e22..f5821b951579d8876505ac60047368ae565f7230 100644 (file)
@@ -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);
 }
 
index d985ba9595cb6511ffbfd12dd798a7dcce52eac2..1a7766d6032c95213139c26e76178344f9814a1a 100644 (file)
@@ -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<StreamLogger> Ptr;
        typedef weak_ptr<StreamLogger> 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);
index aa96a48dea511fa60a841a4c72e8e4dc049e8bd9..8f56d1c45d55557982d438ecb26ceeef82fcf40f 100644 (file)
  ******************************************************************************/
 
 #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.
  *
index 4f3c9e811aa467eabebbc0ce43da03c817faaf26..5d3860ec4eccc6d177af9f817a7a5687f0e32f1f 100644 (file)
@@ -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<SyslogLogger> Ptr;
        typedef weak_ptr<SyslogLogger> WeakPtr;
 
+       explicit SyslogLogger(const Dictionary::Ptr& serializedUpdate);
+
 protected:
        virtual void ProcessLogEntry(const LogEntry& entry);
 };
index 60b719d2f393791bd96674677cae26e1c3a473a3..4912f7a40306f8adb14d38efb50776568d9b629d 100644 (file)
@@ -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",