Commit 288470f3 authored by Matthias Vallentin's avatar Matthias Vallentin Committed by Marian Triebe

Support logging to console only

If logger_filename of the actor system configuration is empty, the
logger will only print to the console.
parent 033ca1ab
...@@ -349,10 +349,18 @@ void logger::init(actor_system_config& cfg) { ...@@ -349,10 +349,18 @@ void logger::init(actor_system_config& cfg) {
void logger::run() { void logger::run() {
#if defined(CAF_LOG_LEVEL) #if defined(CAF_LOG_LEVEL)
std::fstream file;
if (system_.config().logger_filename.empty()) {
// If the console is also disabled, no need to continue.
if (!(system_.config().logger_console == atom("UNCOLORED")
|| system_.config().logger_console == atom("COLORED")))
return;
} else {
auto f = system_.config().logger_filename; auto f = system_.config().logger_filename;
// Replace placeholders. // Replace placeholders.
const char pid[] = "[PID]"; const char pid[] = "[PID]";
auto i = std::search(f.begin(), f.end(), std::begin(pid), std::end(pid) - 1); auto i = std::search(f.begin(), f.end(), std::begin(pid),
std::end(pid) - 1);
if (i != f.end()) { if (i != f.end()) {
auto id = std::to_string(detail::get_process_id()); auto id = std::to_string(detail::get_process_id());
f.replace(i, i + sizeof(pid) - 1, id); f.replace(i, i + sizeof(pid) - 1, id);
...@@ -369,11 +377,13 @@ void logger::run() { ...@@ -369,11 +377,13 @@ void logger::run() {
auto nid = to_string(system_.node()); auto nid = to_string(system_.node());
f.replace(i, i + sizeof(node) - 1, nid); f.replace(i, i + sizeof(node) - 1, nid);
} }
std::fstream file(f, std::ios::out | std::ios::app); file.open(f, std::ios::out | std::ios::app);
if (!file) { if (!file) {
std::cerr << "unable to open log file " << f << std::endl; std::cerr << "unable to open log file " << f << std::endl;
return; return;
} }
}
if (file) {
// log first entry // log first entry
constexpr atom_value level_names[] = { constexpr atom_value level_names[] = {
error_log_lvl_atom::value, warning_log_lvl_atom::value, error_log_lvl_atom::value, warning_log_lvl_atom::value,
...@@ -382,8 +392,9 @@ void logger::run() { ...@@ -382,8 +392,9 @@ void logger::run() {
auto lvl_atom = level_names[CAF_LOG_LEVEL]; auto lvl_atom = level_names[CAF_LOG_LEVEL];
log_prefix(file, CAF_LOG_LEVEL_INFO, "caf", "caf.logger", "start", log_prefix(file, CAF_LOG_LEVEL_INFO, "caf", "caf.logger", "start",
__FILE__, __LINE__, parent_thread_); __FILE__, __LINE__, parent_thread_);
file << " level = " << to_string(lvl_atom) << ", node = " << to_string(system_.node()) file << " level = " << to_string(lvl_atom) << ", node = "
<< std::endl; << 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,6 +415,7 @@ void logger::run() { ...@@ -404,6 +415,7 @@ void logger::run() {
if (it == filter.end()) if (it == filter.end())
continue; continue;
} }
if (file)
file << ptr->prefix << ' ' << ptr->msg << std::endl; 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;
...@@ -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;
} }
} }
if (file) {
log_prefix(file, CAF_LOG_LEVEL_INFO, "caf", "caf.logger", "stop", log_prefix(file, CAF_LOG_LEVEL_INFO, "caf", "caf.logger", "stop",
__FILE__, __LINE__, parent_thread_); __FILE__, __LINE__, parent_thread_);
file << " EOF" << std::endl; file << " EOF" << std::endl;
file.close(); }
#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