Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed Feature: NO_COLOR console or logfile output #878

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CONFIGURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ There is a list of available Plugins [here](./Plugins.md).
| consoleLog | | true | **true** / **false** | Send logging output to the console |
| logFile | | false | **true** / **false** | Send logging output to a file |
| logDir | | logs/ | string | Where the output logs should be put |
| logColor | | "console" (*or* "none" if `NO_COLOR` env set) | **"all"**, **"console"**, **"logfile"**, **"none"** | Control the output of ANSI color in the console or logfiles. The presence of the [`NO_COLOR` env variable](https://no-color.org) will modify the default if set (`export NO_COLOR=1`). |
| frequencyFormat | | "exp" | **"exp" "mhz"** or **"hz"** | the display format for frequencies to display in the console and log file. |
| controlWarnRate | | 10 | number | Log the control channel decode rate when it falls bellow this threshold. The value of *-1* will always log the decode rate. |
| controlRetuneLimit | | 0 | number | Number of times to attempt to retune to a different control channel when there's no signal. *0* means unlimited attemps. The counter is reset when a signal is found. Should be at least equal to the number of channels defined in order for all to be attempted. |
Expand Down
67 changes: 55 additions & 12 deletions trunk-recorder/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,53 @@ void set_logging_level(std::string log_level) {
boost::log::trivial::severity >= sev_level);
}

template <class F>
void add_logs(const F &fmt) {
boost::shared_ptr<sinks::synchronous_sink<sinks::basic_text_ostream_backend<char>>> sink =
boost::log::add_console_log(std::clog, boost::log::keywords::format = fmt);
struct NoColorLoggingFormatter {
void operator()(logging::record_view const& rec, logging::formatting_ostream& strm) const {
auto message = rec.attribute_values()[logging::aux::default_attribute_names::message()];
auto message_str = message.extract<std::string>().get();

std::regex escape_seq_regex("\u001B\\[[0-9;]+m");
strm << std::regex_replace(message_str, escape_seq_regex, "");
}
};

void setup_console_log(std::string log_color, std::string time_fmt) {
boost::shared_ptr<sinks::synchronous_sink<sinks::basic_text_ostream_backend<char>>> console_sink = logging::add_console_log(std::clog);

if ((log_color == "console" ) || (log_color == "all")) {
console_sink->set_formatter(logging::expressions::format("[%1%] (%2%) %3%") %
logging::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", time_fmt) %
logging::expressions::attr<logging::trivial::severity_level>("Severity") %
logging::expressions::smessage);
} else {
console_sink->set_formatter(logging::expressions::format("[%1%] (%2%) %3%") %
logging::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", time_fmt) %
logging::expressions::attr<logging::trivial::severity_level>("Severity") %
logging::expressions::wrap_formatter(NoColorLoggingFormatter{}));
}

std::locale loc = std::locale("C");
console_sink->imbue(loc);
}

sink->imbue(loc);
void setup_file_log(std::string log_dir, std::string log_color, std::string time_fmt) {
boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> log_sink = logging::add_file_log(
keywords::file_name = log_dir + "/%m-%d-%Y_%H%M_%2N.log",
keywords::rotation_size = 100 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::auto_flush = true);

if ((log_color == "logfile" ) || (log_color == "all")) {
log_sink->set_formatter(logging::expressions::format("[%1%] (%2%) %3%") %
logging::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", time_fmt) %
logging::expressions::attr<logging::trivial::severity_level>("Severity") %
logging::expressions::smessage);
} else {
log_sink->set_formatter(logging::expressions::format("[%1%] (%2%) %3%") %
logging::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", time_fmt) %
logging::expressions::attr<logging::trivial::severity_level>("Severity") %
logging::expressions::wrap_formatter(NoColorLoggingFormatter{}));
}
}

bool load_config(string config_file, Config &config, gr::top_block_sptr &tb, std::vector<Source *> &sources, std::vector<System *> &systems) {
Expand All @@ -72,11 +111,19 @@ bool load_config(string config_file, Config &config, gr::top_block_sptr &tb, std
boost::property_tree::ptree pt;
boost::property_tree::read_json(config_file, pt);

// Set NO_COLOR options for console and log files
// Default is [color console, no_color logfile] unless NO_COLOR env var is set. config.json overrides NO_COLOR use.
char *no_color = getenv("NO_COLOR");
bool color = true;

if (no_color != NULL && no_color[0] != '\0')
color = false;

config.log_color = data.value("logColor", (color ? "console" : "none"));

config.console_log = data.value("consoleLog", true);
if (config.console_log) {
add_logs(boost::log::expressions::format("[%1%] (%2%) %3%") % boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") % boost::log::expressions::attr<boost::log::trivial::severity_level>("Severity") % boost::log::expressions::smessage);
setup_console_log(config.log_color, "%Y-%m-%d %H:%M:%S.%f");
}

BOOST_LOG_TRIVIAL(info) << "\n-------------------------------------\n Trunk Recorder\n-------------------------------------\n";
Expand All @@ -86,12 +133,7 @@ bool load_config(string config_file, Config &config, gr::top_block_sptr &tb, std
config.log_file = data.value("logFile", false);
config.log_dir = data.value("logDir", "logs");
if (config.log_file) {
logging::add_file_log(
keywords::file_name = config.log_dir + "/%m-%d-%Y_%H%M_%2N.log",
keywords::format = "[%TimeStamp%] (%Severity%) %Message%",
keywords::rotation_size = 100 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::auto_flush = true);
setup_file_log(config.log_dir, config.log_color, "%Y-%m-%d %H:%M:%S.%f");
}

double config_ver = data.value("ver", 0.0);
Expand Down Expand Up @@ -177,6 +219,7 @@ bool load_config(string config_file, Config &config, gr::top_block_sptr &tb, std
std::string log_level = data.value("logLevel", "info");
BOOST_LOG_TRIVIAL(info) << "Log Level: " << log_level;
set_logging_level(log_level);
BOOST_LOG_TRIVIAL(info) << "Color Console/Logfile Output: " << config.log_color;

config.debug_recorder = data.value("debugRecorder", 0);
config.debug_recorder_address = data.value("debugRecorderAddress", "127.0.0.1");
Expand Down
1 change: 1 addition & 0 deletions trunk-recorder/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <gnuradio/top_block.h>

#include <fstream>
#include <regex>

#include "cmake.h"
#include "git.h"
Expand Down
1 change: 1 addition & 0 deletions trunk-recorder/global_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct Config {
int call_timeout;
bool console_log;
bool log_file;
std::string log_color;
int control_message_warn_rate;
int control_retune_limit;
bool broadcast_signals;
Expand Down
Loading