diff --git a/base/Makefile.am b/base/Makefile.am index 51111332a..d93337638 100644 --- a/base/Makefile.am +++ b/base/Makefile.am @@ -34,6 +34,8 @@ libbase_la_SOURCES = \ ringbuffer.h \ socket.cpp \ socket.h \ + sysloglogger.cpp \ + sysloglogger.h \ tcpclient.cpp \ tcpclient.h \ tcpserver.cpp \ diff --git a/base/application.cpp b/base/application.cpp index 352cbf2e8..7d2d4c636 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -140,12 +140,16 @@ Component::Ptr Application::LoadComponent(const string& path, #ifdef _WIN32 HMODULE hModule = LoadLibrary(path.c_str()); -#else /* _WIN32 */ - lt_dlhandle hModule = lt_dlopen(path.c_str()); -#endif /* _WIN32 */ if (hModule == NULL) - throw runtime_error("Could not load module"); + throw Win32Exception("LoadLibrary('" + path + "') failed", GetLastError()); +#else /* _WIN32 */ + lt_dlhandle hModule = lt_dlopen(path.c_str()); + + 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; } diff --git a/base/application.h b/base/application.h index 5b22e60af..9d8aa4930 100644 --- a/base/application.h +++ b/base/application.h @@ -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. */ diff --git a/base/consolelogger.h b/base/consolelogger.h index 93c525b7e..22b5e06ae 100644 --- a/base/consolelogger.h +++ b/base/consolelogger.h @@ -5,7 +5,7 @@ namespace icinga { /** - * A logger that logs to stderr. + * A logger that logs to stdout. */ class ConsoleLogger : public Logger { diff --git a/base/i2-base.h b/base/i2-base.h index 75e5bb289..2732a7df8 100644 --- a/base/i2-base.h +++ b/base/i2-base.h @@ -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 index 000000000..974d1248d --- /dev/null +++ b/base/sysloglogger.cpp @@ -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 index 000000000..ff2483928 --- /dev/null +++ b/base/sysloglogger.h @@ -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 */ diff --git a/base/unix.h b/base/unix.h index a0130dcc3..cebe038df 100644 --- a/base/unix.h +++ b/base/unix.h @@ -33,6 +33,7 @@ #include #include #include +#include void Sleep(unsigned long milliseconds); diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index 714b8a41f..bc2ab91b5 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -39,6 +39,9 @@ int IcingaApplication::Main(const vector& args) ConsoleLogger::Ptr consoleLogger = boost::make_shared(LogInformation); Logger::RegisterLogger(consoleLogger); + SyslogLogger::Ptr syslogLogger = boost::make_shared("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& args) return EXIT_FAILURE; } - string componentDirectory = GetExeDirectory() + "/../lib/icinga2"; + string componentDirectory = Utility::DirName(GetExePath()) + "/../lib/icinga2"; AddComponentSearchDir(componentDirectory); /* register handler for 'component' config objects */