From 3b38c04209ba45e19df62f4f89aa2d20c04846d4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 5 Aug 2019 20:15:03 +0100 Subject: Add syslog log writer --- icetray/icetray/logWriterSyslog.cpp | 35 +++++++++++++++++++++++++++++++++ icetray/icetray/logWriterSyslog.h | 21 ++++++++++++++++++++ icetray/unittests/testIceTrayLogger.cpp | 7 +++++++ 3 files changed, 63 insertions(+) create mode 100644 icetray/icetray/logWriterSyslog.cpp create mode 100644 icetray/icetray/logWriterSyslog.h diff --git a/icetray/icetray/logWriterSyslog.cpp b/icetray/icetray/logWriterSyslog.cpp new file mode 100644 index 0000000..d78da5b --- /dev/null +++ b/icetray/icetray/logWriterSyslog.cpp @@ -0,0 +1,35 @@ +#include +#include "logWriterSyslog.h" +#include + +// NOLINTNEXTLINE(modernize-concat-nested-namespaces) +namespace IceTray { + namespace Logging { + AdHocFormatter(LogMsg, "%D: %?"); + SyslogLogWriter::SyslogLogWriter(const Ice::PropertiesPtr & p) : + AbstractLogWriter("logging.syslog", p), + width(p ? p->getPropertyAsIntWithDefault("logging.syslog.width", -1) : -1) + { + if (p) { + openlog( + p->getPropertyWithDefault("logging.syslog.ident", "icetray").c_str(), + p->getPropertyAsIntWithDefault("logging.syslog.option", 0), + p->getPropertyAsIntWithDefault("logging.syslog.facility", LOG_DAEMON)); + } + } + + SyslogLogWriter::~SyslogLogWriter() + { + closelog(); + } + + void + SyslogLogWriter::message(LogLevel priority, Domain domain, const std::string_view message, const Ice::Current &) + { + syslog((int)priority, "%s", LogMsg::get(width, domain, message).c_str()); + } + + FACTORY(SyslogLogWriter, LogWriterFactory); + } +} + diff --git a/icetray/icetray/logWriterSyslog.h b/icetray/icetray/logWriterSyslog.h new file mode 100644 index 0000000..fdce2ec --- /dev/null +++ b/icetray/icetray/logWriterSyslog.h @@ -0,0 +1,21 @@ +#ifndef ICETRAY_LOGGING_SYSLOG_H +#define ICETRAY_LOGGING_SYSLOG_H + +#include "logger.h" + +namespace IceTray { + namespace Logging { + class SyslogLogWriter : public AbstractLogWriter { + public: + SyslogLogWriter(const Ice::PropertiesPtr & p); + ~SyslogLogWriter(); + + void message(LogLevel priority, Domain domain, std::string_view message, const Ice::Current &) override; + + const int width; + }; + } +} + +#endif + diff --git a/icetray/unittests/testIceTrayLogger.cpp b/icetray/unittests/testIceTrayLogger.cpp index 0998143..73142a4 100644 --- a/icetray/unittests/testIceTrayLogger.cpp +++ b/icetray/unittests/testIceTrayLogger.cpp @@ -328,6 +328,13 @@ BOOST_AUTO_TEST_CASE( getLoggerForType ) BOOST_AUTO_TEST_SUITE_END(); +BOOST_AUTO_TEST_CASE( syslog ) +{ + IceTray::Logging::LogWriterPtr lwp = + IceTray::Logging::LogWriterFactory::createNew("SyslogLogWriter", nullptr); + lwp->message(LogLevel::DEBUG, testDomain, "some message", {}); +} + BOOST_AUTO_TEST_CASE( console ) { IceTray::Logging::LogWriterPtr lwp = -- cgit v1.2.3