Commit d6f545d2 authored by Matthias Vallentin's avatar Matthias Vallentin

Support logging to console only

If logger_filename of the actor system configuration is empty, the
logger will only print to the console.
parent 0ff20648
...@@ -349,41 +349,52 @@ void logger::init(actor_system_config& cfg) { ...@@ -349,41 +349,52 @@ void logger::init(actor_system_config& cfg) {
void logger::run() { void logger::run() {
#if defined(CAF_LOG_LEVEL) #if defined(CAF_LOG_LEVEL)
auto f = system_.config().logger_filename; std::fstream file;
// Replace placeholders. if (system_.config().logger_filename.empty()) {
const char pid[] = "[PID]"; // If the console is also disabled, no need to continue.
auto i = std::search(f.begin(), f.end(), std::begin(pid), std::end(pid) - 1); if (!(system_.config().logger_console == atom("UNCOLORED")
if (i != f.end()) { || system_.config().logger_console == atom("COLORED")))
auto id = std::to_string(detail::get_process_id()); return;
f.replace(i, i + sizeof(pid) - 1, id); } else {
} auto f = system_.config().logger_filename;
const char ts[] = "[TIMESTAMP]"; // Replace placeholders.
i = std::search(f.begin(), f.end(), std::begin(ts), std::end(ts) - 1); const char pid[] = "[PID]";
if (i != f.end()) { auto i = std::search(f.begin(), f.end(), std::begin(pid),
auto now = timestamp_to_string(make_timestamp()); std::end(pid) - 1);
f.replace(i, i + sizeof(ts) - 1, now); if (i != f.end()) {
} auto id = std::to_string(detail::get_process_id());
const char node[] = "[NODE]"; f.replace(i, i + sizeof(pid) - 1, id);
i = std::search(f.begin(), f.end(), std::begin(node), std::end(node) - 1); }
if (i != f.end()) { const char ts[] = "[TIMESTAMP]";
auto nid = to_string(system_.node()); i = std::search(f.begin(), f.end(), std::begin(ts), std::end(ts) - 1);
f.replace(i, i + sizeof(node) - 1, nid); if (i != f.end()) {
auto now = timestamp_to_string(make_timestamp());
f.replace(i, i + sizeof(ts) - 1, now);
}
const char node[] = "[NODE]";
i = std::search(f.begin(), f.end(), std::begin(node), std::end(node) - 1);
if (i != f.end()) {
auto nid = to_string(system_.node());
f.replace(i, i + sizeof(node) - 1, nid);
}
file.open(f, std::ios::out | std::ios::app);
if (!file) {
std::cerr << "unable to open log file " << f << std::endl;
return;
}
} }
std::fstream file(f, std::ios::out | std::ios::app); if (file) {
if (!file) { // log first entry
std::cerr << "unable to open log file " << f << std::endl; constexpr atom_value level_names[] = {
return; error_log_lvl_atom::value, warning_log_lvl_atom::value,
info_log_lvl_atom::value, debug_log_lvl_atom::value,
trace_log_lvl_atom::value};
auto lvl_atom = level_names[CAF_LOG_LEVEL];
log_prefix(file, CAF_LOG_LEVEL_INFO, "caf", "caf.logger", "start",
__FILE__, __LINE__, parent_thread_);
file << " level = " << to_string(lvl_atom) << ", node = "
<< to_string(system_.node()) << std::endl;
} }
// log first entry
constexpr atom_value level_names[] = {
error_log_lvl_atom::value, warning_log_lvl_atom::value,
info_log_lvl_atom::value, debug_log_lvl_atom::value,
trace_log_lvl_atom::value};
auto lvl_atom = level_names[CAF_LOG_LEVEL];
log_prefix(file, CAF_LOG_LEVEL_INFO, "caf", "caf.logger", "start",
__FILE__, __LINE__, parent_thread_);
file << " level = " << to_string(lvl_atom) << ", node = " << to_string(system_.node())
<< std::endl;
// receive log entries from other threads and actors // receive log entries from other threads and actors
std::unique_ptr<event> ptr; std::unique_ptr<event> ptr;
for (;;) { for (;;) {
...@@ -404,7 +415,8 @@ void logger::run() { ...@@ -404,7 +415,8 @@ void logger::run() {
if (it == filter.end()) if (it == filter.end())
continue; continue;
} }
file << ptr->prefix << ' ' << ptr->msg << std::endl; if (file)
file << ptr->prefix << ' ' << ptr->msg << std::endl;
if (system_.config().logger_console == atom("UNCOLORED")) { if (system_.config().logger_console == atom("UNCOLORED")) {
std::clog << ptr->msg << std::endl; std::clog << ptr->msg << std::endl;
} else if (system_.config().logger_console == atom("COLORED")) { } else if (system_.config().logger_console == atom("COLORED")) {
...@@ -430,10 +442,11 @@ void logger::run() { ...@@ -430,10 +442,11 @@ void logger::run() {
std::clog << ptr->msg << color(reset) << std::endl; std::clog << ptr->msg << color(reset) << std::endl;
} }
} }
log_prefix(file, CAF_LOG_LEVEL_INFO, "caf", "caf.logger", "stop", if (file) {
__FILE__, __LINE__, parent_thread_); log_prefix(file, CAF_LOG_LEVEL_INFO, "caf", "caf.logger", "stop",
file << " EOF" << std::endl; __FILE__, __LINE__, parent_thread_);
file.close(); file << " EOF" << std::endl;
}
#endif #endif
} }
......
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