Implemented command-line control of log level
Change-Id: I9d03c0a67856710f8ade5d98cdcdf26dd4277275
diff --git a/src/main.cpp b/src/main.cpp
index 2a0f69a..a82ff82 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -27,6 +27,7 @@
#include <iterator>
#include <stdexcept>
#include <algorithm>
+#include <unordered_map>
#include <boost/program_options.hpp>
#include <boost/program_options/errors.hpp>
@@ -61,9 +62,6 @@
int main(int argc, char *argv[])
{
try {
- LoggingUtil::init();
- logging::add_common_attributes();
-
po::options_description desc("Allowed options");
desc.add_options()
// TODO(msweatt) Consider adding flagged args for other parameters
@@ -71,6 +69,7 @@
("generate,g" , "-g <data directory> <output-path>? <names-per-segment>? <names-per-manifest-segment>? <data-packet-size>?")
("seed,s", "After download completes, continue to seed")
("dump,d", "-d <file> Dump the contents of the Data stored at the <file>.")
+ ("log-level", po::value<std::string>(), "trace | debug | info | warming | error | fatal")
("args", po::value<std::vector<std::string> >(), "For arguments you want to specify without flags")
;
po::positional_options_description p;
@@ -85,6 +84,27 @@
std::cout << desc << std::endl;
return 1;
}
+ if (vm.count("log-level")) {
+ std::unordered_map<std::string, log::severity_level> supported_levels = {
+ {"trace" , log::severity_level::trace},
+ { "debug" , log::severity_level::debug},
+ { "info" , log::severity_level::info},
+ { "warning", log::severity_level::warning},
+ { "error" , log::severity_level::error},
+ { "fatal" , log::severity_level::fatal}
+ };
+ auto log_str = vm["log-level"].as<std::string>();
+ if (supported_levels.count(log_str)) {
+ LoggingUtil::severity_threshold = supported_levels[log_str];
+ }
+ else {
+ throw ndn::Error("Unsupported log level: " + log_str);
+ }
+ }
+ // setup log
+ LoggingUtil::init();
+ logging::add_common_attributes();
+
if (vm.count("args")) {
auto args = vm["args"].as<std::vector<std::string>>();
// if generate mode
diff --git a/src/util/logging.cpp b/src/util/logging.cpp
index b067c80..d26b11d 100644
--- a/src/util/logging.cpp
+++ b/src/util/logging.cpp
@@ -42,11 +42,13 @@
namespace ndn {
namespace ntorrent {
+log::severity_level LoggingUtil::severity_threshold = log::info;
+
void LoggingUtil::init()
{
logging::core::get()->set_filter
(
- logging::trivial::severity >= SEVERITY_THRESHOLD
+ logging::trivial::severity >= severity_threshold
);
logging::add_file_log
diff --git a/src/util/logging.hpp b/src/util/logging.hpp
index d10e926..e6a2b4b 100644
--- a/src/util/logging.hpp
+++ b/src/util/logging.hpp
@@ -28,8 +28,6 @@
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/trivial.hpp>
-enum { SEVERITY_THRESHOLD = boost::log::trivial::debug };
-
// register a global logger
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(logger, boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level>)
@@ -47,7 +45,11 @@
namespace ndn {
namespace ntorrent {
+namespace log = boost::log::trivial;
+
struct LoggingUtil {
+ static log::severity_level severity_threshold;
+
static void init();
// Initialize the log for the application. THis method must be called in the main function in
// the application before any logging may be performed.