Various changes in logger
authorDariusz Michaluk <d.michaluk@samsung.com>
Tue, 8 Apr 2014 12:23:19 +0000 (14:23 +0200)
committerJan Olszak <j.olszak@samsung.com>
Mon, 19 May 2014 11:47:16 +0000 (13:47 +0200)
[Bug/Feature]   Add systemd journal backend
                Add time and function field in stderr backend
                Set stderr backend in DEBUG build
                Set journal backend in RELEASE build
[Cause]         N/A
[Solution]      N/A
[Verification]  Build with --define "build_type DEBUG" gbs option
                Build with --define "build_type RELEASE" gbs option
                Install, run tests, check logs in output

Change-Id: I3a4d4b3280a201684ac87c4421bfb2a2596fc72b
Signed-off-by: Dariusz Michaluk <d.michaluk@samsung.com>
13 files changed:
common/log/backend-journal.cpp [new file with mode: 0644]
common/log/backend-journal.hpp [new file with mode: 0644]
common/log/backend-null.hpp
common/log/backend-stderr.cpp
common/log/backend-stderr.hpp
common/log/backend.hpp
common/log/logger.cpp
common/log/logger.hpp
packaging/security-containers.spec
server/CMakeLists.txt
server/main.cpp
unit_tests/CMakeLists.txt
unit_tests/log/ut-logger.cpp

