Various changes in logger
authorDariusz Michaluk <d.michaluk@samsung.com>
Wed, 16 Apr 2014 09:17:23 +0000 (11:17 +0200)
committerJan Olszak <j.olszak@samsung.com>
Mon, 19 May 2014 11:47:16 +0000 (13:47 +0200)
[Bug/Feature]   Logger class refactoring
                Separate helper functions
                Add coloring to the StderrBackend
[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: Iffca4d7b9edd7640290535605d29140555349876
Signed-off-by: Dariusz Michaluk <d.michaluk@samsung.com>
14 files changed:
common/log/backend-journal.cpp
common/log/backend-journal.hpp
common/log/backend-null.hpp
common/log/backend-stderr.cpp
common/log/backend-stderr.hpp
common/log/backend.hpp
common/log/ccolor.cpp [new file with mode: 0644]
common/log/ccolor.hpp [new file with mode: 0644]
common/log/formatter.cpp [new file with mode: 0644]
common/log/formatter.hpp [new file with mode: 0644]
common/log/level.hpp [new file with mode: 0644]
common/log/logger.cpp
common/log/logger.hpp
unit_tests/log/ut-logger.cpp

index b772402..9a00470 100644 (file)
  */
 
 #include "log/backend-journal.hpp"
+#include "log/formatter.hpp"
+
 #include <systemd/sd-journal.h>
 
 namespace security_containers {
 namespace log {
 
-void SystemdJournalBackend::log(const std::string& severity,
+void SystemdJournalBackend::log(LogLevel logLevel,
                                 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(),
+    sd_journal_send("PRIORITY=%s", LogFormatter::toString(logLevel).c_str(),
                     "CODE_FILE=%s", file.c_str(),
                     "CODE_LINE=%d", line,
                     "CODE_FUNC=%s", func.c_str(),
@@ -44,8 +46,6 @@ void SystemdJournalBackend::log(const std::string& severity,
 #undef SD_JOURNAL_SUPPRESS_LOCATION
 }
 
-
 } // namespace log
 } // namespace security_containers
 
-
index e9f7f55..f040d9a 100644 (file)
@@ -37,7 +37,7 @@ namespace log {
  */
 class SystemdJournalBackend : public LogBackend {
 public:
-    void log(const std::string& severity,
+    void log(LogLevel logLevel,
              const std::string& file,
              const unsigned int& line,
              const std::string& func,
index 3ee882e..7a26344 100644 (file)
@@ -38,7 +38,7 @@ namespace log {
  */
 class NullLogger : public LogBackend {
 public:
-    void log(const std::string& /*severity*/,
+    void log(LogLevel /*logLevel*/,
              const std::string& /*file*/,
              const unsigned int& /*line*/,
              const std::string& /*func*/,
index dcdfa19..8bc2084 100644 (file)
  */
 
 #include "log/backend-stderr.hpp"
+#include "log/formatter.hpp"
 
 #include <sstream>
 #include <iomanip>
-#include <sys/time.h>
 
 namespace security_containers {
 namespace log {
 
-namespace {
-
-inline std::string getCurrentTime(void)
-{
-    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,
+void StderrBackend::log(LogLevel logLevel,
                         const std::string& file,
                         const unsigned int& line,
                         const std::string& func,
@@ -57,10 +42,13 @@ void StderrBackend::log(const std::string& severity,
     // 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 + ']'
+    logLine << LogFormatter::setConsoleColor(logLevel)
+            << LogFormatter::getCurrentTime() << ' '
+            << std::left << std::setw(8) << '[' + LogFormatter::toString(logLevel) + ']'
             << std::left << std::setw(52) << file + ':' + std::to_string(line) + ' ' + func + ':'
-            << message << std::endl;
+            << message
+            << LogFormatter::setDefaultConsoleColor() << std::endl;
+
     fprintf(stderr, "%s", logLine.str().c_str());
 }
 
index 1c47255..c60d523 100644 (file)
@@ -37,7 +37,7 @@ namespace log {
  */
 class StderrBackend : public LogBackend {
 public:
-    void log(const std::string& severity,
+    void log(LogLevel logLevel,
              const std::string& file,
              const unsigned int& line,
              const std::string& func,
index 69f7ce1..adad533 100644 (file)
@@ -26,8 +26,9 @@
 #ifndef COMMON_LOG_BACKEND_HPP
 #define COMMON_LOG_BACKEND_HPP
 
-#include <string>
+#include "log/level.hpp"
 
+#include <string>
 
 namespace security_containers {
 namespace log {
@@ -38,7 +39,7 @@ namespace log {
  */
 class LogBackend {
 public:
-    virtual void log(const std::string& severity,
+    virtual void log(LogLevel logLevel,
                      const std::string& file,
                      const unsigned int& line,
                      const std::string& func,
diff --git a/common/log/ccolor.cpp b/common/log/ccolor.cpp
new file mode 100644 (file)
index 0000000..703fcfc
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ *  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   Console color for StderrBackend logger
+ */
+
+#include "log/ccolor.hpp"
+
+#include <stdio.h>
+
+namespace security_containers {
+namespace log {
+
+std::string getConsoleEscapeSequence(Attributes attr, Color color)
+{
+    char command[10];
+
+    // Command is the control command to the terminal
+    snprintf(command, sizeof(command), "%c[%d;%dm", 0x1B, attr, color);
+    return std::string(command);
+}
+
+} // namespace log
+} // namespace security_containers
+
diff --git a/common/log/ccolor.hpp b/common/log/ccolor.hpp
new file mode 100644 (file)
index 0000000..ef93146
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ *  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   Console color for StderrBackend logger
+ */
+
+#ifndef COMMON_LOG_CCOLOR_HPP
+#define COMMON_LOG_CCOLOR_HPP
+
+#include <string>
+
+namespace security_containers {
+namespace log {
+
+enum class Color : unsigned int {
+    DEFAULT     = 0,
+    BLACK       = 90,
+    RED         = 91,
+    GREEN       = 92,
+    YELLOW      = 93,
+    BLUE        = 94,
+    MAGENTA     = 95,
+    CYAN        = 96,
+    WHITE       = 97,
+};
+
+enum class Attributes : unsigned int {
+    DEFAULT     = 0,
+    BOLD        = 1,
+};
+
+std::string getConsoleEscapeSequence(Attributes attr, Color color);
+
+} // namespace log
+} // namespace security_containers
+
+#endif // COMMON_LOG_CCOLOR_HPP
+
diff --git a/common/log/formatter.cpp b/common/log/formatter.cpp
new file mode 100644 (file)
index 0000000..c86d212
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ *  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   Helper formatter for logger
+ */
+
+#include "log/formatter.hpp"
+#include "log/ccolor.hpp"
+
+#include <sys/time.h>
+#include <cassert>
+
+namespace security_containers {
+namespace log {
+
+std::string LogFormatter::getCurrentTime(void)
+{
+    char time[13];
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    struct tm* tm = localtime(&tv.tv_sec);
+    snprintf(time,
+             sizeof(time),
+             "%02d:%02d:%02d.%03d",
+             tm->tm_hour,
+             tm->tm_min,
+             tm->tm_sec,
+             int(tv.tv_usec / 1000));
+
+    return std::string(time);
+}
+
+std::string LogFormatter::setConsoleColor(LogLevel logLevel)
+{
+    switch (logLevel) {
+    case LogLevel::ERROR:
+        return getConsoleEscapeSequence(Attributes::BOLD, Color::RED);
+    case LogLevel::WARN:
+        return getConsoleEscapeSequence(Attributes::BOLD, Color::YELLOW);
+    case LogLevel::INFO:
+        return getConsoleEscapeSequence(Attributes::BOLD, Color::BLUE);
+    case LogLevel::DEBUG:
+        return getConsoleEscapeSequence(Attributes::DEFAULT, Color::GREEN);
+    case LogLevel::TRACE:
+        return getConsoleEscapeSequence(Attributes::DEFAULT, Color::BLACK);
+    default:
+        return getConsoleEscapeSequence(Attributes::DEFAULT, Color::DEFAULT);
+    }
+}
+
+std::string LogFormatter::setDefaultConsoleColor(void)
+{
+    return getConsoleEscapeSequence(Attributes::DEFAULT, Color::DEFAULT);
+}
+
+std::string LogFormatter::toString(LogLevel logLevel)
+{
+    switch (logLevel) {
+    case LogLevel::ERROR:
+        return "ERROR";
+    case LogLevel::WARN:
+        return "WARN";
+    case LogLevel::INFO:
+        return "INFO";
+    case LogLevel::DEBUG:
+        return "DEBUG";
+    case LogLevel::TRACE:
+        return "TRACE";
+    default:
+        return "UNKNOWN";
+    }
+}
+
+std::string LogFormatter::stripProjectDir(const std::string& file)
+{
+    const std::string SOURCE_DIR = PROJECT_SOURCE_DIR "/";
+    // it will work until someone use in cmake FILE(GLOB ... RELATIVE ...)
+    assert(0 == file.compare(0, SOURCE_DIR.size(), SOURCE_DIR));
+    return file.substr(SOURCE_DIR.size());
+}
+
+} // namespace log
+} // namespace security_containers
+
diff --git a/common/log/formatter.hpp b/common/log/formatter.hpp
new file mode 100644 (file)
index 0000000..ceafae5
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *  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   Helper formatter for logger
+ */
+
+#ifndef COMMON_LOG_FORMATTER_HPP
+#define COMMON_LOG_FORMATTER_HPP
+
+#include "log/level.hpp"
+
+#include <string>
+
+namespace security_containers {
+namespace log {
+
+class LogFormatter {
+public:
+    static std::string getCurrentTime(void);
+    static std::string setConsoleColor(LogLevel logLevel);
+    static std::string setDefaultConsoleColor(void);
+    static std::string toString(LogLevel logLevel);
+    static std::string stripProjectDir(const std::string& file);
+};
+
+} // namespace log
+} // namespace security_containers
+
+#endif // COMMON_LOG_FORMATTER_HPP
+
diff --git a/common/log/level.hpp b/common/log/level.hpp
new file mode 100644 (file)
index 0000000..60d8adf
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ *  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   LogLevel
+ */
+
+#ifndef COMMON_LOG_LEVEL_HPP
+#define COMMON_LOG_LEVEL_HPP
+
+namespace security_containers {
+namespace log {
+
+enum class LogLevel {
+    TRACE,
+    DEBUG,
+    INFO,
+    WARN,
+    ERROR
+};
+
+} // namespace log
+} // namespace security_containers
+
+#endif // COMMON_LOG_LEVEL_HPP
+
index faa15a9..3bb58b3 100644 (file)
  */
 
 #include "log/logger.hpp"
+#include "log/formatter.hpp"
 #include "log/backend-null.hpp"
 
 #include <memory>
 #include <mutex>
-#include <cassert>
-
 
 namespace security_containers {
 namespace log {
@@ -40,23 +39,14 @@ volatile LogLevel gLogLevel = LogLevel::DEBUG;
 std::unique_ptr<LogBackend> gLogBackendPtr(new NullLogger());
 std::mutex gLogMutex;
 
-const std::string SOURCE_DIR = PROJECT_SOURCE_DIR "/";
-
-inline std::string stripProjectDir(const std::string& file)
-{
-    // it will work until someone use in cmake FILE(GLOB ... RELATIVE ...)
-    assert(0 == file.compare(0, SOURCE_DIR.size(), SOURCE_DIR));
-    return file.substr(SOURCE_DIR.size());
-}
-
 } // namespace
 
-Logger::Logger(const std::string& severity,
+Logger::Logger(LogLevel logLevel,
                const std::string& file,
                const unsigned int line,
                const std::string& func)
-    : mSeverity(severity),
-      mFile(stripProjectDir(file)),
+    : mLogLevel(logLevel),
+      mFile(LogFormatter::stripProjectDir(file)),
       mLine(line),
       mFunc(func)
 {
@@ -66,7 +56,7 @@ Logger::Logger(const std::string& severity,
 void Logger::logMessage(const std::string& message)
 {
     std::unique_lock<std::mutex> lock(gLogMutex);
-    gLogBackendPtr->log(mSeverity, mFile, mLine, mFunc, message);
+    gLogBackendPtr->log(mLogLevel, mFile, mLine, mFunc, message);
 }
 
 void Logger::setLogLevel(LogLevel level)
index d023bc4..91a4578 100644 (file)
@@ -26,6 +26,8 @@
 #ifndef COMMON_LOG_LOGGER_HPP
 #define COMMON_LOG_LOGGER_HPP
 
+#include "log/level.hpp"
+
 #include <sstream>
 #include <string>
 
 namespace security_containers {
 namespace log {
 
-
-enum class LogLevel {
-    TRACE, DEBUG, INFO, WARN, ERROR
-};
-
 class LogBackend;
 
 class Logger {
 public:
-    Logger(const std::string& severity,
+    Logger(LogLevel logLevel,
            const std::string& file,
            const unsigned int line,
            const std::string& func);
@@ -54,7 +51,7 @@ public:
     static void setLogBackend(LogBackend* pBackend);
 
 private:
-    std::string mSeverity;
+    LogLevel mLogLevel;
     std::string mFile;
     unsigned int mLine;
     std::string mFunc;
@@ -70,7 +67,10 @@ private:
             security_containers::log::LogLevel::SEVERITY) { \
             std::ostringstream messageStream__; \
             messageStream__ << MESSAGE; \
-            security_containers::log::Logger logger(#SEVERITY, __FILE__, __LINE__, __func__); \
+            security_containers::log::Logger logger(security_containers::log::LogLevel::SEVERITY, \
+                                                    __FILE__, \
+                                                    __LINE__, \
+                                                    __func__); \
             logger.logMessage(messageStream__.str()); \
         } \
     } while(0)
@@ -83,3 +83,4 @@ private:
 
 
 #endif // COMMON_LOG_LOGGER_HPP
+
index f0a0019..1f4f000 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "ut.hpp"
 #include "log/logger.hpp"
+#include "log/formatter.hpp"
 #include "log/backend.hpp"
 #include "log/backend-stderr.hpp"
 
@@ -38,13 +39,13 @@ public:
     StubbedBackend(std::ostringstream& s) : mLogStream(s) {}
 
     // stubbed function
-    void log(const std::string& severity,
+    void log(LogLevel logLevel,
              const std::string& file,
              const unsigned int& line,
              const std::string& func,
              const std::string& message) override
     {
-        mLogStream << '[' + severity + ']' + ' '
+        mLogStream << '[' + LogFormatter::toString(logLevel) + ']' + ' '
                    << file + ':' + std::to_string(line) + ' ' + func + ':'
                    << message << std::endl;
     }