Commit 324d1b7c authored by Dominik Charousset's avatar Dominik Charousset

Use new get_or API for logger setup

parent 3f470f6f
......@@ -323,13 +323,13 @@ public:
// -- logger parameters ------------------------------------------------------
std::string logger_file_name;
std::string logger_file_format;
atom_value logger_console;
std::string logger_console_format;
std::string logger_component_filter;
atom_value logger_verbosity;
bool logger_inline_output;
std::string logger_file_name CAF_DEPRECATED;
std::string logger_file_format CAF_DEPRECATED;
atom_value logger_console CAF_DEPRECATED;
std::string logger_console_format CAF_DEPRECATED;
std::string logger_component_filter CAF_DEPRECATED;
atom_value logger_verbosity CAF_DEPRECATED;
bool logger_inline_output CAF_DEPRECATED;
// -- middleman parameters ---------------------------------------------------
......
......@@ -60,11 +60,12 @@ extern const timespan relaxed_sleep_duration;
namespace logger {
extern const char* file_name;
extern const char* file_format;
extern const atom_value console;
extern const char* console_format;
extern const atom_value verbosity;
extern const char* component_filter;
extern const char* console_format;
extern const char* file_format;
extern const char* file_name;
} // namespace logger
......
......@@ -155,8 +155,7 @@ public:
/// Represents a single format string field.
struct field {
field_type kind;
const char* first;
const char* last;
std::string text;
};
/// Stores a parsed format string as list of fields.
......@@ -243,7 +242,7 @@ public:
/// Parses `format_str` into a format description vector.
/// @warning The returned vector can have pointers into `format_str`.
static line_format parse_format(const char* format_str);
static line_format parse_format(const std::string& format_str);
/// Skips path in `filename`.
static const char* skip_path(const char* filename);
......@@ -271,6 +270,18 @@ public:
return "[]";
}
// -- individual flags -------------------------------------------------------
static constexpr int inline_output_flag = 0x01;
static constexpr int uncolored_console_flag = 0x02;
static constexpr int colored_console_flag = 0x04;
// -- composed flags ---------------------------------------------------------
static constexpr int console_output_flag = 0x06;
private:
void handle_event(event& x);
......@@ -288,9 +299,17 @@ private:
void stop();
inline bool has(int flag) const noexcept {
return (flags_ & flag) != 0;
}
inline void set(int flag) noexcept {
flags_ |= flag;
}
actor_system& system_;
int level_;
bool inline_output_;
int flags_;
detail::shared_spinlock aids_lock_;
std::unordered_map<std::thread::id, actor_id> aids_;
std::thread thread_;
......@@ -302,6 +321,7 @@ private:
line_format file_format_;
line_format console_format_;
std::fstream file_;
std::string component_filter;
};
std::string to_string(logger::field_type x);
......
......@@ -149,7 +149,7 @@ actor_system_config::actor_system_config()
.add(logger_file_name, "filename",
"deprecated (use file-name instead)")
.add(logger_component_filter, "filter",
"deprecated (use console-component-filter instead)");
"deprecated (use component-filter instead)");
opt_group{custom_options_, "middleman"}
.add(middleman_network_backend, "network-backend",
"sets the network backend to either 'default' or 'asio' (if available)")
......
......@@ -72,11 +72,12 @@ const timespan relaxed_sleep_duration = ms(10);
namespace logger {
const char* file_name = "actor_log_[PID]_[TIMESTAMP]_[NODE].log";
const char* file_format = "%r %c %p %a %t %C %M %F:%L %m%n";
const atom_value console = atom("none");
const char* console_format = "%m";
const atom_value verbosity = atom("trace");
const char* component_filter = "";
const char* console_format = "%m";
const char* file_format = "%r %c %p %a %t %C %M %F:%L %m%n";
const char* file_name = "actor_log_[PID]_[TIMESTAMP]_[NODE].log";
} // namespace logger
......
......@@ -31,17 +31,16 @@
#include "caf/actor_proxy.hpp"
#include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp"
#include "caf/defaults.hpp"
#include "caf/detail/get_process_id.hpp"
#include "caf/detail/pretty_type_name.hpp"
#include "caf/intrusive/task_result.hpp"
#include "caf/local_actor.hpp"
#include "caf/locks.hpp"
#include "caf/string_algorithms.hpp"
#include "caf/term.hpp"
#include "caf/timestamp.hpp"
#include "caf/intrusive/task_result.hpp"
#include "caf/detail/get_process_id.hpp"
#include "caf/detail/pretty_type_name.hpp"
namespace caf {
namespace {
......@@ -194,11 +193,11 @@ actor_id logger::thread_local_aid(actor_id aid) {
void logger::log(event* x) {
CAF_ASSERT(x->level >= 0 && x->level <= 4);
if (!inline_output_) {
queue_.synchronized_push_back(queue_mtx_, queue_cv_,x);
} else {
if (has(inline_output_flag)) {
std::unique_ptr<event> ptr{x};
handle_event(*ptr);
} else {
queue_.synchronized_push_back(queue_mtx_, queue_cv_,x);
}
}
......@@ -219,10 +218,10 @@ bool logger::accepts(int level, const char* cname_begin,
CAF_ASSERT(level >= 0 && level <= 4);
if (level > level_)
return false;
auto& filter = system_.config().logger_component_filter;
if (!filter.empty()) {
auto it = std::search(filter.begin(), filter.end(), cname_begin, cname_end);
return it != filter.end();
if (!component_filter.empty()) {
auto it = std::search(component_filter.begin(), component_filter.end(),
cname_begin, cname_end);
return it != component_filter.end();
}
return true;
}
......@@ -237,7 +236,7 @@ logger::~logger() {
logger::logger(actor_system& sys)
: system_(sys),
inline_output_(false),
flags_(0),
queue_(policy{}) {
// nop
}
......@@ -245,9 +244,12 @@ logger::logger(actor_system& sys)
void logger::init(actor_system_config& cfg) {
CAF_IGNORE_UNUSED(cfg);
#if defined(CAF_LOG_LEVEL)
inline_output_ = cfg.logger_inline_output;
namespace lg = defaults::logger;
component_filter = get_or(cfg, "logger.component-filter",
lg::component_filter);
// Parse the configured log level.
switch (static_cast<uint64_t>(cfg.logger_verbosity)) {
auto verbosity = get_or(cfg, "logger.verbosity", lg::verbosity);
switch (static_cast<uint64_t>(verbosity)) {
case atom_uint("quiet"):
case atom_uint("QUIET"):
level_ = CAF_LOG_LEVEL_QUIET;
......@@ -277,8 +279,18 @@ void logger::init(actor_system_config& cfg) {
}
}
// Parse the format string.
file_format_ = parse_format(cfg.logger_file_format.c_str());
console_format_ = parse_format(cfg.logger_console_format.c_str());
file_format_ = parse_format(get_or(cfg, "logger.file-format",
lg::file_format));
console_format_ = parse_format(get_or(cfg,"logger.console-format",
lg::console_format));
// Set flags.
if (get_or(cfg, "logger.inline-output", false))
set(inline_output_flag);
auto con_atm = get_or(cfg, "logger.console", lg::console);
if (con_atm == atom("UNCOLORED"))
set(uncolored_console_flag);
else if (con_atm == atom("COLORED"))
set(colored_console_flag);
#endif
}
......@@ -364,17 +376,17 @@ void logger::render(std::ostream& out, const line_format& lf,
case thread_field: out << x.tid; break;
case actor_field: out << "actor" << x.aid; break;
case percent_sign_field: out << '%'; break;
case plain_text_field: out.write(f.first, f.last - f.first); break;
case plain_text_field: out << f.text; break;
default: ; // nop
}
}
logger::line_format logger::parse_format(const char* format_str) {
logger::line_format logger::parse_format(const std::string& format_str) {
std::vector<field> res;
auto i = format_str;
auto plain_text_first = i;
auto plain_text_first = format_str.begin();
bool read_percent_sign = false;
for (; *i != '\0'; ++i) {
auto i = format_str.begin();
for (; i != format_str.end(); ++i) {
if (read_percent_sign) {
field_type ft;
switch (*i) {
......@@ -397,19 +409,20 @@ logger::line_format logger::parse_format(const char* format_str) {
<< *i << std::endl;
}
if (ft != invalid_field)
res.emplace_back(field{ft, nullptr, nullptr});
res.emplace_back(field{ft, std::string{}});
plain_text_first = i + 1;
read_percent_sign = false;
} else {
if (*i == '%') {
if (plain_text_first != i)
res.emplace_back(field{plain_text_field, plain_text_first, i});
res.emplace_back(field{plain_text_field,
std::string{plain_text_first, i}});
read_percent_sign = true;
}
}
}
if (plain_text_first != i)
res.emplace_back(field{plain_text_field, plain_text_first, i});
res.emplace_back(field{plain_text_field, std::string{plain_text_first, i}});
return res;
}
......@@ -442,11 +455,15 @@ void logger::run() {
}
void logger::handle_event(event& x) {
// Print to file if available.
if (file_)
render(file_, file_format_, x);
if (system_.config().logger_console == atom("UNCOLORED")) {
// Stop if no console output is configured.
if (!has(console_output_flag))
return;
if (has(uncolored_console_flag)) {
render(std::clog, console_format_, x);
} else if (system_.config().logger_console == atom("COLORED")) {
} else {
switch (x.level) {
default:
break;
......@@ -473,7 +490,8 @@ void logger::handle_event(event& x) {
void logger::log_first_line() {
std::string msg = "level = ";
msg += to_string(system_.config().logger_verbosity);
msg += to_string(get_or(system_.config(), "logger.verbosity",
defaults::logger::verbosity));
msg += ", node = ";
msg += to_string(system_.node());
event tmp{CAF_LOG_LEVEL_INFO,
......@@ -504,17 +522,16 @@ void logger::log_last_line() {
void logger::start() {
#if defined(CAF_LOG_LEVEL)
parent_thread_ = std::this_thread::get_id();
auto verbosity = system_.config().logger_verbosity;
if (verbosity == atom("quiet") || verbosity == atom("QUIET"))
if (level_ == CAF_LOG_LEVEL_QUIET)
return;
t0_ = make_timestamp();
if (system_.config().logger_file_name.empty()) {
auto f = get_or(system_.config(), "logger.file-name",
defaults::logger::file_name);
if (f.empty()) {
// No need to continue if console and log file are disabled.
if (!(system_.config().logger_console == atom("UNCOLORED")
|| system_.config().logger_console == atom("COLORED")))
if (has(console_output_flag))
return;
} else {
auto f = system_.config().logger_file_name;
// Replace placeholders.
const char pid[] = "[PID]";
auto i = std::search(f.begin(), f.end(), std::begin(pid),
......@@ -541,7 +558,7 @@ void logger::start() {
return;
}
}
if (inline_output_)
if (has(inline_output_flag))
log_first_line();
else
thread_ = std::thread{[this] {
......@@ -554,7 +571,7 @@ void logger::start() {
void logger::stop() {
#if defined(CAF_LOG_LEVEL)
if (inline_output_) {
if (has(inline_output_flag)) {
log_last_line();
return;
}
......@@ -577,9 +594,9 @@ std::string to_string(logger::field_type x) {
std::string to_string(const logger::field& x) {
std::string result = "field{";
result += to_string(x.kind);
if (x.first != nullptr) {
if (x.kind == logger::plain_text_field) {
result += ", \"";
result.insert(result.end(), x.first, x.last);
result += x.text;
result += '\"';
}
result += "}";
......@@ -587,13 +604,7 @@ std::string to_string(const logger::field& x) {
}
bool operator==(const logger::field& x, const logger::field& y) {
if (x.kind == y.kind) {
if (x.kind == logger::plain_text_field)
return std::distance(x.first, x.last) == std::distance(y.first, y.last)
&& std::equal(x.first, x.last, y.first);
return true;
}
return false;
return x.kind == y.kind && x.text == y.text;
}
} // namespace caf
......@@ -18,7 +18,7 @@
#include "caf/config.hpp"
#define CAF_SUITE logger
#define CAF_SUITE logger
#include "caf/test/unit_test.hpp"
#include <ctime>
......@@ -38,12 +38,12 @@ struct fixture {
}
void add(logger::field_type kind) {
lf.emplace_back(logger::field{kind, nullptr, nullptr});
lf.emplace_back(logger::field{kind, std::string{}});
}
template <size_t N>
void add(logger::field_type kind, const char (&str)[N]) {
lf.emplace_back(logger::field{kind, str, str + (N - 1)}); // exclude \0
lf.emplace_back(logger::field{kind, std::string{str, str + (N - 1)}});
}
template <class F, class... Ts>
......@@ -67,7 +67,6 @@ CAF_TEST_FIXTURE_SCOPE(logger_tests, fixture)
// and finally serialization round-trip
CAF_TEST(parse_default_format_strings) {
actor_system sys{cfg};
CAF_CHECK_EQUAL(cfg.logger_file_format, file_format);
add(logger::runtime_field);
add(logger::plain_text_field, " ");
add(logger::category_field);
......
......@@ -84,7 +84,7 @@ template <class Hook>
struct config : actor_system_config {
config() {
add_thread_hook<Hook>();
logger_verbosity = atom("quiet");
set("logger.verbosity", atom("quiet"));
}
};
......
......@@ -755,7 +755,7 @@ struct config : public actor_system_config {
"Include hidden (system-level) actors")
.add(verbosity, "verbosity,v", "Debug output (from 0 to 2)");
// shutdown logging per default
logger_verbosity = atom("quiet");
set("logger.verbosity", atom("quiet"));
}
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment