mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-31 01:24:19 +02:00
Implemented syslog logger class.
This commit is contained in:
parent
60c4dce4dd
commit
50ffb492b9
@ -34,6 +34,8 @@ libbase_la_SOURCES = \
|
|||||||
ringbuffer.h \
|
ringbuffer.h \
|
||||||
socket.cpp \
|
socket.cpp \
|
||||||
socket.h \
|
socket.h \
|
||||||
|
sysloglogger.cpp \
|
||||||
|
sysloglogger.h \
|
||||||
tcpclient.cpp \
|
tcpclient.cpp \
|
||||||
tcpclient.h \
|
tcpclient.h \
|
||||||
tcpserver.cpp \
|
tcpserver.cpp \
|
||||||
|
@ -140,12 +140,16 @@ Component::Ptr Application::LoadComponent(const string& path,
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
HMODULE hModule = LoadLibrary(path.c_str());
|
HMODULE hModule = LoadLibrary(path.c_str());
|
||||||
#else /* _WIN32 */
|
|
||||||
lt_dlhandle hModule = lt_dlopen(path.c_str());
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
|
|
||||||
if (hModule == NULL)
|
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
|
#ifdef _WIN32
|
||||||
pCreateComponent = (CreateComponentFunction)GetProcAddress(hModule,
|
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;
|
static string result;
|
||||||
|
|
||||||
@ -268,18 +272,16 @@ string Application::GetExeDirectory(void) const
|
|||||||
if (realpath(executablePath.c_str(), buffer) == NULL)
|
if (realpath(executablePath.c_str(), buffer) == NULL)
|
||||||
throw PosixException("realpath failed", errno);
|
throw PosixException("realpath failed", errno);
|
||||||
|
|
||||||
executablePath = buffer;
|
result = buffer;
|
||||||
#else /* _WIN32 */
|
#else /* _WIN32 */
|
||||||
char FullExePath[MAXPATHLEN];
|
char FullExePath[MAXPATHLEN];
|
||||||
|
|
||||||
if (!GetModuleFileName(NULL, FullExePath, sizeof(FullExePath)))
|
if (!GetModuleFileName(NULL, FullExePath, sizeof(FullExePath)))
|
||||||
throw Win32Exception("GetModuleFileName() failed", GetLastError());
|
throw Win32Exception("GetModuleFileName() failed", GetLastError());
|
||||||
|
|
||||||
executablePath = FullExePath;
|
result = FullExePath;
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
result = Utility::DirName(executablePath);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void RunEventLoop(void);
|
void RunEventLoop(void);
|
||||||
string GetExeDirectory(void) const;
|
string GetExePath(void) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Application::Ptr m_Instance; /**< The application instance. */
|
static Application::Ptr m_Instance; /**< The application instance. */
|
||||||
|
@ -5,7 +5,7 @@ namespace icinga
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A logger that logs to stderr.
|
* A logger that logs to stdout.
|
||||||
*/
|
*/
|
||||||
class ConsoleLogger : public Logger
|
class ConsoleLogger : public Logger
|
||||||
{
|
{
|
||||||
|
@ -170,5 +170,6 @@ using boost::system_time;
|
|||||||
#include "threadpool.h"
|
#include "threadpool.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "consolelogger.h"
|
#include "consolelogger.h"
|
||||||
|
#include "sysloglogger.h"
|
||||||
|
|
||||||
#endif /* I2BASE_H */
|
#endif /* I2BASE_H */
|
||||||
|
44
base/sysloglogger.cpp
Normal file
44
base/sysloglogger.cpp
Normal 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
21
base/sysloglogger.h
Normal 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 */
|
@ -33,6 +33,7 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
|
#include <syslog.h>
|
||||||
|
|
||||||
void Sleep(unsigned long milliseconds);
|
void Sleep(unsigned long milliseconds);
|
||||||
|
|
||||||
|
@ -39,6 +39,9 @@ int IcingaApplication::Main(const vector<string>& args)
|
|||||||
ConsoleLogger::Ptr consoleLogger = boost::make_shared<ConsoleLogger>(LogInformation);
|
ConsoleLogger::Ptr consoleLogger = boost::make_shared<ConsoleLogger>(LogInformation);
|
||||||
Logger::RegisterLogger(consoleLogger);
|
Logger::RegisterLogger(consoleLogger);
|
||||||
|
|
||||||
|
SyslogLogger::Ptr syslogLogger = boost::make_shared<SyslogLogger>("icinga", LogDebug);
|
||||||
|
Logger::RegisterLogger(syslogLogger);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
Logger::Write(LogInformation, "icinga", "Icinga component loader");
|
Logger::Write(LogInformation, "icinga", "Icinga component loader");
|
||||||
#else /* _WIN32 */
|
#else /* _WIN32 */
|
||||||
@ -54,7 +57,7 @@ int IcingaApplication::Main(const vector<string>& args)
|
|||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
string componentDirectory = GetExeDirectory() + "/../lib/icinga2";
|
string componentDirectory = Utility::DirName(GetExePath()) + "/../lib/icinga2";
|
||||||
AddComponentSearchDir(componentDirectory);
|
AddComponentSearchDir(componentDirectory);
|
||||||
|
|
||||||
/* register handler for 'component' config objects */
|
/* register handler for 'component' config objects */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user