From 9e1cdaaa515a67debf6445481f399c6960234ddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Thu, 1 Dec 2016 17:18:58 +0100 Subject: [PATCH] Implemented new command-line options replacing --warning-level. --- CHANGELOG.md | 2 ++ app/main.cpp | 40 +++++++++++++++++++---------------- include/plasp/output/Logger.h | 5 ++++- src/plasp/output/Logger.cpp | 23 +++++++++++++++++--- 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e2cf08..e3c2c4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ Features: * extended PDDL support (`imply` expressions) * improved command-line interface +* new command-line option `--log-level` to control which status messages should be shown +* new command-line option `--warnings-as-errors` to abort program execution upon warnings (replaces `--warning-level`) ## 3.0.3 (2016-09-02) diff --git a/app/main.cpp b/app/main.cpp index cbd1fc5..e4f3029 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -16,14 +16,17 @@ int main(int argc, char **argv) { namespace po = boost::program_options; + bool warningsAsErrors; + po::options_description description("Allowed options"); description.add_options() ("help,h", "Display this help message") ("version,v", "Display version information") ("input,i", po::value>(), "Input files (in PDDL or SAS format)") ("language,l", po::value()->default_value("auto"), "Input language (pddl, sas, auto)") - ("warning-level", po::value()->default_value("show"), "Show warnings (show), treat them as errors (error), or ignore them (ignore)") - ("color", po::value()->default_value("auto"), "Colorize output (always, never, auto)"); + ("color", po::value()->default_value("auto"), "Colorize output (always, never, auto)") + ("log-priority,p", po::value()->default_value("warning"), "Log messages starting from this priority (debug, info, warning, error)") + ("warnings-as-errors", po::bool_switch(&warningsAsErrors), "Treat warnings as errors"); po::positional_options_description positionalOptionsDescription; positionalOptionsDescription.add("input", -1); @@ -70,22 +73,8 @@ int main(int argc, char **argv) return EXIT_SUCCESS; } - const auto warningLevel = variablesMap["warning-level"].as(); - - // TODO: reimplement - /*if (warningLevel == "error") - logger.setWarningLevel(plasp::output::Logger::WarningLevel::Error); - else if (warningLevel == "ignore") - logger.setWarningLevel(plasp::output::Logger::WarningLevel::Ignore); - else if (warningLevel == "show") - logger.setWarningLevel(plasp::output::Logger::WarningLevel::Show); - else - { - logger.log(plasp::output::Priority::Error, "unknown warning level “" + warningLevel + "”"); - std::cout << std::endl; - printHelp(); - return EXIT_FAILURE; - }*/ + if (warningsAsErrors) + logger.setAbortPriority(plasp::output::Priority::Warning); const auto colorPolicy = variablesMap["color"].as(); @@ -103,6 +92,21 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + const auto logPriorityString = variablesMap["log-priority"].as(); + + try + { + const auto logPriority = plasp::output::priorityFromName(logPriorityString.c_str()); + logger.setLogPriority(logPriority); + } + catch (const std::exception &e) + { + logger.log(plasp::output::Priority::Error, ("unknown log priorty “" + logPriorityString + "”").c_str()); + logger.errorStream() << std::endl; + printHelp(); + return EXIT_FAILURE; + } + try { plasp::input::Parser parser; diff --git a/include/plasp/output/Logger.h b/include/plasp/output/Logger.h index e15be53..8624426 100644 --- a/include/plasp/output/Logger.h +++ b/include/plasp/output/Logger.h @@ -30,8 +30,10 @@ class Logger ColorStream &outputStream(); ColorStream &errorStream(); - // The level from which on messages should be printed + // The priority from which on messages should be printed void setLogPriority(Priority logPriority); + // Messages with this priority (or higher) will terminate the program’s execution + void setAbortPriority(Priority abortPriority); void setColorPolicy(ColorStream::ColorPolicy colorPolicy); void log(Priority priority, const char *message); @@ -44,6 +46,7 @@ class Logger ColorStream m_errorStream; Priority m_logPriority; + Priority m_abortPriority; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/output/Logger.cpp b/src/plasp/output/Logger.cpp index 4b461a4..3cf57f3 100644 --- a/src/plasp/output/Logger.cpp +++ b/src/plasp/output/Logger.cpp @@ -57,7 +57,8 @@ Logger::Logger(ColorStream &&outputStream) Logger::Logger(ColorStream &&outputStream, ColorStream &&errorStream) : m_outputStream{outputStream}, m_errorStream{errorStream}, - m_logPriority{Priority::Warning} + m_logPriority{Priority::Warning}, + m_abortPriority{Priority::Error} { } @@ -66,7 +67,8 @@ Logger::Logger(ColorStream &&outputStream, ColorStream &&errorStream) Logger::Logger(Logger &&other) : m_outputStream{std::move(other.m_outputStream)}, m_errorStream{std::move(other.m_errorStream)}, - m_logPriority{other.m_logPriority} + m_logPriority{other.m_logPriority}, + m_abortPriority{other.m_abortPriority} { } @@ -102,6 +104,13 @@ void Logger::setLogPriority(Priority logPriority) //////////////////////////////////////////////////////////////////////////////////////////////////// +void Logger::setAbortPriority(Priority abortPriority) +{ + m_abortPriority = abortPriority; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + void Logger::setColorPolicy(ColorStream::ColorPolicy colorPolicy) { m_outputStream.setColorPolicy(colorPolicy); @@ -137,8 +146,12 @@ void Logger::log(Priority priority, const input::Location &location, const char { const auto priorityID = static_cast(priority); - if (priorityID < static_cast(m_logPriority)) + // Always show messages that lead to program termination + if (priorityID < static_cast(m_logPriority) && + priorityID < static_cast(m_abortPriority)) + { return; + } m_errorStream << LocationFormat @@ -148,6 +161,10 @@ void Logger::log(Priority priority, const input::Location &location, const char << ResetFormat() << " " << MessageBodyFormat << message << ResetFormat() << std::endl; + + // TODO: print original warning message + if (priorityID >= static_cast(m_abortPriority)) + throw std::runtime_error("received warning (treated as error by configuration)"); } ////////////////////////////////////////////////////////////////////////////////////////////////////