From 5cd91c64da78c2b685d01957bc804ada9fc33471 Mon Sep 17 00:00:00 2001 From: Steven Murray <steven.murray@cern.ch> Date: Wed, 9 Nov 2016 17:39:08 +0100 Subject: [PATCH] Added cta::log::toLogLevel() --- common/CMakeLists.txt | 2 + common/log/LogLevel.cpp | 46 +++++++++++++++++++ common/log/LogLevel.hpp | 34 ++++++++++++++ common/log/LogLevelTest.cpp | 91 +++++++++++++++++++++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 common/log/LogLevel.cpp create mode 100644 common/log/LogLevel.hpp create mode 100644 common/log/LogLevelTest.cpp diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index d34538f275..eed4ecc112 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -90,6 +90,7 @@ set (COMMON_LIB_SRC_FILES log/DummyLogger.cpp log/LogContext.cpp log/Logger.cpp + log/LogLevel.cpp log/Message.cpp log/Param.cpp log/StringLogger.cpp @@ -143,6 +144,7 @@ set (COMMON_UNIT_TESTS_LIB_SRC_FILES dataStructures/StorageClassTest.cpp processCap/SmartCapTest.cpp log/LogContextTest.cpp + log/LogLevelTest.cpp log/ParamTest.cpp log/SyslogLoggerTest.cpp log/StringLoggerTest.cpp diff --git a/common/log/LogLevel.cpp b/common/log/LogLevel.cpp new file mode 100644 index 0000000000..9f9f847480 --- /dev/null +++ b/common/log/LogLevel.cpp @@ -0,0 +1,46 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2015 CERN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "common/exception/Exception.hpp" +#include "common/log/Constants.hpp" + +namespace cta { +namespace log { + +//------------------------------------------------------------------------------ +// toLogLevel +//------------------------------------------------------------------------------ +int toLogLevel(const std::string &s) { + if(s == "EMERG") return EMERG; + if(s == "ALERT") return ALERT; + if(s == "CRIT") return CRIT; + if(s == "ERR") return ERR; + if(s == "WARNING") return WARNING; + if(s == "NOTICE") return NOTICE; + if(s == "INFO") return INFO; + if(s == "DEBUG") return DEBUG; + + // It is a convention of CTA to use syslog level of LOG_NOTICE to label + // user errors. + if(s == "USERERR") return NOTICE; + + throw exception::Exception(s + " is not a valid log level"); +} + +} // namespace log +} // namespace cta diff --git a/common/log/LogLevel.hpp b/common/log/LogLevel.hpp new file mode 100644 index 0000000000..e57e8e4832 --- /dev/null +++ b/common/log/LogLevel.hpp @@ -0,0 +1,34 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2015 CERN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "common/log/Constants.hpp" + +#include <string> + +namespace cta { +namespace log { + +/** + * Returns the numeric value of the specified log level. + */ +int toLogLevel(const std::string &s); + +} // namespace log +} // namespace cta diff --git a/common/log/LogLevelTest.cpp b/common/log/LogLevelTest.cpp new file mode 100644 index 0000000000..ffc8151427 --- /dev/null +++ b/common/log/LogLevelTest.cpp @@ -0,0 +1,91 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2015 CERN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "common/log/LogLevel.hpp" + +#include <gtest/gtest.h> + +namespace unitTests { + +class cta_log_LogLevelTest : public ::testing::Test { +protected: + + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; // class cta_log_LogLevelTest + +TEST_F(cta_log_LogLevelTest, toLogLevel_EMERG) { + using namespace cta::log; + + ASSERT_EQ(EMERG, toLogLevel("EMERG")); +} + +TEST_F(cta_log_LogLevelTest, toLogLevel_ALERT) { + using namespace cta::log; + + ASSERT_EQ(ALERT, toLogLevel("ALERT")); +} + +TEST_F(cta_log_LogLevelTest, toLogLevel_CRIT) { + using namespace cta::log; + + ASSERT_EQ(CRIT, toLogLevel("CRIT")); +} + +TEST_F(cta_log_LogLevelTest, toLogLevel_ERR) { + using namespace cta::log; + + ASSERT_EQ(ERR, toLogLevel("ERR")); +} + +TEST_F(cta_log_LogLevelTest, toLogLevel_WARNING) { + using namespace cta::log; + + ASSERT_EQ(WARNING, toLogLevel("WARNING")); +} + +TEST_F(cta_log_LogLevelTest, toLogLevel_NOTICE) { + using namespace cta::log; + + ASSERT_EQ(NOTICE, toLogLevel("NOTICE")); +} + +TEST_F(cta_log_LogLevelTest, toLogLevel_INFO) { + using namespace cta::log; + + ASSERT_EQ(INFO, toLogLevel("INFO")); +} + +TEST_F(cta_log_LogLevelTest, toLogLevel_DEBUG) { + using namespace cta::log; + + ASSERT_EQ(DEBUG, toLogLevel("DEBUG")); +} + +TEST_F(cta_log_LogLevelTest, toLogLevel_USERERR) { + using namespace cta::log; + + // It is a convention of CTA to use syslog level of LOG_NOTICE to label + // user errors. + ASSERT_EQ(NOTICE, toLogLevel("USERERR")); +} + +} // namespace unitTests -- GitLab