diff --git a/common/log/backend-journal.cpp b/common/log/backend-journal.cpp
new file mode 100644 (file)
index 0000000..b772402
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Dariusz Michaluk <d.michaluk@samsung.com>
+ *
+ *  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
+ */
+
+/**
+ * @file
+ * @author  Dariusz Michaluk (d.michaluk@samsung.com)
+ * @brief   Systemd journal backend for logger
+ */
+
+#include "log/backend-journal.hpp"
+#include <systemd/sd-journal.h>
+
+namespace security_containers {
+namespace log {
+
+void SystemdJournalBackend::log(const std::string& severity,
+                                const std::string& file,
+                                const unsigned int& line,
+                                const std::string& func,
+                                const std::string& message)
+{
+#define SD_JOURNAL_SUPPRESS_LOCATION
+    sd_journal_send("PRIORITY=%s", severity.c_str(),
+                    "CODE_FILE=%s", file.c_str(),
+                    "CODE_LINE=%d", line,
+                    "CODE_FUNC=%s", func.c_str(),
+                    "MESSAGE=%s", message.c_str(),
+                    NULL);
+#undef SD_JOURNAL_SUPPRESS_LOCATION
+}
+
+
+} // namespace log
+} // namespace security_containers
+
+
diff --git a/common/log/backend-journal.hpp b/common/log/backend-journal.hpp
new file mode 100644 (file)
index 0000000..e9f7f55
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Dariusz Michaluk <d.michaluk@samsung.com>
+ *
+ *  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
+ */
+
+/**
+ * @file
+ * @author  Dariusz Michaluk (d.michaluk@samsung.com)
+ * @brief   Systemd journal backend for logger
+ */
+
+#ifndef COMMON_LOG_BACKEND_JOURNAL_HPP
+#define COMMON_LOG_BACKEND_JOURNAL_HPP
+
+#include "log/backend.hpp"
+
+
+namespace security_containers {
+namespace log {
+
+
+/**
+    systemd journal logging backend
+ */
+class SystemdJournalBackend : public LogBackend {
+public:
+    void log(const std::string& severity,
+             const std::string& file,
+             const unsigned int& line,
+             const std::string& func,
+             const std::string& message) override;
+};
+
+
+} // namespace log
+} // namespace security_containers
+
+
+#endif // COMMON_LOG_BACKEND_JOURNAL_HPP
index 6947df3..3ee882e 100644 (file)
@@ -38,7 +38,11 @@ namespace log {
  */
 class NullLogger : public LogBackend {
 public:
-    void log(const std::string& /*message*/) override {}
+    void log(const std::string& /*severity*/,
+             const std::string& /*file*/,
+             const unsigned int& /*line*/,
+             const std::string& /*func*/,
+             const std::string& /*message*/) override {}
 };
 
 
index 3210738..dcdfa19 100644 (file)
 
 #include "log/backend-stderr.hpp"
 
-#include <cstdio>
-
+#include <sstream>
+#include <iomanip>
+#include <sys/time.h>
 
 namespace security_containers {
 namespace log {
 
+namespace {
 
-void StderrBackend::log(const std::string& message)
+inline std::string getCurrentTime(void)
 {
-    fprintf(stderr, "%s", message.c_str());
+    char time[13];
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    struct tm* tm = localtime(&tv.tv_sec);
+    sprintf(time, "%02d:%02d:%02d.%03d", tm->tm_hour, tm->tm_min, tm->tm_sec, int(tv.tv_usec / 1000));
+
+    return std::string(time);
+}
+
+} // namespace
+
+void StderrBackend::log(const std::string& severity,
+                        const std::string& file,
+                        const unsigned int& line,
+                        const std::string& func,
+                        const std::string& message)
+{
+    std::ostringstream logLine;
+
+    // example log string
+    // 06:52:35.123 [ERROR] src/util/fs.cpp:43 readFileContent: /file/file.txt is missing
+
+    logLine << getCurrentTime() << ' '
+            << std::left << std::setw(8) << '[' + severity + ']'
+            << std::left << std::setw(52) << file + ':' + std::to_string(line) + ' ' + func + ':'
+            << message << std::endl;
+    fprintf(stderr, "%s", logLine.str().c_str());
 }
 
 
 } // namespace log
 } // namespace security_containers
+
index 7980975..1c47255 100644 (file)
@@ -37,7 +37,11 @@ namespace log {
  */
 class StderrBackend : public LogBackend {
 public:
-    void log(const std::string& message) override;
+    void log(const std::string& severity,
+             const std::string& file,
+             const unsigned int& line,
+             const std::string& func,
+             const std::string& message) override;
 };
 
 
index 2c01bb6..69f7ce1 100644 (file)
@@ -38,7 +38,11 @@ namespace log {
  */
 class LogBackend {
 public:
-    virtual void log(const std::string& message) = 0;
+    virtual void log(const std::string& severity,
+                     const std::string& file,
+                     const unsigned int& line,
+                     const std::string& func,
+                     const std::string& message) = 0;
     virtual ~LogBackend() {}
 };
 
index cfdf0f1..faa15a9 100644 (file)
@@ -25,7 +25,6 @@
 #include "log/logger.hpp"
 #include "log/backend-null.hpp"
 
-#include <iomanip>
 #include <memory>
 #include <mutex>
 #include <cassert>
@@ -52,18 +51,22 @@ inline std::string stripProjectDir(const std::string& file)
 
 } // namespace
 
-Logger::Logger(const std::string& severity, const std::string& file, const int line)
+Logger::Logger(const std::string& severity,
+               const std::string& file,
+               const unsigned int line,
+               const std::string& func)
+    : mSeverity(severity),
+      mFile(stripProjectDir(file)),
+      mLine(line),
+      mFunc(func)
 {
-    mLogLine << std::left << std::setw(8) << '[' + severity + ']'
-             << std::left << std::setw(40) << stripProjectDir(file) + ':' + std::to_string(line)
-             << ' ';
+
 }
 
 void Logger::logMessage(const std::string& message)
 {
-    mLogLine << message << std::endl;
     std::unique_lock<std::mutex> lock(gLogMutex);
-    gLogBackendPtr->log(mLogLine.str());
+    gLogBackendPtr->log(mSeverity, mFile, mLine, mFunc, message);
 }
 
 void Logger::setLogLevel(LogLevel level)
@@ -84,3 +87,4 @@ void Logger::setLogBackend(LogBackend* pBackend)
 
 } // namespace log
 } // namespace security_containers
+
index 1e99c0b..d023bc4 100644 (file)
@@ -42,7 +42,11 @@ class LogBackend;
 
 class Logger {
 public:
-    Logger(const std::string& severity, const std::string& file, const int line);
+    Logger(const std::string& severity,
+           const std::string& file,
+           const unsigned int line,
+           const std::string& func);
+
     void logMessage(const std::string& message);
 
     static void setLogLevel(LogLevel level);
@@ -50,7 +54,10 @@ public:
     static void setLogBackend(LogBackend* pBackend);
 
 private:
-    std::ostringstream mLogLine;
+    std::string mSeverity;
+    std::string mFile;
+    unsigned int mLine;
+    std::string mFunc;
 };
 
 } // namespace log
