From a0fb0bbfe326b4cb07bc562107f0beaa6d61d39a Mon Sep 17 00:00:00 2001 From: Alan Jenkins Date: Mon, 28 May 2018 21:08:57 +0100 Subject: [PATCH] fix "Console" log to flush It's called "Console", which would be line-buffered anyway. But, it's implemented as std::cout. This might be piped to a logger, as in daemontools or systemd. In this case it will not be a TTY, and log lines should be flushed without too much delay. Let's just flush each message. Let's not introduce a static instance of StreamLogger (flushed by interval timer). That's too stressful to read, because static instances are really annoying to order. Example citation: "Yay, our static destructors are pretty much beyond repair at this point." -- application.cpp. I don't know if there will be any need to optimize logging syscalls. The init script uses `--daemonize`. I think the systemd service should also avoid using the "Console" log after startup (see next commit). The documentation does not warn that the syslog feature is less efficient in system calls than mainlog; deferred flusing does not seem to be a highly prominent feature. There's no cool comment in the code about how much the syscalls were slowing down some use case (or qualifying that this optimization can only eliminate syscalls on platforms with both mutexes and clocks that can work without syscalls). --- lib/base/logger.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp index 9a59d199f..8af34d6ca 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -245,8 +245,13 @@ Log::~Log() #endif /* I2_DEBUG */ } - if (Logger::IsConsoleLogEnabled() && entry.Severity >= Logger::GetConsoleLogSeverity()) + if (Logger::IsConsoleLogEnabled() && entry.Severity >= Logger::GetConsoleLogSeverity()) { StreamLogger::ProcessLogEntry(std::cout, entry); + + /* "Console" might be a pipe/socket (systemd, daemontools, docker, ...), + * then cout will not flush lines automatically. */ + std::cout << std::flush; + } } Log& Log::operator<<(const char *val) -- 2.40.0