Add standard stream logger 52/1052/2
authorRolf Badorek <rolf.badorek@nokia.com>
Wed, 2 Oct 2019 14:22:45 +0000 (17:22 +0300)
committerRolf Badorek <rolf.badorek@nokia.com>
Wed, 2 Oct 2019 14:45:44 +0000 (17:45 +0300)
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 <rolf.badorek@nokia.com>
Change-Id: I1b2d4085aa3112b02c4a2161e371712ea5ba2cb5

Makefile.am
configure.ac
include/private/stdstreamlogger.hpp [new file with mode: 0755]
src/createlogger.cpp
src/stdstreamlogger.cpp [new file with mode: 0755]

index 19f2de9..bea0ada 100644 (file)
@@ -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 \
index 216705f..dbf53cf 100644 (file)
@@ -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 (executable)
index 0000000..4af6a8c
--- /dev/null
@@ -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 <memory>
+#include <string>
+#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<std::ostream> osEmerg;
+        std::unique_ptr<std::ostream> osAlert;
+        std::unique_ptr<std::ostream> osCrit;
+        std::unique_ptr<std::ostream> osError;
+        std::unique_ptr<std::ostream> osWarning;
+        std::unique_ptr<std::ostream> osNotice;
+        std::unique_ptr<std::ostream> osInfo;
+        std::unique_ptr<std::ostream> osDebug;
+    };
+}
+
+#endif
index cd85f02..78e03d9 100644 (file)
 
 #include "config.h"
 #include <memory>
+#include <ostream>
 #include <string>
 #include "private/createlogger.hpp"
+#include "private/stdstreamlogger.hpp"
 #if HAVE_SYSTEMLOGGER
 #include "private/systemlogger.hpp"
 #endif
@@ -29,24 +31,24 @@ std::shared_ptr<Logger> shareddatalayer::createLogger(const std::string& prefix)
 #if HAVE_SYSTEMLOGGER
     return std::shared_ptr<Logger>(new SystemLogger(prefix));
 #else
-#error "Need to compile with at least one logging backend"
+    return std::shared_ptr<Logger>(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 (executable)
index 0000000..dc25e29
--- /dev/null
@@ -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 <iostream>
+#include <sstream>
+#include <syslog.h>
+#include <boost/iostreams/stream.hpp>
+#include <boost/iostreams/concepts.hpp>
+
+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>(Sink(prefix, LOG_EMERG)));
+    return *osEmerg;
+}
+
+std::ostream& StdStreamLogger::alert()
+{
+    if (osAlert == nullptr)
+        osAlert.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ALERT)));
+    return *osAlert;
+}
+
+std::ostream& StdStreamLogger::crit()
+{
+    if (osCrit == nullptr)
+        osCrit.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_CRIT)));
+    return *osCrit;
+}
+
+std::ostream& StdStreamLogger::error()
+{
+    if (osError == nullptr)
+        osError.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ERR)));
+    return *osError;
+}
+
+std::ostream& StdStreamLogger::warning()
+{
+    if (osWarning == nullptr)
+        osWarning.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_WARNING)));
+    return *osWarning;
+}
+
+std::ostream& StdStreamLogger::notice()
+{
+    if (osNotice == nullptr)
+        osNotice.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_NOTICE)));
+    return *osNotice;
+}
+
+std::ostream& StdStreamLogger::info()
+{
+    if (osInfo == nullptr)
+        osInfo.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_INFO)));
+    return *osInfo;
+}
+
+std::ostream& StdStreamLogger::debug()
+{
+    if (osDebug == nullptr)
+        osDebug.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_DEBUG)));
+    return *osDebug;
+}