Properly aligned log when '\n' is used
authorMateusz Malicki <m.malicki2@samsung.com>
Tue, 22 Apr 2014 12:54:43 +0000 (14:54 +0200)
committerJan Olszak <j.olszak@samsung.com>
Mon, 19 May 2014 11:47:16 +0000 (13:47 +0200)
[Bug/Feature]   Multiline logs in SC
[Cause]         Wrong aligned logs when '\n' is used
[Solution]      N/A
[Verification]  Build with --define 'build_type DEBUG' and
                run 'security-containers-server -l TRACE' on the target

Change-Id: I260ed215b9dc9a428973b25ac89c0b0176f8b7c4

common/log/backend-stderr.cpp
common/log/formatter.cpp
common/log/formatter.hpp

index 8bc2084..4d0aaa7 100644 (file)
@@ -25,8 +25,7 @@
 #include "log/backend-stderr.hpp"
 #include "log/formatter.hpp"
 
-#include <sstream>
-#include <iomanip>
+#include <boost/tokenizer.hpp>
 
 namespace security_containers {
 namespace log {
@@ -37,19 +36,26 @@ void StderrBackend::log(LogLevel logLevel,
                         const std::string& func,
                         const std::string& message)
 {
-    std::ostringstream logLine;
+    typedef boost::char_separator<char> charSeparator;
+    typedef boost::tokenizer<charSeparator> tokenizer;
 
     // example log string
     // 06:52:35.123 [ERROR] src/util/fs.cpp:43 readFileContent: /file/file.txt is missing
 
-    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
-            << LogFormatter::setDefaultConsoleColor() << std::endl;
-
-    fprintf(stderr, "%s", logLine.str().c_str());
+    const std::string logColor = LogFormatter::getConsoleColor(logLevel);
+    const std::string defaultColor = LogFormatter::getDefaultConsoleColor();
+    const std::string header = LogFormatter::getHeader(logLevel, file, line, func);
+    tokenizer tokens(message, charSeparator("\n"));
+    for(const auto& line : tokens) {
+        if (!line.empty()) {
+            fprintf(stderr,
+                    "%s%s%s%s\n",
+                    logColor.c_str(),
+                    header.c_str(),
+                    line.c_str(),
+                    defaultColor.c_str());
+        }
+    }
 }
 
 
index c86d212..a8f6384 100644 (file)
 
 #include <sys/time.h>
 #include <cassert>
+#include <sstream>
+#include <iomanip>
 
 namespace security_containers {
 namespace log {
 
+const int TIME_COLUMN_LENGTH = 12;
+const int SEVERITY_COLUMN_LENGTH = 8;
+const int FILE_COLUMN_LENGTH = 52;
+
 std::string LogFormatter::getCurrentTime(void)
 {
-    char time[13];
+    char time[TIME_COLUMN_LENGTH + 1];
     struct timeval tv;
     gettimeofday(&tv, NULL);
     struct tm* tm = localtime(&tv.tv_sec);
@@ -48,7 +54,7 @@ std::string LogFormatter::getCurrentTime(void)
     return std::string(time);
 }
 
-std::string LogFormatter::setConsoleColor(LogLevel logLevel)
+std::string LogFormatter::getConsoleColor(LogLevel logLevel)
 {
     switch (logLevel) {
     case LogLevel::ERROR:
@@ -66,7 +72,7 @@ std::string LogFormatter::setConsoleColor(LogLevel logLevel)
     }
 }
 
-std::string LogFormatter::setDefaultConsoleColor(void)
+std::string LogFormatter::getDefaultConsoleColor(void)
 {
     return getConsoleEscapeSequence(Attributes::DEFAULT, Color::DEFAULT);
 }
@@ -97,6 +103,19 @@ std::string LogFormatter::stripProjectDir(const std::string& file)
     return file.substr(SOURCE_DIR.size());
 }
 
+std::string LogFormatter::getHeader(LogLevel logLevel,
+                                    const std::string& file,
+                                    const unsigned int& line,
+                                    const std::string& func)
+{
+    std::ostringstream logLine;
+    logLine << getCurrentTime() << ' '
+            << std::left << std::setw(SEVERITY_COLUMN_LENGTH) << '[' + toString(logLevel) + ']'
+            << std::left << std::setw(FILE_COLUMN_LENGTH)
+            << file + ':' + std::to_string(line) + ' ' + func + ':';
+    return logLine.str();
+}
+
 } // namespace log
 } // namespace security_containers
 
index ceafae5..6cc783d 100644 (file)
@@ -35,10 +35,14 @@ namespace log {
 class LogFormatter {
 public:
     static std::string getCurrentTime(void);
-    static std::string setConsoleColor(LogLevel logLevel);
-    static std::string setDefaultConsoleColor(void);
+    static std::string getConsoleColor(LogLevel logLevel);
+    static std::string getDefaultConsoleColor(void);
     static std::string toString(LogLevel logLevel);
     static std::string stripProjectDir(const std::string& file);
+    static std::string getHeader(LogLevel logLevel,
+                                 const std::string& file,
+                                 const unsigned int& line,
+                                 const std::string& func);
 };
 
 } // namespace log