]> granicus.if.org Git - icinga2/commitdiff
Implemented syslog logger class.
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 10 Jul 2012 11:31:17 +0000 (13:31 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 10 Jul 2012 11:31:17 +0000 (13:31 +0200)
base/Makefile.am
base/application.cpp
base/application.h
base/consolelogger.h
base/i2-base.h
base/sysloglogger.cpp [new file with mode: 0644]
base/sysloglogger.h [new file with mode: 0644]
base/unix.h
icinga/icingaapplication.cpp

index 51111332a5fd96cddf87d4051fc9e082bb9f6739..d93337638506c980352f28dc4bd46e2467f6f09c 100644 (file)
@@ -34,6 +34,8 @@ libbase_la_SOURCES =  \
        ringbuffer.h \
        socket.cpp \
        socket.h \
+       sysloglogger.cpp \
+       sysloglogger.h \
        tcpclient.cpp \
        tcpclient.h \
        tcpserver.cpp \
index 352cbf2e8ef3ce592a5b94fef62a4e76799a65b3..7d2d4c636ea52bbade5d6e18f3e0a8e158929cf6 100644 (file)
@@ -140,12 +140,16 @@ Component::Ptr Application::LoadComponent(const string& path,
 
 #ifdef _WIN32
        HMODULE hModule = LoadLibrary(path.c_str());
+
+       if (hModule == NULL)
+               throw Win32Exception("LoadLibrary('" + path + "') failed", GetLastError());
 #else /* _WIN32 */
        lt_dlhandle hModule = lt_dlopen(path.c_str());
-#endif /* _WIN32 */
 
-       if (hModule == NULL)
-               throw runtime_error("Could not load module");
+       if (hModule == NULL) {
+               throw runtime_error("Could not load module '" + path + "': " +  lt_dlerror());
+       }
+#endif /* _WIN32 */
 
 #ifdef _WIN32
        pCreateComponent = (CreateComponentFunction)GetProcAddress(hModule,
@@ -215,11 +219,11 @@ Component::Ptr Application::GetComponent(const string& name) const
 }
 
 /**
- * Retrieves the directory the application's binary is contained in.
+ * Retrieves the full path of the executable.
  *
- * @returns The directory.
+ * @returns The path.
  */
-string Application::GetExeDirectory(void) const
+string Application::GetExePath(void) const
 {
        static string result;
 
@@ -268,18 +272,16 @@ string Application::GetExeDirectory(void) const
        if (realpath(executablePath.c_str(), buffer) == NULL)
                throw PosixException("realpath failed", errno);
 
-       executablePath = buffer;
+       result = buffer;
 #else /* _WIN32 */
        char FullExePath[MAXPATHLEN];
 
        if (!GetModuleFileName(NULL, FullExePath, sizeof(FullExePath)))
                throw Win32Exception("GetModuleFileName() failed", GetLastError());
 
-       executablePath = FullExePath;
+       result = FullExePath;
 #endif /* _WIN32 */
 
-       result = Utility::DirName(executablePath);
-
        return result;
 }
 
index 5b22e60af7c1b2e08781eb62e3f7f36fb24156d2..9d8aa4930b7e174428a65b08de6711f52f127b52 100644 (file)
@@ -58,7 +58,7 @@ public:
 
 protected:
        void RunEventLoop(void);
-       string GetExeDirectory(void) const;
+       string GetExePath(void) const;
 
 private:
        static Application::Ptr m_Instance; /**< The application instance. */
index 93c525b7eddbc4c0acd426041556e30fca145332..22b5e06ae5e3a8e5fcfe4d032560c2f1d0c5a439 100644 (file)
@@ -5,7 +5,7 @@ namespace icinga
 {
 
 /**
- * A logger that logs to stderr.
+ * A logger that logs to stdout.
  */
 class ConsoleLogger : public Logger
 {
index 75e5bb289b7a490118ed912599e62b3cec73b348..2732a7df8fef4a2003c6e17beeafa233158e0460 100644 (file)
@@ -170,5 +170,6 @@ using boost::system_time;
 #include "threadpool.h"
 #include "logger.h"
 #include "consolelogger.h"
+#include "sysloglogger.h"
 
 #endif /* I2BASE_H */
diff --git a/base/sysloglogger.cpp b/base/sysloglogger.cpp
new file mode 100644 (file)
index 0000000..974d124
--- /dev/null
@@ -0,0 +1,44 @@
+#include "i2-base.h"
+
+using namespace icinga;
+
+/**
+ * Constructor for the SyslogLogger class.
+ *
+ * @param minSeverity Minimum severity for log messages.
+ */
+SyslogLogger::SyslogLogger(const string& ident, LogSeverity minSeverity)
+       : Logger(minSeverity)
+{
+//     openlog(ident.c_str(), 0, LOG_USER);
+}
+
+/**
+ * Processes a log entry and outputs it to syslog.
+ *
+ * @param entry The log entry.
+ */
+void SyslogLogger::ProcessLogEntry(const LogEntry& entry)
+{
+       char timestamp[100];
+
+       int severity;
+       switch (entry.Severity) {
+               case LogDebug:
+                       severity = LOG_DEBUG;
+                       break;
+               case LogInformation:
+                       severity = LOG_INFO;
+                       break;
+               case LogWarning:
+                       severity = LOG_WARNING;
+                       break;
+               case LogCritical:
+                       severity = LOG_CRIT;
+                       break;
+               default:
+                       assert(!"Invalid severity specified.");
+       }
+
+       syslog(severity | LOG_USER, "%s", entry.Message.c_str());
+}
diff --git a/base/sysloglogger.h b/base/sysloglogger.h
new file mode 100644 (file)
index 0000000..ff24839
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef SYSLOGLOGGER_H
+#define SYSLOGLOGGER_H
+
+namespace icinga
+{
+
+/**
+ * A logger that logs to syslog.
+ */
+class SyslogLogger : public Logger
+{
+public:
+       SyslogLogger(const string& ident, LogSeverity minSeverity);
+
+protected:
+       virtual void ProcessLogEntry(const LogEntry& entry);
+};
+
+}
+
+#endif /* SYSLOGLOGGER_H */
index a0130dcc3803b369edc8f666889e52837ad9e05f..cebe038dfdc8a285b7645a58fde5c22e25cadb26 100644 (file)
@@ -33,6 +33,7 @@
 #include <pthread.h>
 #include <signal.h>
 #include <libgen.h>
+#include <syslog.h>
 
 void Sleep(unsigned long milliseconds);
 
index 714b8a41fa3db6babdccba6a84ca47ade079c5bf..bc2ab91b53a56ba3bd531ef4c39fa8c8cbcfd003 100644 (file)
@@ -39,6 +39,9 @@ int IcingaApplication::Main(const vector<string>& args)
        ConsoleLogger::Ptr consoleLogger = boost::make_shared<ConsoleLogger>(LogInformation);
        Logger::RegisterLogger(consoleLogger);
 
+       SyslogLogger::Ptr syslogLogger = boost::make_shared<SyslogLogger>("icinga", LogDebug);
+       Logger::RegisterLogger(syslogLogger);
+
 #ifdef _WIN32
        Logger::Write(LogInformation, "icinga", "Icinga component loader");
 #else /* _WIN32 */
@@ -54,7 +57,7 @@ int IcingaApplication::Main(const vector<string>& args)
                return EXIT_FAILURE;
        }
 
-       string componentDirectory = GetExeDirectory() + "/../lib/icinga2";
+       string componentDirectory = Utility::DirName(GetExePath()) + "/../lib/icinga2";
        AddComponentSearchDir(componentDirectory);
 
        /* register handler for 'component' config objects */