Commit a0b58f45 authored by Matthias Vallentin's avatar Matthias Vallentin

Add new log component features

By defining CAF_LOG_COMPONENT before including caf/logger.hpp, users can
now define their own log component, which is a string that appears as
second log column to faciliate distinguishing user and system log
messages.
parent 975a8c1d
...@@ -123,7 +123,7 @@ public: ...@@ -123,7 +123,7 @@ public:
actor_id thread_local_aid(actor_id aid); actor_id thread_local_aid(actor_id aid);
/// Writes an entry to the log file. /// Writes an entry to the log file.
void log(int level, const std::string& class_name, void log(int level, const char* component, const std::string& class_name,
const char* function_name, const char* file_name, const char* function_name, const char* file_name,
int line_num, const std::string& msg); int line_num, const std::string& msg);
...@@ -133,13 +133,15 @@ public: ...@@ -133,13 +133,15 @@ public:
class trace_helper { class trace_helper {
public: public:
trace_helper(std::string class_name, const char* fun_name, trace_helper(const char* component, std::string class_name,
const char* file_name, int line_num, const std::string& msg); const char* fun_name, const char* file_name, int line_num,
const std::string& msg);
~trace_helper(); ~trace_helper();
private: private:
logger* parent_; logger* parent_;
const char* component_;
std::string class_; std::string class_;
const char* fun_name_; const char* fun_name_;
const char* file_name_; const char* file_name_;
...@@ -150,9 +152,11 @@ public: ...@@ -150,9 +152,11 @@ public:
static logger* current_logger(); static logger* current_logger();
static void log_static(int level, const std::string& class_name, static void log_static(int level, const char* component,
const char* function_name, const char* file_name, const std::string& class_name,
int line_num, const std::string& msg); const char* function_name,
const char* file_name, int line_num,
const std::string& msg);
/** @endcond */ /** @endcond */
...@@ -229,9 +233,13 @@ inline caf::actor_id caf_set_aid_dummy() { return 0; } ...@@ -229,9 +233,13 @@ inline caf::actor_id caf_set_aid_dummy() { return 0; }
#else // CAF_LOG_LEVEL #else // CAF_LOG_LEVEL
#ifndef CAF_LOG_COMPONENT
#define CAF_LOG_COMPONENT "caf"
#endif
#define CAF_LOG_IMPL(loglvl, message) \ #define CAF_LOG_IMPL(loglvl, message) \
caf::logger::log_static(loglvl, CAF_GET_CLASS_NAME, __func__, \ caf::logger::log_static(loglvl, CAF_LOG_COMPONENT, CAF_GET_CLASS_NAME, \
__FILE__, __LINE__, \ __func__, __FILE__, __LINE__, \
(caf::logger::line_builder{} << message).get()) (caf::logger::line_builder{} << message).get())
#define CAF_PUSH_AID(aarg) \ #define CAF_PUSH_AID(aarg) \
......
...@@ -211,9 +211,10 @@ actor_id logger::thread_local_aid(actor_id aid) { ...@@ -211,9 +211,10 @@ actor_id logger::thread_local_aid(actor_id aid) {
return 0; // was empty before return 0; // was empty before
} }
void logger::log(int level, const std::string& class_name, void logger::log(int level, const char* component,
const char* function_name, const char* c_full_file_name, const std::string& class_name, const char* function_name,
int line_num, const std::string& msg) { const char* c_full_file_name, int line_num,
const std::string& msg) {
CAF_ASSERT(level >= 0 && level <= 4); CAF_ASSERT(level >= 0 && level <= 4);
std::string file_name; std::string file_name;
std::string full_file_name = c_full_file_name; std::string full_file_name = c_full_file_name;
...@@ -230,28 +231,32 @@ void logger::log(int level, const std::string& class_name, ...@@ -230,28 +231,32 @@ void logger::log(int level, const std::string& class_name,
auto t0 = std::chrono::high_resolution_clock::now().time_since_epoch(); auto t0 = std::chrono::high_resolution_clock::now().time_since_epoch();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t0).count(); auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t0).count();
std::ostringstream line; std::ostringstream line;
line << ms << " " << log_level_name[level] << " " line << ms << " " << component << " " << log_level_name[level] << " "
<< "actor" << thread_local_aid() << " " << std::this_thread::get_id() << "actor" << thread_local_aid() << " " << std::this_thread::get_id()
<< " " << class_name << " " << function_name << " " << file_name << ":" << " " << class_name << " " << function_name << " " << file_name << ":"
<< line_num << " " << msg << std::endl; << line_num << " " << msg << std::endl;
queue_.synchronized_enqueue(queue_mtx_, queue_cv_, new event{line.str()}); queue_.synchronized_enqueue(queue_mtx_, queue_cv_, new event{line.str()});
} }
logger::trace_helper::trace_helper(std::string class_name, const char* fun_name, logger::trace_helper::trace_helper(const char* component,
std::string class_name, const char* fun_name,
const char* file_name, int line_num, const char* file_name, int line_num,
const std::string& msg) const std::string& msg)
: parent_(current_logger()), : parent_(current_logger()),
component_(component),
class_(std::move(class_name)), class_(std::move(class_name)),
fun_name_(fun_name), fun_name_(fun_name),
file_name_(file_name), file_name_(file_name),
line_num_(line_num) { line_num_(line_num) {
if (parent_) if (parent_)
parent_->log(4, class_, fun_name, file_name, line_num, "ENTRY " + msg); parent_->log(4, component, class_, fun_name, file_name, line_num,
"ENTRY " + msg);
} }
logger::trace_helper::~trace_helper() { logger::trace_helper::~trace_helper() {
if (parent_) if (parent_)
parent_->log(4, class_.c_str(), fun_name_, file_name_, line_num_, "EXIT"); parent_->log(4, component_, class_.c_str(), fun_name_, file_name_,
line_num_, "EXIT");
} }
void logger::set_current_actor_system(actor_system* x) { void logger::set_current_actor_system(actor_system* x) {
...@@ -262,15 +267,16 @@ logger* logger::current_logger() { ...@@ -262,15 +267,16 @@ logger* logger::current_logger() {
return get_current_logger(); return get_current_logger();
} }
void logger::log_static(int level, const std::string& class_name, void logger::log_static(int level, const char* component,
const char* function_name, const char* file_name, const std::string& class_name,
int line_num, const std::string& msg) { const char* function_name, const char* file_name,
int line_num, const std::string& msg) {
auto ptr = get_current_logger(); auto ptr = get_current_logger();
if (ptr) if (ptr)
ptr->log(level, class_name, function_name, file_name, line_num, msg); ptr->log(level, component, class_name, function_name, file_name, line_num,
msg);
} }
logger::~logger() { logger::~logger() {
// nop // nop
} }
...@@ -306,13 +312,13 @@ void logger::start() { ...@@ -306,13 +312,13 @@ void logger::start() {
thread_ = std::thread{[this] { this->run(); }}; thread_ = std::thread{[this] { this->run(); }};
std::string msg = "ENTRY log level = "; std::string msg = "ENTRY log level = ";
msg += log_level_table[global_log_level]; msg += log_level_table[global_log_level];
log(4, "caf::logger", "run", __FILE__, __LINE__, msg); log(4, "caf", "caf::logger", "run", __FILE__, __LINE__, msg);
# endif # endif
} }
void logger::stop() { void logger::stop() {
# ifdef CAF_LOG_LEVEL # ifdef CAF_LOG_LEVEL
log(4, "caf::logger", "run", __FILE__, __LINE__, "EXIT"); log(4, "caf", "caf::logger", "run", __FILE__, __LINE__, "EXIT");
// an empty string means: shut down // an empty string means: shut down
queue_.synchronized_enqueue(queue_mtx_, queue_cv_, new event{""}); queue_.synchronized_enqueue(queue_mtx_, queue_cv_, new event{""});
thread_.join(); thread_.join();
......
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