Add --verbosity,-l command line option

Allow to fine-control logging verbosity via command line
and LogPolicy setting (useful when OSRM used as library).

Closes #4299
This commit is contained in:
Mateusz Loskot
2017-08-28 21:20:13 +02:00
committed by Patrick Niklaus
parent fb5bd818d9
commit 7323221e3b
13 changed files with 144 additions and 38 deletions
+12 -4
View File
@@ -28,11 +28,17 @@ enum class return_code : unsigned
exit
};
return_code parseArguments(int argc, char *argv[], contractor::ContractorConfig &contractor_config)
return_code parseArguments(int argc,
char *argv[],
std::string &verbosity,
contractor::ContractorConfig &contractor_config)
{
// declare a group of options that will be allowed only on command line
boost::program_options::options_description generic_options("Options");
generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message");
generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message")(
"verbosity,l",
boost::program_options::value<std::string>(&verbosity)->default_value("INFO"),
std::string("Log verbosity level: " + util::LogPolicy::GetLevels()).c_str());
// declare a group of options that will be allowed on command line
boost::program_options::options_description config_options("Configuration");
@@ -137,11 +143,11 @@ return_code parseArguments(int argc, char *argv[], contractor::ContractorConfig
int main(int argc, char *argv[]) try
{
util::LogPolicy::GetInstance().Unmute();
std::string verbosity;
contractor::ContractorConfig contractor_config;
const return_code result = parseArguments(argc, argv, contractor_config);
const return_code result = parseArguments(argc, argv, verbosity, contractor_config);
if (return_code::fail == result)
{
@@ -153,6 +159,8 @@ int main(int argc, char *argv[]) try
return EXIT_SUCCESS;
}
util::LogPolicy::GetInstance().SetLevel(verbosity);
contractor_config.UseDefaultOutputNames(contractor_config.base_path);
if (1 > contractor_config.requested_num_threads)
+12 -4
View File
@@ -21,12 +21,17 @@ enum class return_code : unsigned
exit
};
return_code
parseArguments(int argc, char *argv[], customizer::CustomizationConfig &customization_config)
return_code parseArguments(int argc,
char *argv[],
std::string &verbosity,
customizer::CustomizationConfig &customization_config)
{
// declare a group of options that will be allowed only on command line
boost::program_options::options_description generic_options("Options");
generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message");
generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message")(
"verbosity,l",
boost::program_options::value<std::string>(&verbosity)->default_value("INFO"),
std::string("Log verbosity level: " + util::LogPolicy::GetLevels()).c_str());
// declare a group of options that will be allowed both on command line
boost::program_options::options_description config_options("Configuration");
@@ -130,9 +135,10 @@ parseArguments(int argc, char *argv[], customizer::CustomizationConfig &customiz
int main(int argc, char *argv[]) try
{
util::LogPolicy::GetInstance().Unmute();
std::string verbosity;
customizer::CustomizationConfig customization_config;
const auto result = parseArguments(argc, argv, customization_config);
const auto result = parseArguments(argc, argv, verbosity, customization_config);
if (return_code::fail == result)
{
@@ -144,6 +150,8 @@ int main(int argc, char *argv[]) try
return EXIT_SUCCESS;
}
util::LogPolicy::GetInstance().SetLevel(verbosity);
// set the default in/output names
customization_config.UseDefaultOutputNames(customization_config.base_path);
+13 -4
View File
@@ -24,11 +24,17 @@ enum class return_code : unsigned
exit
};
return_code parseArguments(int argc, char *argv[], extractor::ExtractorConfig &extractor_config)
return_code parseArguments(int argc,
char *argv[],
std::string &verbosity,
extractor::ExtractorConfig &extractor_config)
{
// declare a group of options that will be allowed only on command line
// declare a group of options that will be a llowed only on command line
boost::program_options::options_description generic_options("Options");
generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message");
generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message")(
"verbosity,l",
boost::program_options::value<std::string>(&verbosity)->default_value("INFO"),
std::string("Log verbosity level: " + util::LogPolicy::GetLevels()).c_str());
// declare a group of options that will be allowed both on command line
boost::program_options::options_description config_options("Configuration");
@@ -127,8 +133,9 @@ int main(int argc, char *argv[]) try
{
util::LogPolicy::GetInstance().Unmute();
extractor::ExtractorConfig extractor_config;
std::string verbosity;
const auto result = parseArguments(argc, argv, extractor_config);
const auto result = parseArguments(argc, argv, verbosity, extractor_config);
if (return_code::fail == result)
{
@@ -140,6 +147,8 @@ int main(int argc, char *argv[]) try
return EXIT_SUCCESS;
}
util::LogPolicy::GetInstance().SetLevel(verbosity);
extractor_config.UseDefaultOutputNames(extractor_config.input_path);
if (1 > extractor_config.requested_num_threads)
+10 -3
View File
@@ -69,11 +69,15 @@ void validate(boost::any &v, const std::vector<std::string> &values, MaxCellSize
v = boost::any(MaxCellSizesArgument{output});
}
return_code parseArguments(int argc, char *argv[], partition::PartitionConfig &config)
return_code
parseArguments(int argc, char *argv[], std::string &verbosity, partition::PartitionConfig &config)
{
// declare a group of options that will be allowed only on command line
boost::program_options::options_description generic_options("Options");
generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message");
generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message")(
"verbosity,l",
boost::program_options::value<std::string>(&verbosity)->default_value("INFO"),
std::string("Log verbosity level: " + util::LogPolicy::GetLevels()).c_str());
// declare a group of options that will be allowed both on command line
boost::program_options::options_description config_options("Configuration");
@@ -184,9 +188,10 @@ return_code parseArguments(int argc, char *argv[], partition::PartitionConfig &c
int main(int argc, char *argv[]) try
{
util::LogPolicy::GetInstance().Unmute();
std::string verbosity;
partition::PartitionConfig partition_config;
const auto result = parseArguments(argc, argv, partition_config);
const auto result = parseArguments(argc, argv, verbosity, partition_config);
if (return_code::fail == result)
{
@@ -198,6 +203,8 @@ int main(int argc, char *argv[]) try
return EXIT_SUCCESS;
}
util::LogPolicy::GetInstance().SetLevel(verbosity);
// set the default in/output names
partition_config.UseDefaultOutputNames(partition_config.base_path);
+12 -3
View File
@@ -68,6 +68,7 @@ static EngineConfig::Algorithm stringToAlgorithm(std::string algorithm)
// generate boost::program_options object for the routing part
inline unsigned generateServerProgramOptions(const int argc,
const char *argv[],
std::string verbosity,
boost::filesystem::path &base_path,
std::string &ip_address,
int &ip_port,
@@ -87,9 +88,12 @@ inline unsigned generateServerProgramOptions(const int argc,
// declare a group of options that will be allowed only on command line
boost::program_options::options_description generic_options("Options");
generic_options.add_options() //
("version,v", "Show version")("help,h", "Show this help message") //
("trial", value<bool>(&trial)->implicit_value(true), "Quit after initialization");
generic_options.add_options() //
("version,v", "Show version")("help,h", "Show this help message")(
"verbosity,l",
boost::program_options::value<std::string>(&verbosity)->default_value("INFO"),
std::string("Log verbosity level: " + util::LogPolicy::GetLevels()).c_str())(
"trial", value<bool>(&trial)->implicit_value(true), "Quit after initialization");
// declare a group of options that will be allowed on command line
boost::program_options::options_description config_options("Configuration");
@@ -202,10 +206,12 @@ int main(int argc, const char *argv[]) try
int ip_port, requested_thread_num;
EngineConfig config;
std::string verbosity;
boost::filesystem::path base_path;
std::string algorithm;
const unsigned init_result = generateServerProgramOptions(argc,
argv,
verbosity,
base_path,
ip_address,
ip_port,
@@ -227,6 +233,9 @@ int main(int argc, const char *argv[]) try
{
return EXIT_FAILURE;
}
util::LogPolicy::GetInstance().SetLevel(verbosity);
if (!base_path.empty())
{
config.storage_config = storage::StorageConfig(base_path);
+9 -1
View File
@@ -52,12 +52,16 @@ void springClean()
// generate boost::program_options object for the routing part
bool generateDataStoreOptions(const int argc,
const char *argv[],
std::string &verbosity,
boost::filesystem::path &base_path,
int &max_wait)
{
// declare a group of options that will be allowed only on command line
boost::program_options::options_description generic_options("Options");
generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message")(
"verbosity,l",
boost::program_options::value<std::string>(&verbosity)->default_value("INFO"),
std::string("Log verbosity level: " + util::LogPolicy::GetLevels()).c_str())(
"remove-locks,r", "Remove locks")("spring-clean,s",
"Spring-cleaning all shared memory regions");
@@ -157,12 +161,16 @@ int main(const int argc, const char *argv[]) try
util::LogPolicy::GetInstance().Unmute();
std::string verbosity;
boost::filesystem::path base_path;
int max_wait = -1;
if (!generateDataStoreOptions(argc, argv, base_path, max_wait))
if (!generateDataStoreOptions(argc, argv, verbosity, base_path, max_wait))
{
return EXIT_SUCCESS;
}
util::LogPolicy::GetInstance().SetLevel(verbosity);
storage::StorageConfig config(base_path);
if (!config.IsValid())
{
+51 -16
View File
@@ -1,5 +1,6 @@
#include "util/log.hpp"
#include "util/isatty.hpp"
#include <boost/algorithm/string/predicate.hpp>
#include <cstdio>
#include <iostream>
#include <mutex>
@@ -29,32 +30,64 @@ void LogPolicy::Mute() { m_is_mute = true; }
bool LogPolicy::IsMute() const { return m_is_mute; }
LogLevel LogPolicy::GetLevel() const { return m_level; }
void LogPolicy::SetLevel(LogLevel level) { m_level = level; }
void LogPolicy::SetLevel(std::string const &level)
{
// Keep in sync with LogLevel definition
if (boost::iequals(level, "NONE"))
m_level = logNONE;
else if (boost::iequals(level, "ERROR"))
m_level = logERROR;
else if (boost::iequals(level, "WARNING"))
m_level = logWARNING;
else if (boost::iequals(level, "INFO"))
m_level = logINFO;
else if (boost::iequals(level, "DEBUG"))
m_level = logDEBUG;
else
;
}
LogPolicy &LogPolicy::GetInstance()
{
static LogPolicy runningInstance;
return runningInstance;
}
std::string LogPolicy::GetLevels()
{
// Keep in sync with LogLevel definition
return "NONE, ERROR, WARNING, INFO, DEBUG";
}
Log::Log(LogLevel level_, std::ostream &ostream) : level(level_), stream(ostream)
{
const bool is_terminal = IsStdoutATTY();
std::lock_guard<std::mutex> lock(get_mutex());
switch (level)
if (!LogPolicy::GetInstance().IsMute() && level <= LogPolicy::GetInstance().GetLevel())
{
case logWARNING:
stream << (is_terminal ? YELLOW : "") << "[warn] ";
break;
case logERROR:
stream << (is_terminal ? RED : "") << "[error] ";
break;
case logDEBUG:
const bool is_terminal = IsStdoutATTY();
switch (level)
{
case logNONE:
break;
case logWARNING:
stream << (is_terminal ? YELLOW : "") << "[warn] ";
break;
case logERROR:
stream << (is_terminal ? RED : "") << "[error] ";
break;
case logDEBUG:
#ifndef NDEBUG
stream << (is_terminal ? MAGENTA : "") << "[debug] ";
stream << (is_terminal ? MAGENTA : "") << "[debug] ";
#endif
break;
default: // logINFO:
stream << "[info] ";
break;
break;
default: // logINFO:
stream << "[info] ";
break;
}
}
}
@@ -76,14 +109,16 @@ std::mutex &Log::get_mutex()
Log::~Log()
{
std::lock_guard<std::mutex> lock(get_mutex());
const bool usestd = (&stream == &buffer);
if (!LogPolicy::GetInstance().IsMute())
if (!LogPolicy::GetInstance().IsMute() && level <= LogPolicy::GetInstance().GetLevel())
{
const bool usestd = (&stream == &buffer);
const bool is_terminal = IsStdoutATTY();
if (usestd)
{
switch (level)
{
case logNONE:
break;
case logWARNING:
case logERROR:
std::cerr << buffer.str();