@@ -63,7 +70,7 @@ private:
             security_containers::log::LogLevel::SEVERITY) { \
             std::ostringstream messageStream__; \
             messageStream__ << MESSAGE; \
-            security_containers::log::Logger logger(#SEVERITY, __FILE__, __LINE__); \
+            security_containers::log::Logger logger(#SEVERITY, __FILE__, __LINE__, __func__); \
             logger.logMessage(messageStream__.str()); \
         } \
     } while(0)
index 73bbfaf..367d77c 100644 (file)
@@ -11,6 +11,7 @@ BuildRequires: cmake
 BuildRequires: libvirt-devel
 BuildRequires: libjson-devel
 BuildRequires: pkgconfig(glib-2.0)
+BuildRequires: pkgconfig(libsystemd-journal)
 
 %description
 This package provides a daemon used to manage containers - start, stop and switch
index 3daba33..5e24fef 100644 (file)
@@ -30,7 +30,7 @@ ADD_EXECUTABLE(${SERVER_CODENAME} ${project_SRCS} ${common_SRCS})
 ## Link libraries ##############################################################
 FIND_PACKAGE (Boost COMPONENTS program_options REQUIRED)
 
-PKG_CHECK_MODULES(SERVER_DEPS REQUIRED libvirt json gio-2.0)
+PKG_CHECK_MODULES(SERVER_DEPS REQUIRED libvirt json gio-2.0 libsystemd-journal)
 INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${SERVER_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
 TARGET_LINK_LIBRARIES(${SERVER_CODENAME} ${SERVER_DEPS_LIBRARIES} ${Boost_LIBRARIES})
 
index d7e7bf9..2b20823 100644 (file)
  * @brief   Main file for the Security Containers Daemon
  */
 
+// Always log to console in DEBUG mode
+#if !defined(LOG_TO_CONSOLE) && !defined(NDEBUG)
+#define LOG_TO_CONSOLE
+#endif
+
 #include "log/logger.hpp"
 #include "log/backend-stderr.hpp"
+#include "log/backend-journal.hpp"
 #include "utils/typeinfo.hpp"
 #include "exception.hpp"
 #include "server.hpp"
@@ -120,7 +126,11 @@ int main(int argc, char* argv[])
 
         LogLevel level = validateLogLevel(vm["log-level"].as<std::string>());
         Logger::setLogLevel(level);
+#ifdef LOG_TO_CONSOLE
         Logger::setLogBackend(new StderrBackend());
+#else
+        Logger::setLogBackend(new SystemdJournalBackend());
+#endif
 
         configPath = vm["config"].as<std::string>();
 
@@ -140,3 +150,4 @@ int main(int argc, char* argv[])
 
     return 0;
 }
+
index f221cc9..55e4a6c 100644 (file)
@@ -34,7 +34,7 @@ ADD_EXECUTABLE(${UT_SERVER_CODENAME} ${project_SRCS} ${common_SRCS} ${server_SRC
 ## Link libraries ##############################################################
 FIND_PACKAGE (Boost COMPONENTS unit_test_framework REQUIRED)
 
-PKG_CHECK_MODULES(UT_SERVER_DEPS REQUIRED libvirt json gio-2.0)
+PKG_CHECK_MODULES(UT_SERVER_DEPS REQUIRED libvirt json gio-2.0 libsystemd-journal)
 INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${SERVER_FOLDER} ${UNIT_TESTS_FOLDER}
                     ${UT_SERVER_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
 TARGET_LINK_LIBRARIES(${UT_SERVER_CODENAME} ${UT_SERVER_DEPS_LIBRARIES} ${Boost_LIBRARIES})
index 6458753..f0a0019 100644 (file)
@@ -24,7 +24,6 @@
  */
 
 #include "ut.hpp"
-
 #include "log/logger.hpp"
 #include "log/backend.hpp"
 #include "log/backend-stderr.hpp"
@@ -39,9 +38,15 @@ public:
     StubbedBackend(std::ostringstream& s) : mLogStream(s) {}
 
     // stubbed function
-    void log(const std::string& s) override
+    void log(const std::string& severity,
+             const std::string& file,
+             const unsigned int& line,
+             const std::string& func,
+             const std::string& message) override
     {
-        mLogStream << s;
+        mLogStream << '[' + severity + ']' + ' '
+                   << file + ':' + std::to_string(line) + ' ' + func + ':'
+                   << message << std::endl;
     }
 
 private:
@@ -164,3 +169,4 @@ BOOST_AUTO_TEST_CASE(TestLogsTrace)
 
 
 BOOST_AUTO_TEST_SUITE_END()
+