From 44ce3715eb382aa06dd94da3dcfacef7cda9aada Mon Sep 17 00:00:00 2001 From: Jan Olszak Date: Wed, 14 May 2014 16:41:27 +0200 Subject: [PATCH] Parsing LogLevel in the Logger [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 | 2 +- common/log/formatter.cpp | 18 ----------- common/log/formatter.hpp | 1 - common/log/level.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++ common/log/level.hpp | 17 ++++++++++- common/log/logger.cpp | 7 ++++- common/log/logger.hpp | 3 +- container-daemon/main.cpp | 31 +------------------ server/main.cpp | 30 +----------------- unit_tests/log/ut-logger.cpp | 26 +++++++++++++++- 10 files changed, 121 insertions(+), 83 deletions(-) create mode 100644 common/log/level.cpp diff --git a/common/log/backend-journal.cpp b/common/log/backend-journal.cpp index 9a00470..032ab9b 100644 --- a/common/log/backend-journal.cpp +++ b/common/log/backend-journal.cpp @@ -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(), diff --git a/common/log/formatter.cpp b/common/log/formatter.cpp index a8f6384..8ac3581 100644 --- a/common/log/formatter.cpp +++ b/common/log/formatter.cpp @@ -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 "/"; diff --git a/common/log/formatter.hpp b/common/log/formatter.hpp index 6cc783d..3667ecd 100644 --- a/common/log/formatter.hpp +++ b/common/log/formatter.hpp @@ -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 index 0000000..5fc3c2a --- /dev/null +++ b/common/log/level.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Jan Olszak + * + * 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 +#include + +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 diff --git a/common/log/level.hpp b/common/log/level.hpp index 60d8adf..6a54cda 100644 --- a/common/log/level.hpp +++ b/common/log/level.hpp @@ -25,6 +25,9 @@ #ifndef COMMON_LOG_LEVEL_HPP #define COMMON_LOG_LEVEL_HPP +#include + + 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 - diff --git a/common/log/logger.cpp b/common/log/logger.cpp index 3bb58b3..f834557 100644 --- a/common/log/logger.cpp +++ b/common/log/logger.cpp @@ -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; diff --git a/common/log/logger.hpp b/common/log/logger.hpp index 91a4578..45c5f26 100644 --- a/common/log/logger.hpp +++ b/common/log/logger.hpp @@ -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); diff --git a/container-daemon/main.cpp b/container-daemon/main.cpp index 40e7fd9..769de69 100644 --- a/container-daemon/main.cpp +++ b/container-daemon/main.cpp @@ -36,7 +36,6 @@ #include "exception.hpp" #include "runner.hpp" -#include #include #include @@ -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()); - Logger::setLogLevel(level); + Logger::setLogLevel(vm["log-level"].as()); #ifdef LOG_TO_CONSOLE Logger::setLogBackend(new StderrBackend()); #else diff --git a/server/main.cpp b/server/main.cpp index 2b20823..d911071 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -35,7 +35,6 @@ #include "exception.hpp" #include "server.hpp" -#include #include #include @@ -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()); - Logger::setLogLevel(level); + Logger::setLogLevel(vm["log-level"].as()); #ifdef LOG_TO_CONSOLE Logger::setLogBackend(new StderrBackend()); #else diff --git a/unit_tests/log/ut-logger.cpp b/unit_tests/log/ut-logger.cpp index 66c698d..696bda5 100644 --- a/unit_tests/log/ut-logger.cpp +++ b/unit_tests/log/ut-logger.cpp @@ -29,6 +29,7 @@ #include "log/backend.hpp" #include "log/backend-stderr.hpp" +#include 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); -- 2.7.4