Commit b968f227 authored by Dominik Charousset's avatar Dominik Charousset

Merge branch 'topic/log-component' into develop

parents 3c0b73b4 9e0bec39
......@@ -87,6 +87,17 @@ public:
public:
line_builder();
template <class T>
line_builder& operator<<(const T& x) {
if (! str_.empty())
str_ += " ";
std::stringstream ss;
ss << x;
str_ += ss.str();
behind_arg_ = false;
return *this;
}
template <class T>
line_builder& operator<<(const arg_wrapper<T>& x) {
if (behind_arg_)
......@@ -100,6 +111,8 @@ public:
return *this;
}
line_builder& operator<<(const std::string& str);
line_builder& operator<<(const char* str);
std::string get() const;
......@@ -123,7 +136,7 @@ public:
actor_id thread_local_aid(actor_id aid);
/// 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,
int line_num, const std::string& msg);
......@@ -135,9 +148,11 @@ public:
static logger* current_logger();
static void log_static(int level, const std::string& class_name,
const char* function_name, const char* file_name,
int line_num, const std::string& msg);
static void log_static(int level, const char* component,
const std::string& class_name,
const char* function_name,
const char* file_name, int line_num,
const std::string& msg);
/** @endcond */
......@@ -214,9 +229,13 @@ inline caf::actor_id caf_set_aid_dummy() { return 0; }
#else // CAF_LOG_LEVEL
#ifndef CAF_LOG_COMPONENT
#define CAF_LOG_COMPONENT "caf"
#endif
#define CAF_LOG_IMPL(loglvl, message) \
caf::logger::log_static(loglvl, CAF_GET_CLASS_NAME, __func__, \
__FILE__, __LINE__, \
caf::logger::log_static(loglvl, CAF_LOG_COMPONENT, CAF_GET_CLASS_NAME, \
__func__, __FILE__, __LINE__, \
(caf::logger::line_builder{} << message).get())
#define CAF_PUSH_AID(aarg) \
......@@ -254,9 +273,9 @@ inline caf::actor_id caf_set_aid_dummy() { return 0; }
const char* CAF_UNIFYN(func_name_) = __func__; \
CAF_LOG_IMPL(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_GET_CLASS_NAME, \
CAF_UNIFYN(func_name_), __FILE__, __LINE__, \
"EXIT"); \
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
......
......@@ -136,6 +136,14 @@ logger::line_builder::line_builder() : behind_arg_(false) {
// nop
}
logger::line_builder& logger::line_builder::operator<<(const std::string& str) {
if (! str_.empty())
str_ += " ";
str_ += str;
behind_arg_ = false;
return *this;
}
logger::line_builder& logger::line_builder::operator<<(const char* str) {
if (! str_.empty())
str_ += " ";
......@@ -211,9 +219,10 @@ actor_id logger::thread_local_aid(actor_id aid) {
return 0; // was empty before
}
void logger::log(int level, const std::string& class_name,
const char* function_name, const char* c_full_file_name,
int line_num, const std::string& msg) {
void logger::log(int level, const char* component,
const std::string& class_name, const char* function_name,
const char* c_full_file_name, int line_num,
const std::string& msg) {
CAF_ASSERT(level >= 0 && level <= 4);
std::string file_name;
std::string full_file_name = c_full_file_name;
......@@ -230,7 +239,7 @@ void logger::log(int level, const std::string& class_name,
auto t0 = std::chrono::high_resolution_clock::now().time_since_epoch();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t0).count();
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()
<< " " << class_name << " " << function_name << " " << file_name << ":"
<< line_num << " " << msg << std::endl;
......@@ -245,15 +254,16 @@ logger* logger::current_logger() {
return get_current_logger();
}
void logger::log_static(int level, const std::string& class_name,
const char* function_name, const char* file_name,
int line_num, const std::string& msg) {
void logger::log_static(int level, const char* component,
const std::string& class_name,
const char* function_name, const char* file_name,
int line_num, const std::string& msg) {
auto ptr = get_current_logger();
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() {
// nop
}
......@@ -289,13 +299,13 @@ void logger::start() {
thread_ = std::thread{[this] { this->run(); }};
std::string msg = "ENTRY 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
}
void logger::stop() {
# 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
queue_.synchronized_enqueue(queue_mtx_, queue_cv_, new event{""});
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