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) {
void logger::run() {
#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;
// Replace placeholders.
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()) {
auto id = std::to_string(detail::get_process_id());
f.replace(i, i + sizeof(pid) - 1, id);
......@@ -369,11 +377,13 @@ void logger::run() {
auto nid = to_string(system_.node());
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) {
std::cerr << "unable to open log file " << f << std::endl;
return;
}
}
if (file) {
// log first entry
constexpr atom_value level_names[] = {
error_log_lvl_atom::value, warning_log_lvl_atom::value,
......@@ -382,8 +392,9 @@ void logger::run() {
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;
file << " level = " << to_string(lvl_atom) << ", node = "
<< to_string(system_.node()) << std::endl;
}
// receive log entries from other threads and actors
std::unique_ptr<event> ptr;
for (;;) {
......@@ -404,6 +415,7 @@ void logger::run() {
if (it == filter.end())
continue;
}
if (file)
file << ptr->prefix << ' ' << ptr->msg << std::endl;
if (system_.config().logger_console == atom("UNCOLORED")) {
std::clog << ptr->msg << std::endl;
......@@ -430,10 +442,11 @@ void logger::run() {
std::clog << ptr->msg << color(reset) << std::endl;
}
}
if (file) {
log_prefix(file, CAF_LOG_LEVEL_INFO, "caf", "caf.logger", "stop",
__FILE__, __LINE__, parent_thread_);
file << " EOF" << std::endl;
file.close();
}
#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