Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Log: add colored severity levels
Browse files Browse the repository at this point in the history
also adds the option --log-disable-color
for consoles that do not support this.
  • Loading branch information
selsta committed Sep 27, 2017
1 parent b27b7ed commit cf62086
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
11 changes: 11 additions & 0 deletions pkg/config/kovri.conf
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ log-level = 3

log-auto-flush = 0

#
# Disable console log color
# ==========================
#
# 1 = no color, 0 = color
#
# Default: 0
#

log-disable-color = 0

#
# Tunnels Config
# ==============
Expand Down
2 changes: 2 additions & 0 deletions src/app/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ void Configuration::ParseKovriConfig() {
bpo::value<std::uint16_t>()->default_value(3))(
"log-auto-flush",
bpo::value<bool>()->default_value(false)->value_name("bool"))(
"log-disable-color",
bpo::value<bool>()->default_value(false)->value_name("bool"))(
"kovriconf,c",
bpo::value<std::string>()->default_value("")->value_name("path"))(
"tunnelsconf,t",
Expand Down
73 changes: 57 additions & 16 deletions src/core/util/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/make_shared.hpp>
#include <boost/phoenix/bind.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/shared_ptr.hpp>
Expand All @@ -64,12 +65,62 @@ namespace kovri
{
namespace core
{
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace attrs = boost::log::attributes;

std::string SetColor(
logging::value_ref<
logging::trivial::severity_level,
logging::trivial::tag::severity> const& severity,
bool has_color)
{
if (has_color)
{
if (severity)
{
switch (severity.get())
{
case logging::trivial::fatal:
case logging::trivial::error:
return "\033[31m"; // Red
case logging::trivial::info:
return "\033[32m"; // Green
case logging::trivial::warning:
return "\033[33m"; // Yellow
case logging::trivial::debug:
return "\033[34m"; // Blue
case logging::trivial::trace:
return "\033[36m"; // Cyan
}
}
return "\033[0m"; // Reset
}
return "";
}

logging::formatter GetFormat(bool has_color)
{
logging::formatter format =
expr::stream
<< boost::phoenix::bind(
&SetColor, logging::trivial::severity.or_none(), has_color)
<< "["
<< expr::format_date_time(
expr::attr<boost::posix_time::ptime>("TimeStamp"),
"%Y.%m.%d %T.%f")
<< "]"
<< " [" << expr::attr<attrs::current_thread_id::value_type>("ThreadID")
<< "]"
<< " [" << logging::trivial::severity << "]"
<< " " << expr::smessage
<< (has_color == true ? "\033[0m" : ""); // Reset
return format;
}

void SetupLogging(const boost::program_options::variables_map& kovri_config)
{
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace keywords = boost::log::keywords;
// Get global logger
// TODO(unassigned): depends on global logging initialization. See notes in log impl
Expand Down Expand Up @@ -131,19 +182,9 @@ void SetupLogging(const boost::program_options::variables_map& kovri_config)
auto file_sink = boost::shared_ptr<text_file_sink>(
std::make_unique<text_file_sink>(file_backend));
// Set sink formatting
logging::formatter format =
expr::stream << "["
<< expr::format_date_time(
expr::attr<boost::posix_time::ptime>("TimeStamp"),
"%Y.%m.%d %T.%f")
<< "]"
<< " [" << expr::attr<attrs::current_thread_id::value_type>(
"ThreadID")
<< "]"
<< " [" << logging::trivial::severity << "]"
<< " " << expr::smessage;
text_sink->set_formatter(format);
file_sink->set_formatter(format);
text_sink->set_formatter(
GetFormat(true && !kovri_config["log-disable-color"].as<bool>()));
file_sink->set_formatter(GetFormat(false));
// Add sinks
core->add_sink(text_sink);
core->add_sink(file_sink);
Expand Down

0 comments on commit cf62086

Please sign in to comment.