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(),
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 "/";
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,
--- /dev/null
+/*
+ * 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
#ifndef COMMON_LOG_LEVEL_HPP
#define COMMON_LOG_LEVEL_HPP
+#include <string>
+
+
namespace security_containers {
namespace log {
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
-
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;
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);
#include "exception.hpp"
#include "runner.hpp"
-#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <iostream>
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
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
#include "exception.hpp"
#include "server.hpp"
-#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <iostream>
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
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
#include "log/backend.hpp"
#include "log/backend-stderr.hpp"
+#include <stdexcept>
BOOST_AUTO_TEST_SUITE(LogSuite)
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;
}
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);