Fix logging initialization and flush at program exit

This commit is contained in:
Stephen Shelton 2020-04-02 10:47:56 -06:00
parent 2479049876
commit 6909e20588
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C
11 changed files with 72 additions and 6 deletions

View File

@ -263,6 +263,7 @@ main(int argc, char* argv[])
"annoying image in your syslog for all time."})
{
LogError(wtf);
llarp::LogContext::Instance().ImmediateFlush();
}
std::abort();
}
@ -272,6 +273,8 @@ main(int argc, char* argv[])
main_thread.join();
const auto code = ftr.get();
llarp::LogContext::Instance().ImmediateFlush();
#ifdef _WIN32
::WSACleanup();
#endif

View File

@ -347,7 +347,11 @@ namespace llarp
{
(void)params;
conf.defineOption<std::string>("logging", "type", false, "file",
constexpr auto DefaultLogType = "file";
constexpr auto DefaultLogFile = "stdout";
constexpr auto DefaultLogLevel = "info";
conf.defineOption<std::string>("logging", "type", false, DefaultLogType,
[this](std::string arg) {
LoggingConfig::LogType type = LogTypeFromString(arg);
if (type == LogType::Unknown)
@ -356,7 +360,7 @@ namespace llarp
m_logType = type;
});
conf.defineOption<std::string>("logging", "level", false, "info",
conf.defineOption<std::string>("logging", "level", false, DefaultLogLevel,
[this](std::string arg) {
nonstd::optional<LogLevel> level = LogLevelFromString(arg);
if (not level.has_value())
@ -365,7 +369,7 @@ namespace llarp
m_logLevel = level.value();
});
conf.defineOption<std::string>("logging", "file", false, "stdout",
conf.defineOption<std::string>("logging", "file", false, DefaultLogFile,
AssignmentAcceptor(m_logFile));
}

View File

@ -621,10 +621,24 @@ namespace llarp
assert(conf->logging.m_logType != LoggingConfig::LogType::Unknown);
break;
case LoggingConfig::LogType::File:
LogContext::Instance().logStream =
std::make_unique< FileLogStream >(diskworker(), logfile, 100ms, true);
if (logfile != stdout)
{
LogInfo("Switching logger to file ", conf->logging.m_logFile);
std::cout << std::flush;
LogContext::Instance().logStream =
std::make_unique< FileLogStream >(diskworker(), logfile, 100ms, true);
}
else
{
LogInfo("Logger remains stdout");
}
break;
case LoggingConfig::LogType::Json:
LogInfo("Switching logger to JSON with file: ", conf->logging.m_logFile);
std::cout << std::flush;
LogContext::Instance().logStream = std::make_unique< JSONLogStream >(
diskworker(), logfile, 100ms, logfile != stdout);
break;
@ -632,6 +646,7 @@ namespace llarp
if (logfile)
{
// TODO: this logic should be handled in Config
// TODO: this won't even work because of default value for 'file' (== "stdout")
LogError("Cannot mix log type=syslog and file=*");
::fclose(logfile);
return false;
@ -640,6 +655,8 @@ namespace llarp
LogError("syslog not supported on win32");
return false;
#else
LogInfo("Switching logger to syslog");
std::cout << std::flush;
LogContext::Instance().logStream = std::make_unique< SysLogStream >();
#endif
break;

View File

@ -1,5 +1,7 @@
#include <chrono>
#include <util/logging/file_logger.hpp>
#include <util/logging/logger_internal.hpp>
#include <util/time.hpp>
#include <utility>
@ -92,6 +94,13 @@ namespace llarp
Tick(llarp::time_now_ms());
}
void
FileLogStream::ImmediateFlush()
{
Flush(&m_Lines, m_File);
m_LastFlush = time_now_ms();
}
void
FileLogStream::Tick(llarp_time_t now)
{

View File

@ -47,7 +47,10 @@ namespace llarp
const std::string& nodename,
const std::string msg) override;
using Lines_t = thread::Queue<std::string>;
virtual void
ImmediateFlush() override;
using Lines_t = thread::Queue< std::string >;
protected:
Lines_t m_Lines;

View File

@ -67,6 +67,13 @@ namespace llarp
LogContext::Instance().runtimeLevel = lvl;
}
}
void
LogContext::ImmediateFlush()
{
logStream->ImmediateFlush();
}
} // namespace llarp
extern "C"

View File

@ -27,6 +27,11 @@ namespace llarp
void
RevertRuntimeLevel();
/// A blocking call that will not return until any existing log functions have flushed.
/// Should only be called in rare circumstances, such as when the program is about to exit.
void
ImmediateFlush();
};
void

View File

@ -22,6 +22,11 @@ namespace llarp
void
PostLog(std::stringstream& ss) const override;
virtual void
ImmediateFlush() override
{
}
void Tick(llarp_time_t) override
{
}

View File

@ -44,6 +44,10 @@ namespace llarp
Print(lvl, fname, ss.str());
}
/// A blocking call to flush to disk. Should only be called in rare circumstances.
virtual void
ImmediateFlush() = 0;
/// called every end of event loop tick
virtual void
Tick(llarp_time_t now) = 0;

View File

@ -57,4 +57,10 @@ namespace llarp
m_Out << msg << std::flush;
}
void
OStreamLogStream::ImmediateFlush()
{
m_Out << std::flush;
}
} // namespace llarp

View File

@ -26,6 +26,9 @@ namespace llarp
void
PostLog(std::stringstream& ss) const override;
virtual void
ImmediateFlush() override;
void Tick(llarp_time_t) override
{
}