Implemented syslog logger class.

This commit is contained in:
Gunnar Beutner 2012-07-10 13:31:17 +02:00
parent 60c4dce4dd
commit 50ffb492b9
9 changed files with 88 additions and 14 deletions

View File

@ -34,6 +34,8 @@ libbase_la_SOURCES = \
ringbuffer.h \
socket.cpp \
socket.h \
sysloglogger.cpp \
sysloglogger.h \
tcpclient.cpp \
tcpclient.h \
tcpserver.cpp \

View File

@ -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;
}

View 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. */

View File

@ -5,7 +5,7 @@ namespace icinga
{
/**
* A logger that logs to stderr.
* A logger that logs to stdout.
*/
class ConsoleLogger : public Logger
{

View File

@ -170,5 +170,6 @@ using boost::system_time;
#include "threadpool.h"
#include "logger.h"
#include "consolelogger.h"
#include "sysloglogger.h"
#endif /* I2BASE_H */

44
base/sysloglogger.cpp Normal file
View File

@ -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());
}

21
base/sysloglogger.h Normal file
View File

@ -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 */

View File

@ -33,6 +33,7 @@
#include <pthread.h>
#include <signal.h>
#include <libgen.h>
#include <syslog.h>
void Sleep(unsigned long milliseconds);

View 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 */