mirror of
https://github.com/oxen-io/lokinet
synced 2023-12-14 06:53:00 +01:00
Fix logging initialization and flush at program exit
This commit is contained in:
parent
2479049876
commit
6909e20588
11 changed files with 72 additions and 6 deletions
|
@ -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
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -621,10 +621,24 @@ namespace llarp
|
|||
assert(conf->logging.m_logType != LoggingConfig::LogType::Unknown);
|
||||
break;
|
||||
case LoggingConfig::LogType::File:
|
||||
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;
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -47,6 +47,9 @@ namespace llarp
|
|||
const std::string& nodename,
|
||||
const std::string msg) override;
|
||||
|
||||
virtual void
|
||||
ImmediateFlush() override;
|
||||
|
||||
using Lines_t = thread::Queue< std::string >;
|
||||
|
||||
protected:
|
||||
|
|
|
@ -67,6 +67,13 @@ namespace llarp
|
|||
LogContext::Instance().runtimeLevel = lvl;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
LogContext::ImmediateFlush()
|
||||
{
|
||||
logStream->ImmediateFlush();
|
||||
}
|
||||
|
||||
} // namespace llarp
|
||||
|
||||
extern "C"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -22,6 +22,11 @@ namespace llarp
|
|||
void
|
||||
PostLog(std::stringstream& ss) const override;
|
||||
|
||||
virtual void
|
||||
ImmediateFlush() override
|
||||
{
|
||||
}
|
||||
|
||||
void Tick(llarp_time_t) override
|
||||
{
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -57,4 +57,10 @@ namespace llarp
|
|||
m_Out << msg << std::flush;
|
||||
}
|
||||
|
||||
void
|
||||
OStreamLogStream::ImmediateFlush()
|
||||
{
|
||||
m_Out << std::flush;
|
||||
}
|
||||
|
||||
} // namespace llarp
|
||||
|
|
|
@ -26,6 +26,9 @@ namespace llarp
|
|||
void
|
||||
PostLog(std::stringstream& ss) const override;
|
||||
|
||||
virtual void
|
||||
ImmediateFlush() override;
|
||||
|
||||
void Tick(llarp_time_t) override
|
||||
{
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue