From: Rolf Badorek Date: Wed, 2 Oct 2019 14:22:45 +0000 (+0300) Subject: Add standard stream logger X-Git-Tag: 1.2.1~22 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=65fb3b1375a1baaf75a581582a4c8d0d62fbabb6;p=ric-plt%2Fsdl.git Add standard stream logger Added and activated logger implementation, which will write logs to stdout and stderr. This way SDL logs are included to 'kubectl logs' command output of containerized application which is using SDL library. SDL logs starts with "shareddatalayer: " prefix. RIC has own logging library, which will be taken into use later as a separate commit / merge request. Signed-off-by: Rolf Badorek Change-Id: I1b2d4085aa3112b02c4a2161e371712ea5ba2cb5 --- diff --git a/Makefile.am b/Makefile.am index 19f2de9..bea0ada 100644 --- a/Makefile.am +++ b/Makefile.am @@ -35,6 +35,7 @@ libsdl_la_SOURCES = \ include/private/namespaceconfigurations.hpp \ include/private/namespaceconfigurationsimpl.hpp \ include/private/namespacevalidator.hpp \ + include/private/stdstreamlogger.hpp \ include/private/syncstorageimpl.hpp \ include/private/system.hpp \ include/private/timer.hpp \ @@ -67,6 +68,7 @@ libsdl_la_SOURCES = \ src/publisherid.cpp \ src/rejectedbybackend.cpp \ src/rejectedbysdl.cpp \ + src/stdstreamlogger.cpp \ src/syncstorage.cpp \ src/syncstorageimpl.cpp \ src/system.cpp \ diff --git a/configure.ac b/configure.ac index 216705f..dbf53cf 100644 --- a/configure.ac +++ b/configure.ac @@ -11,7 +11,7 @@ m4_define([SDL_MAJOR], [1]) m4_define([SDL_MINOR], [0]) -m4_define([SDL_MICRO], [1]) +m4_define([SDL_MICRO], [2]) # SDL ABI version with libtool # @@ -26,7 +26,7 @@ m4_define([SDL_MICRO], [1]) # Change the numbers just before release. m4_define([SDL_CURRENT], [1]) -m4_define([SDL_REVISION], [1]) +m4_define([SDL_REVISION], [2]) m4_define([SDL_AGE], [0]) AC_INIT([shareddatalayer], [SDL_MAJOR.SDL_MINOR.SDL_MICRO], [], [], [https://gerrit.o-ran-sc.org/r/admin/repos/ric-plt/sdl]) @@ -53,8 +53,8 @@ AX_BOOST_SYSTEM AX_BOOST_FILESYSTEM PKG_PROG_PKG_CONFIG -AC_DEFINE([HAVE_SYSTEMLOGGER], [1], [Have systemlogger]) -AM_CONDITIONAL([SYSTEMLOGGER], [test xtrue]) +AC_DEFINE([HAVE_SYSTEMLOGGER], [0], [Have systemlogger]) +AM_CONDITIONAL([SYSTEMLOGGER], [test "xyes" = "xno"]) SDL_CONF_DIR=$sysconfdir/$PACKAGE_NAME.d AC_SUBST(SDL_CONF_DIR) diff --git a/include/private/stdstreamlogger.hpp b/include/private/stdstreamlogger.hpp new file mode 100755 index 0000000..4af6a8c --- /dev/null +++ b/include/private/stdstreamlogger.hpp @@ -0,0 +1,62 @@ +/* + Copyright (c) 2018-2019 Nokia. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef SHAREDDATALAYER_STDSTREAMLOGGER_HPP_ +#define SHAREDDATALAYER_STDSTREAMLOGGER_HPP_ + +#include +#include +#include "private/logger.hpp" + +namespace shareddatalayer +{ + class StdStreamLogger: public Logger + { + public: + explicit StdStreamLogger(const std::string& prefix); + + ~StdStreamLogger(); + + std::ostream& emerg() override; + + std::ostream& alert() override; + + std::ostream& crit() override; + + std::ostream& error() override; + + std::ostream& warning() override; + + std::ostream& notice() override; + + std::ostream& info() override; + + std::ostream& debug() override; + + private: + const std::string prefix; + std::unique_ptr osEmerg; + std::unique_ptr osAlert; + std::unique_ptr osCrit; + std::unique_ptr osError; + std::unique_ptr osWarning; + std::unique_ptr osNotice; + std::unique_ptr osInfo; + std::unique_ptr osDebug; + }; +} + +#endif diff --git a/src/createlogger.cpp b/src/createlogger.cpp index cd85f02..78e03d9 100644 --- a/src/createlogger.cpp +++ b/src/createlogger.cpp @@ -16,8 +16,10 @@ #include "config.h" #include +#include #include #include "private/createlogger.hpp" +#include "private/stdstreamlogger.hpp" #if HAVE_SYSTEMLOGGER #include "private/systemlogger.hpp" #endif @@ -29,24 +31,24 @@ std::shared_ptr shareddatalayer::createLogger(const std::string& prefix) #if HAVE_SYSTEMLOGGER return std::shared_ptr(new SystemLogger(prefix)); #else -#error "Need to compile with at least one logging backend" + return std::shared_ptr(new StdStreamLogger(prefix)); #endif } void shareddatalayer::logDebugOnce(const std::string& msg) noexcept { auto logger(createLogger(SDL_LOG_PREFIX)); - logger->debug() << msg; + logger->debug() << msg << std::endl; } void shareddatalayer::logErrorOnce(const std::string& msg) noexcept { auto logger(createLogger(SDL_LOG_PREFIX)); - logger->error() << msg; + logger->error() << msg << std::endl; } void shareddatalayer::logInfoOnce(const std::string& msg) noexcept { auto logger(createLogger(SDL_LOG_PREFIX)); - logger->info() << msg; + logger->info() << msg << std::endl; } diff --git a/src/stdstreamlogger.cpp b/src/stdstreamlogger.cpp new file mode 100755 index 0000000..dc25e29 --- /dev/null +++ b/src/stdstreamlogger.cpp @@ -0,0 +1,115 @@ +/* + Copyright (c) 2018-2019 Nokia. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include "private/stdstreamlogger.hpp" +#include +#include +#include +#include +#include + +using namespace shareddatalayer; + +namespace +{ + class Sink: public boost::iostreams::sink + { + public: + Sink(const std::string& prefix, int level): prefix(prefix), level(level) { } + + ~Sink() { } + + std::streamsize write(const char* s, std::streamsize n); + + private: + const std::string prefix; + const int level; + }; +} + +std::streamsize Sink::write(const char* s, std::streamsize n) +{ + if (level < LOG_ERR) + std::cout << prefix << ": " << std::string(s, n); + else + std::cerr << prefix << ": " << std::string(s, n); + return n; +} + +StdStreamLogger::StdStreamLogger(const std::string& prefix): + prefix(prefix) +{ +} + +StdStreamLogger::~StdStreamLogger() +{ +} + +std::ostream& StdStreamLogger::emerg() +{ + if (osEmerg == nullptr) + osEmerg.reset(new boost::iostreams::stream(Sink(prefix, LOG_EMERG))); + return *osEmerg; +} + +std::ostream& StdStreamLogger::alert() +{ + if (osAlert == nullptr) + osAlert.reset(new boost::iostreams::stream(Sink(prefix, LOG_ALERT))); + return *osAlert; +} + +std::ostream& StdStreamLogger::crit() +{ + if (osCrit == nullptr) + osCrit.reset(new boost::iostreams::stream(Sink(prefix, LOG_CRIT))); + return *osCrit; +} + +std::ostream& StdStreamLogger::error() +{ + if (osError == nullptr) + osError.reset(new boost::iostreams::stream(Sink(prefix, LOG_ERR))); + return *osError; +} + +std::ostream& StdStreamLogger::warning() +{ + if (osWarning == nullptr) + osWarning.reset(new boost::iostreams::stream(Sink(prefix, LOG_WARNING))); + return *osWarning; +} + +std::ostream& StdStreamLogger::notice() +{ + if (osNotice == nullptr) + osNotice.reset(new boost::iostreams::stream(Sink(prefix, LOG_NOTICE))); + return *osNotice; +} + +std::ostream& StdStreamLogger::info() +{ + if (osInfo == nullptr) + osInfo.reset(new boost::iostreams::stream(Sink(prefix, LOG_INFO))); + return *osInfo; +} + +std::ostream& StdStreamLogger::debug() +{ + if (osDebug == nullptr) + osDebug.reset(new boost::iostreams::stream(Sink(prefix, LOG_DEBUG))); + return *osDebug; +}