Implemented command-line option for setting the output log priority.

This commit is contained in:
Patrick Lühne 2016-11-24 23:25:53 +01:00
parent 5816207af7
commit a0cf6e21e4
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
4 changed files with 68 additions and 33 deletions

View File

@ -16,7 +16,8 @@ int main(int argc, char **argv)
("help,h", "Display this help message")
("version,v", "Display version information")
("input,i", po::value<std::vector<std::string>>(), "Input files")
("color", po::value<std::string>()->default_value("auto"), "Whether to colorize the output (always, never, or auto).");
("color,c", po::value<std::string>()->default_value("auto"), "Colorize the output (always, never, or auto).")
("log-priority,p", po::value<std::string>()->default_value("warning"), "Log messages starting from this priority (debug, info, warning, or error).");
po::positional_options_description positionalOptionsDescription;
positionalOptionsDescription.add("input", -1);
@ -60,17 +61,32 @@ int main(int argc, char **argv)
return EXIT_SUCCESS;
}
const auto colorPolicy = variablesMap["color"].as<std::string>();
const auto colorPolicyString = variablesMap["color"].as<std::string>();
if (colorPolicy == "auto")
if (colorPolicyString == "auto")
context.logger.setColorPolicy(anthem::output::ColorStream::ColorPolicy::Auto);
else if (colorPolicy == "never")
else if (colorPolicyString == "never")
context.logger.setColorPolicy(anthem::output::ColorStream::ColorPolicy::Never);
else if (colorPolicy == "always")
else if (colorPolicyString == "always")
context.logger.setColorPolicy(anthem::output::ColorStream::ColorPolicy::Always);
else
{
context.logger.log(anthem::output::Priority::Error, ("unknown color policy “" + colorPolicy + "").c_str());
context.logger.log(anthem::output::Priority::Error, ("unknown color policy “" + colorPolicyString + "").c_str());
context.logger.errorStream() << std::endl;
printHelp();
return EXIT_FAILURE;
}
const auto logPriorityString = variablesMap["log-priority"].as<std::string>();
try
{
const auto logPriority = anthem::output::priorityFromName(logPriorityString.c_str());
context.logger.setLogPriority(logPriority);
}
catch (const std::exception &e)
{
context.logger.log(anthem::output::Priority::Error, ("unknown log priorty “" + logPriorityString + "").c_str());
context.logger.errorStream() << std::endl;
printHelp();
return EXIT_FAILURE;

View File

@ -29,7 +29,7 @@ class Logger
ColorStream &errorStream();
// The level from which on messages should be printed
void setOutputPriority(Priority outputLevel);
void setLogPriority(Priority logPriority);
void setColorPolicy(ColorStream::ColorPolicy colorPolicy);
void log(Priority priority, const char *message);
@ -39,7 +39,7 @@ class Logger
ColorStream m_outputStream;
ColorStream m_errorStream;
Priority m_outputPriority;
Priority m_logPriority;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,6 +1,9 @@
#ifndef __ANTHEM__OUTPUT__PRIORITY_H
#define __ANTHEM__OUTPUT__PRIORITY_H
#include <cstring>
#include <exception>
namespace anthem
{
namespace output
@ -22,6 +25,41 @@ enum class Priority
////////////////////////////////////////////////////////////////////////////////////////////////////
inline constexpr const char *priorityName(Priority priority)
{
switch (priority)
{
case Priority::Debug:
return "debug";
case Priority::Info:
return "info";
case Priority::Warning:
return "warning";
case Priority::Error:
return "error";
}
return "unknown";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline Priority priorityFromName(const char *priorityName)
{
if (std::strcmp(priorityName, "debug") == 0)
return Priority::Debug;
if (std::strcmp(priorityName, "info") == 0)
return Priority::Info;
if (std::strcmp(priorityName, "warning") == 0)
return Priority::Warning;
if (std::strcmp(priorityName, "error") == 0)
return Priority::Error;
throw std::runtime_error("unknown log priority");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -20,7 +20,7 @@ constexpr Format priorityFormat(Priority priority)
case Priority::Debug:
return {Color::Green, FontWeight::Bold};
case Priority::Info:
return {Color::Cyan, FontWeight::Bold};
return {Color::Blue, FontWeight::Bold};
case Priority::Warning:
return {Color::Magenta, FontWeight::Bold};
case Priority::Error:
@ -32,25 +32,6 @@ constexpr Format priorityFormat(Priority priority)
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr const char *priorityName(Priority priority)
{
switch (priority)
{
case Priority::Debug:
return "debug";
case Priority::Info:
return "info";
case Priority::Warning:
return "warning";
case Priority::Error:
return "error";
}
return "message";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr const Format MessageBodyFormat = {Color::White, FontWeight::Bold};
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -76,7 +57,7 @@ Logger::Logger(ColorStream &&outputStream)
Logger::Logger(ColorStream &&outputStream, ColorStream &&errorStream)
: m_outputStream{outputStream},
m_errorStream{errorStream},
m_outputPriority{Priority::Warning}
m_logPriority{Priority::Warning}
{
}
@ -96,9 +77,9 @@ ColorStream &Logger::errorStream()
////////////////////////////////////////////////////////////////////////////////////////////////////
void Logger::setOutputPriority(Priority outputPriority)
void Logger::setLogPriority(Priority logPriority)
{
m_outputPriority = outputPriority;
m_logPriority = logPriority;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -115,7 +96,7 @@ void Logger::log(Priority priority, const char *message)
{
const auto priorityID = static_cast<int>(priority);
if (priorityID < static_cast<int>(m_outputPriority))
if (priorityID < static_cast<int>(m_logPriority))
return;
m_outputStream
@ -131,7 +112,7 @@ void Logger::log(Priority priority, const input::Location &location, const char
{
const auto priorityID = static_cast<int>(priority);
if (priorityID < static_cast<int>(m_outputPriority))
if (priorityID < static_cast<int>(m_logPriority))
return;
auto &stream =