Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[system] Logger levels were not case insensitive #711

Merged
merged 4 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/aliceVision/system/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ alicevision_add_library(aliceVision_system
PUBLIC_INCLUDE_DIRS
${Boost_INCLUDE_DIR}
)

alicevision_add_test(Logger_test.cpp NAME "system_Logger" LINKS aliceVision_system)
43 changes: 43 additions & 0 deletions src/aliceVision/system/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <boost/log/expressions/attr.hpp>
#include <boost/log/expressions/message.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/algorithm/string.hpp>

#if BOOST_VERSION >= 105600
#include <boost/core/null_deleter.hpp>
Expand All @@ -31,6 +32,48 @@
namespace aliceVision {
namespace system {

std::string EVerboseLevel_enumToString(EVerboseLevel verboseLevel)
{
switch(verboseLevel)
{
case EVerboseLevel::Fatal: return "fatal";
case EVerboseLevel::Error: return "error";
case EVerboseLevel::Warning: return "warning";
case EVerboseLevel::Info: return "info";
case EVerboseLevel::Debug: return "debug";
case EVerboseLevel::Trace: return "trace";
}
throw std::out_of_range("Invalid verbose level enum");
}

EVerboseLevel EVerboseLevel_stringToEnum(std::string verboseLevel)
{
boost::to_lower(verboseLevel);

if(verboseLevel == "fatal") return EVerboseLevel::Fatal;
if(verboseLevel == "error") return EVerboseLevel::Error;
if(verboseLevel == "warning") return EVerboseLevel::Warning;
if(verboseLevel == "info") return EVerboseLevel::Info;
if(verboseLevel == "debug") return EVerboseLevel::Debug;
if(verboseLevel == "trace") return EVerboseLevel::Trace;

throw std::out_of_range("Invalid verbose level : '" + verboseLevel + "'");
}

std::ostream& operator<<(std::ostream& os, EVerboseLevel verboseLevel)
{
os << EVerboseLevel_enumToString(verboseLevel);
return os;
}

std::istream& operator>>(std::istream& in, EVerboseLevel& verboseLevel)
{
std::string token;
in >> token;
verboseLevel = EVerboseLevel_stringToEnum(token);
return in;
}

std::shared_ptr<Logger> Logger::_instance = nullptr;

Logger::Logger()
Expand Down
53 changes: 9 additions & 44 deletions src/aliceVision/system/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,56 +60,21 @@ enum class EVerboseLevel

/**
* @brief convert an enum EVerboseLevel to its corresponding string
* @param EVerboseLevel
* @return String
* @param[in] verboseLevel The verbose level.
* @return the string corresponding to the verbose level.
*/
inline std::string EVerboseLevel_enumToString(const EVerboseLevel verboseLevel)
{
switch(verboseLevel)
{
case EVerboseLevel::Fatal: return "fatal";
case EVerboseLevel::Error: return "error";
case EVerboseLevel::Warning: return "warning";
case EVerboseLevel::Info: return "info";
case EVerboseLevel::Debug: return "debug";
case EVerboseLevel::Trace: return "trace";
}
throw std::out_of_range("Invalid verbose level enum");
}
std::string EVerboseLevel_enumToString(EVerboseLevel verboseLevel);

/**
* @brief convert a string verboseLevel to its corresponding enum EVerboseLevel
* @param String
* @return EVerboseLevel
* @brief convert a string to its corresponding enum EVerboseLevel
* @param[in] verboseLevel the string with the verbose level
* @return the corresponding EVerboseLevel
*/
inline EVerboseLevel EVerboseLevel_stringToEnum(const std::string& verboseLevel)
{
std::string level = verboseLevel;
std::transform(level.begin(), level.end(), level.begin(), ::tolower);

if(verboseLevel == "fatal") return EVerboseLevel::Fatal;
if(verboseLevel == "error") return EVerboseLevel::Error;
if(verboseLevel == "warning") return EVerboseLevel::Warning;
if(verboseLevel == "info") return EVerboseLevel::Info;
if(verboseLevel == "debug") return EVerboseLevel::Debug;
if(verboseLevel == "trace") return EVerboseLevel::Trace;

throw std::out_of_range("Invalid verbose level : '" + verboseLevel + "'");
}
EVerboseLevel EVerboseLevel_stringToEnum(std::string verboseLevel);

inline std::ostream& operator<<(std::ostream& os, const EVerboseLevel verboseLevel)
{
os << EVerboseLevel_enumToString(verboseLevel);
return os;
}
std::ostream& operator<<(std::ostream& os, EVerboseLevel verboseLevel);

inline std::istream& operator>>(std::istream& in, EVerboseLevel& verboseLevel)
{
std::string token;
in >> token;
verboseLevel = EVerboseLevel_stringToEnum(token);
return in;
}
std::istream& operator>>(std::istream& in, EVerboseLevel& verboseLevel);

class Logger
{
Expand Down
41 changes: 41 additions & 0 deletions src/aliceVision/system/Logger_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This file is part of the AliceVision project.
// Copyright (c) 2019 AliceVision contributors.
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include <aliceVision/system/Logger.hpp>

#define BOOST_TEST_MODULE Logger
#include <boost/test/included/unit_test.hpp>
#include <boost/algorithm/string.hpp>

#include <array>
#include <exception>

BOOST_AUTO_TEST_CASE(Logger_Enums)
{
using namespace aliceVision::system;
const std::array<EVerboseLevel, 6> allLevels{EVerboseLevel::Trace,
EVerboseLevel::Debug,
EVerboseLevel::Info,
EVerboseLevel::Warning,
EVerboseLevel::Error ,
EVerboseLevel::Fatal};

const std::array<std::string, 6> allStringLevels{"trace",
"debug",
"info",
"warning",
"error" ,
"fatal"};

for(std::size_t i = 0; i < allLevels.size(); ++i)
{
BOOST_CHECK(allLevels[i] == EVerboseLevel_stringToEnum(allStringLevels[i]));
BOOST_CHECK(allLevels[i] == EVerboseLevel_stringToEnum(boost::to_upper_copy(allStringLevels[i])));
BOOST_CHECK(allStringLevels[i] == EVerboseLevel_enumToString(allLevels[i]));
}

BOOST_CHECK_THROW(EVerboseLevel_stringToEnum("not a level"), std::out_of_range);
}