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