Parsing LogLevel in the Logger
authorJan Olszak <j.olszak@samsung.com>
Wed, 14 May 2014 14:41:27 +0000 (16:41 +0200)
committerJan Olszak <j.olszak@samsung.com>
Mon, 19 May 2014 11:47:16 +0000 (13:47 +0200)
[Bug/Feature]   No way to initialize LogLevel with a string value
[Cause]         N/A
[Solution]      Added parsing of LogLevel from std::string
[Verification]  Build, install and run tests

Change-Id: I1f1dada51d0a131d13aa21d6e49e6c4d37ee0f25

common/log/backend-journal.cpp
common/log/formatter.cpp
common/log/formatter.hpp
common/log/level.cpp [new file with mode: 0644]
common/log/level.hpp
common/log/logger.cpp
common/log/logger.hpp
container-daemon/main.cpp
server/main.cpp
unit_tests/log/ut-logger.cpp

index 9a00470..032ab9b 100644 (file)
@@ -37,7 +37,7 @@ void SystemdJournalBackend::log(LogLevel logLevel,
                                 const std::string& message)
 {
 #define SD_JOURNAL_SUPPRESS_LOCATION
-    sd_journal_send("PRIORITY=%s", LogFormatter::toString(logLevel).c_str(),
+    sd_journal_send("PRIORITY=%s", toString(logLevel).c_str(),
                     "CODE_FILE=%s", file.c_str(),
                     "CODE_LINE=%d", line,
                     "CODE_FUNC=%s", func.c_str(),
index a8f6384..8ac3581 100644 (file)
@@ -77,24 +77,6 @@ std::string LogFormatter::getDefaultConsoleColor(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 "/";
index 6cc783d..3667ecd 100644 (file)
@@ -37,7 +37,6 @@ public:
     static std::string getCurrentTime(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,
diff --git a/common/log/level.cpp b/common/log/level.cpp
new file mode 100644 (file)
index 0000000..5fc3c2a
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Jan Olszak <j.olszak@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  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Functions to handle LogLevel
+ */
+
+
+#include "log/level.hpp"
+
+#include <stdexcept>
+#include <boost/algorithm/string.hpp>
+
+namespace security_containers {
+namespace log {
+
+LogLevel parseLogLevel(const std::string& level)
+{
+    if (boost::iequals(level, "ERROR")) {
+        return LogLevel::ERROR;
+    } else if (boost::iequals(level, "WARN")) {
+        return LogLevel::WARN;
+    } else if (boost::iequals(level, "INFO")) {
+        return LogLevel::INFO;
+    } else if (boost::iequals(level, "DEBUG")) {
+        return LogLevel::DEBUG;
+    } else if (boost::iequals(level, "TRACE")) {
+        return LogLevel::TRACE;
+    } else {
+        throw std::runtime_error("Invalid LogLevel to parse");
+    }
+}
+
+std::string toString(const 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";
+    }
+}
+} // namespace log
+} // namespace security_containers
index 60d8adf..6a54cda 100644 (file)
@@ -25,6 +25,9 @@
 #ifndef COMMON_LOG_LEVEL_HPP
 #define COMMON_LOG_LEVEL_HPP
 
+#include <string>
+
+
 namespace security_containers {
 namespace log {
 
@@ -36,8 +39,20 @@ enum class LogLevel {
     ERROR
 };
 
+/**
+ * @param logLevel LogLevel
+ * @return std::sting representation of the LogLevel value
+ */
+std::string toString(const LogLevel logLevel);
+
+/**
+ * @param level string representation of log level
+ * @return parsed LogLevel value
+ */
+LogLevel parseLogLevel(const std::string& level);
+
+
 } // namespace log
 } // namespace security_containers
 
 #endif // COMMON_LOG_LEVEL_HPP
-
index 3bb58b3..f834557 100644 (file)
@@ -59,11 +59,16 @@ void Logger::logMessage(const std::string& message)
     gLogBackendPtr->log(mLogLevel, mFile, mLine, mFunc, message);
 }
 
-void Logger::setLogLevel(LogLevel level)
+void Logger::setLogLevel(const LogLevel level)
 {
     gLogLevel = level;
 }
 
+void Logger::setLogLevel(const std::string& level)
+{
+    gLogLevel = parseLogLevel(level);
+}
+
 LogLevel Logger::getLogLevel(void)
 {
     return gLogLevel;
index 91a4578..45c5f26 100644 (file)
@@ -46,7 +46,8 @@ public:
 
     void logMessage(const std::string& message);
 
-    static void setLogLevel(LogLevel level);
+    static void setLogLevel(const LogLevel level);
+    static void setLogLevel(const std::string& level);
     static LogLevel getLogLevel(void);
     static void setLogBackend(LogBackend* pBackend);
 
index 40e7fd9..769de69 100644 (file)
@@ -36,7 +36,6 @@
 #include "exception.hpp"
 #include "runner.hpp"
 
-#include <boost/algorithm/string.hpp>
 #include <boost/program_options.hpp>
 #include <iostream>
 
@@ -51,33 +50,6 @@ namespace {
 const std::string PROGRAM_NAME_AND_VERSION =
     "Security Containers Containers Daemon " PROGRAM_VERSION;
 
-/**
- * TODO: This is a copied function, move to common/log
- * Resolve if given log severity level is valid
- *
- * @param s     log severity level
- * @return      LogLevel when valid,
- *              otherwise exception po::validation_error::invalid_option_value thrown
- */
-LogLevel validateLogLevel(const std::string& s)
-{
-    std::string s_capitalized = boost::to_upper_copy(s);
-
-    if (s_capitalized == "ERROR") {
-        return LogLevel::ERROR;
-    } else if (s_capitalized == "WARN") {
-        return LogLevel::WARN;
-    } else if (s_capitalized == "INFO") {
-        return LogLevel::INFO;
-    } else if (s_capitalized == "DEBUG") {
-        return LogLevel::DEBUG;
-    } else if (s_capitalized == "TRACE") {
-        return LogLevel::TRACE;
-    } else {
-        throw po::validation_error(po::validation_error::invalid_option_value);
-    }
-}
-
 } // namespace
 
 
@@ -125,8 +97,7 @@ int main(int argc, char* argv[])
             return 0;
         }
 
-        LogLevel level = validateLogLevel(vm["log-level"].as<std::string>());
-        Logger::setLogLevel(level);
+        Logger::setLogLevel(vm["log-level"].as<std::string>());
 #ifdef LOG_TO_CONSOLE
         Logger::setLogBackend(new StderrBackend());
 #else
index 2b20823..d911071 100644 (file)
@@ -35,7 +35,6 @@
 #include "exception.hpp"
 #include "server.hpp"
 
-#include <boost/algorithm/string.hpp>
 #include <boost/program_options.hpp>
 #include <iostream>
 
@@ -50,32 +49,6 @@ namespace {
 const std::string PROGRAM_NAME_AND_VERSION =
     "Security Containers Server " PROGRAM_VERSION;
 
-/**
- * Resolve if given log severity level is valid
- *
- * @param s     log severity level
- * @return      LogLevel when valid,
- *              otherwise exception po::validation_error::invalid_option_value thrown
- */
-LogLevel validateLogLevel(const std::string& s)
-{
-    std::string s_capitalized = boost::to_upper_copy(s);
-
-    if (s_capitalized == "ERROR") {
-        return LogLevel::ERROR;
-    } else if (s_capitalized == "WARN") {
-        return LogLevel::WARN;
-    } else if (s_capitalized == "INFO") {
-        return LogLevel::INFO;
-    } else if (s_capitalized == "DEBUG") {
-        return LogLevel::DEBUG;
-    } else if (s_capitalized == "TRACE") {
-        return LogLevel::TRACE;
-    } else {
-        throw po::validation_error(po::validation_error::invalid_option_value);
-    }
-}
-
 } // namespace
 
 
@@ -124,8 +97,7 @@ int main(int argc, char* argv[])
             return 0;
         }
 
-        LogLevel level = validateLogLevel(vm["log-level"].as<std::string>());
-        Logger::setLogLevel(level);
+        Logger::setLogLevel(vm["log-level"].as<std::string>());
 #ifdef LOG_TO_CONSOLE
         Logger::setLogBackend(new StderrBackend());
 #else
index 66c698d..696bda5 100644 (file)
@@ -29,6 +29,7 @@
 #include "log/backend.hpp"
 #include "log/backend-stderr.hpp"
 
+#include <stdexcept>
 
 BOOST_AUTO_TEST_SUITE(LogSuite)
 
@@ -47,7 +48,7 @@ public:
              const std::string& func,
              const std::string& message) override
     {
-        mLogStream << '[' + LogFormatter::toString(logLevel) + ']' + ' '
+        mLogStream << '[' + toString(logLevel) + ']' + ' '
                    << file + ':' + std::to_string(line) + ' ' + func + ':'
                    << message << std::endl;
     }
@@ -112,6 +113,29 @@ BOOST_AUTO_TEST_CASE(LogLevelSetandGet)
     BOOST_CHECK(LogLevel::ERROR == Logger::getLogLevel());
 }
 
+BOOST_AUTO_TEST_CASE(StringLogLevelSetandGet)
+{
+    Logger::setLogLevel("TRACE");
+    BOOST_CHECK(LogLevel::TRACE == Logger::getLogLevel());
+
+    Logger::setLogLevel("traCE");
+    BOOST_CHECK(LogLevel::TRACE == Logger::getLogLevel());
+
+    Logger::setLogLevel("DEBUG");
+    BOOST_CHECK(LogLevel::DEBUG == Logger::getLogLevel());
+
+    Logger::setLogLevel("INFO");
+    BOOST_CHECK(LogLevel::INFO == Logger::getLogLevel());
+
+    Logger::setLogLevel("WARN");
+    BOOST_CHECK(LogLevel::WARN == Logger::getLogLevel());
+
+    Logger::setLogLevel("ERROR");
+    BOOST_CHECK(LogLevel::ERROR == Logger::getLogLevel());
+
+    BOOST_REQUIRE_THROW(Logger::setLogLevel("UNKNOWN"), std::runtime_error);
+}
+
 BOOST_AUTO_TEST_CASE(TestLogsError)
 {
     TestLog tf(LogLevel::ERROR);