Commit 2b0953d5 authored by Matthias Vallentin's avatar Matthias Vallentin

Move logging filter before expression evaluation

This patch to the logging API prevents evaluation of unneeded
expressions by checking whether the logger will accept a given
level-component pair.
parent b8c07840
......@@ -66,11 +66,9 @@ public:
event* next;
event* prev;
int level;
const char* component;
std::string prefix;
std::string msg;
explicit event(int l = 0, char const* c = "", std::string p = "",
std::string m = "");
explicit event(int lvl = 0, std::string pfx = "", std::string msg = "");
};
template <class T>
......@@ -158,6 +156,8 @@ public:
const char* file_name, int line_num,
const std::string& msg);
static bool accepts(int level, const char* component);
/** @endcond */
private:
......@@ -233,9 +233,12 @@ inline caf::actor_id caf_set_aid_dummy() { return 0; }
#endif
#define CAF_LOG_IMPL(component, loglvl, message) \
caf::logger::log_static(loglvl, component, CAF_GET_CLASS_NAME, \
__func__, __FILE__, __LINE__, \
(caf::logger::line_builder{} << message).get())
do { \
if (caf::logger::accepts(loglvl, component)) \
caf::logger::log_static(loglvl, component, CAF_GET_CLASS_NAME, \
__func__, __FILE__, __LINE__, \
(caf::logger::line_builder{} << message).get()); \
} while (0)
#define CAF_PUSH_AID(aarg) \
auto CAF_UNIFYN(caf_tmp_ptr) = caf::logger::current_logger(); \
......@@ -270,9 +273,10 @@ inline caf::actor_id caf_set_aid_dummy() { return 0; }
CAF_LOG_IMPL(CAF_LOG_COMPONENT, CAF_LOG_LEVEL_TRACE, \
"ENTRY" << entry_message); \
auto CAF_UNIFYN(caf_log_trace_guard_) = ::caf::detail::make_scope_guard([=] {\
caf::logger::log_static(CAF_LOG_LEVEL_TRACE, CAF_LOG_COMPONENT, \
CAF_GET_CLASS_NAME, CAF_UNIFYN(func_name_), \
__FILE__, __LINE__, "EXIT"); \
if (caf::logger::accepts(CAF_LOG_LEVEL_TRACE, CAF_LOG_COMPONENT)) \
caf::logger::log_static(CAF_LOG_LEVEL_TRACE, CAF_LOG_COMPONENT, \
CAF_GET_CLASS_NAME, CAF_UNIFYN(func_name_), \
__FILE__, __LINE__, "EXIT"); \
})
#endif // CAF_LOG_LEVEL < CAF_LOG_LEVEL_TRACE
......
......@@ -152,13 +152,12 @@ void prettify_type_name(std::string& class_name, const char* c_class_name) {
} // namespace <anonymous>
logger::event::event(int l, const char* c, std::string p, std::string m)
logger::event::event(int lvl, std::string pfx, std::string msg)
: next(nullptr),
prev(nullptr),
level(l),
component(c),
prefix(std::move(p)),
msg(std::move(m)) {
level(lvl),
prefix(std::move(pfx)),
msg(std::move(msg)) {
// nop
}
......@@ -278,13 +277,11 @@ void logger::log(int level, const char* component,
const char* c_full_file_name, int line_num,
const std::string& msg) {
CAF_ASSERT(level >= 0 && level <= 4);
if (level > level_)
return;
std::ostringstream prefix;
log_prefix(prefix, level, component, class_name, function_name,
c_full_file_name, line_num);
queue_.synchronized_enqueue(queue_mtx_, queue_cv_,
new event{level, component, prefix.str(), msg});
new event{level, prefix.str(), msg});
}
void logger::set_current_actor_system(actor_system* x) {
......@@ -308,6 +305,24 @@ void logger::log_static(int level, const char* component,
msg);
}
bool logger::accepts(int level, const char* component) {
CAF_ASSERT(component != nullptr);
CAF_ASSERT(level >= 0 && level <= 4);
auto ptr = get_current_logger();
if (ptr == nullptr || level > ptr->level_)
return false;
// TODO: once we've phased out GCC 4.8, we can upgrade this to a regex.
// Until then, we just search for a substring in the filter.
auto& filter = ptr->system_.config().logger_filter;
if (!filter.empty()) {
auto it = std::search(filter.begin(), filter.end(),
component, component + std::strlen(component));
if (it == filter.end())
return false;
}
return true;
}
logger::~logger() {
stop();
// tell system our dtor is done
......@@ -406,15 +421,6 @@ void logger::run() {
// empty message means: shut down
if (ptr->msg.empty())
break;
// TODO: once we've phased out GCC 4.8, we can upgrade this to a regex.
// Until then, we just search for a substring in the filter.
auto& filter = system_.config().logger_filter;
if (!filter.empty()) {
auto it = std::search(filter.begin(), filter.end(), ptr->component,
ptr->component + std::strlen(ptr->component));
if (it == filter.end())
continue;
}
if (file)
file << ptr->prefix << ' ' << ptr->msg << std::endl;
if (system_.config().logger_console == atom("UNCOLORED")) {
......@@ -424,7 +430,7 @@ void logger::run() {
default:
break;
case CAF_LOG_LEVEL_ERROR:
std::clog << term::red;;
std::clog << term::red;
break;
case CAF_LOG_LEVEL_WARNING:
std::clog << term::yellow;
......
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