2020-04-02 16:47:56 +00:00
|
|
|
#include <chrono>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "file_logger.hpp"
|
|
|
|
#include "logger_internal.hpp"
|
|
|
|
#include <llarp/util/time.hpp>
|
2019-09-01 12:10:49 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <utility>
|
2019-04-16 13:20:48 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2019-11-15 21:16:46 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
FileLogStream::Flush(Lines_t* lines, FILE* const f)
|
2019-05-18 18:46:49 +00:00
|
|
|
{
|
2019-11-15 21:16:46 +00:00
|
|
|
bool wrote_stuff = false;
|
|
|
|
do
|
2019-05-18 18:46:49 +00:00
|
|
|
{
|
2019-11-15 21:16:46 +00:00
|
|
|
auto maybe_line = lines->tryPopFront();
|
2020-05-20 19:46:08 +00:00
|
|
|
if (not maybe_line)
|
2019-11-15 21:16:46 +00:00
|
|
|
break;
|
2020-05-20 19:46:08 +00:00
|
|
|
if (fprintf(f, "%s\n", maybe_line->c_str()) >= 0)
|
2019-11-15 21:16:46 +00:00
|
|
|
wrote_stuff = true;
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (true);
|
2019-11-15 21:10:51 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (wrote_stuff)
|
2019-11-15 21:16:46 +00:00
|
|
|
fflush(f);
|
|
|
|
}
|
2019-04-16 13:20:48 +00:00
|
|
|
|
2019-11-15 21:16:46 +00:00
|
|
|
// namespace
|
2020-04-07 18:38:56 +00:00
|
|
|
FileLogStream::FileLogStream(
|
2020-06-11 11:44:02 +00:00
|
|
|
std::function<void(Work_t)> disk, FILE* f, llarp_time_t flushInterval, bool closeFile)
|
2019-11-15 21:16:46 +00:00
|
|
|
: m_Lines(1024 * 8)
|
|
|
|
, m_Disk(std::move(disk))
|
|
|
|
, m_File(f)
|
|
|
|
, m_FlushInterval(flushInterval)
|
|
|
|
, m_Close(closeFile)
|
|
|
|
{
|
|
|
|
m_Lines.enable();
|
|
|
|
}
|
2019-04-16 13:20:48 +00:00
|
|
|
|
2019-11-15 21:16:46 +00:00
|
|
|
FileLogStream::~FileLogStream()
|
|
|
|
{
|
|
|
|
m_Lines.disable();
|
|
|
|
do
|
2019-11-15 21:10:51 +00:00
|
|
|
{
|
2019-11-15 21:16:46 +00:00
|
|
|
auto line = m_Lines.tryPopFront();
|
2020-05-20 19:46:08 +00:00
|
|
|
if (not line)
|
2019-11-15 21:16:46 +00:00
|
|
|
break;
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (true);
|
2019-11-15 21:16:46 +00:00
|
|
|
fflush(m_File);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_Close)
|
2019-11-15 21:16:46 +00:00
|
|
|
fclose(m_File);
|
|
|
|
}
|
2019-04-16 13:20:48 +00:00
|
|
|
|
2019-11-15 21:16:46 +00:00
|
|
|
bool
|
|
|
|
FileLogStream::ShouldFlush(llarp_time_t now) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_Lines.full())
|
2019-11-15 21:16:46 +00:00
|
|
|
return true;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_LastFlush >= now)
|
2019-11-15 21:16:46 +00:00
|
|
|
return false;
|
|
|
|
const auto dlt = now - m_LastFlush;
|
|
|
|
return dlt >= m_FlushInterval;
|
|
|
|
}
|
2019-04-16 13:20:48 +00:00
|
|
|
|
2019-11-15 21:16:46 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
FileLogStream::PreLog(
|
|
|
|
std::stringstream& ss,
|
|
|
|
LogLevel lvl,
|
2021-05-11 11:22:36 +00:00
|
|
|
std::string_view filename,
|
2020-04-07 18:38:56 +00:00
|
|
|
int lineno,
|
|
|
|
const std::string& nodename) const
|
2019-11-15 21:16:46 +00:00
|
|
|
{
|
|
|
|
ss << "[" << LogLevelToString(lvl) << "] ";
|
|
|
|
ss << "[" << nodename << "]"
|
2021-05-11 11:22:36 +00:00
|
|
|
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << filename << ":" << lineno
|
2020-04-07 18:38:56 +00:00
|
|
|
<< "\t";
|
2019-11-15 21:16:46 +00:00
|
|
|
}
|
2019-04-16 13:20:48 +00:00
|
|
|
|
2019-11-15 21:16:46 +00:00
|
|
|
void
|
2021-05-11 11:22:36 +00:00
|
|
|
FileLogStream::Print(LogLevel, std::string_view, const std::string& msg)
|
2019-11-15 21:16:46 +00:00
|
|
|
{
|
|
|
|
m_Lines.pushBack(msg);
|
|
|
|
}
|
2019-04-16 13:20:48 +00:00
|
|
|
|
2019-11-15 21:16:46 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
FileLogStream::AppendLog(
|
|
|
|
LogLevel lvl,
|
2021-05-11 11:22:36 +00:00
|
|
|
std::string_view filename,
|
2020-04-07 18:38:56 +00:00
|
|
|
int lineno,
|
|
|
|
const std::string& nodename,
|
|
|
|
const std::string msg)
|
2019-11-15 21:16:46 +00:00
|
|
|
{
|
2021-05-11 11:22:36 +00:00
|
|
|
ILogStream::AppendLog(lvl, filename, lineno, nodename, msg);
|
2019-11-15 21:16:46 +00:00
|
|
|
Tick(llarp::time_now_ms());
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:47:56 +00:00
|
|
|
void
|
|
|
|
FileLogStream::ImmediateFlush()
|
|
|
|
{
|
|
|
|
Flush(&m_Lines, m_File);
|
|
|
|
m_LastFlush = time_now_ms();
|
|
|
|
}
|
|
|
|
|
2019-11-15 21:16:46 +00:00
|
|
|
void
|
|
|
|
FileLogStream::Tick(llarp_time_t now)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ShouldFlush(now))
|
2019-11-15 21:16:46 +00:00
|
|
|
FlushLinesToDisk(now);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileLogStream::FlushLinesToDisk(llarp_time_t now)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
FILE* const f = m_File;
|
|
|
|
auto lines = &m_Lines;
|
2020-06-11 11:44:02 +00:00
|
|
|
m_Disk([f, lines]() { Flush(lines, f); });
|
2019-11-15 21:16:46 +00:00
|
|
|
m_LastFlush = now;
|
|
|
|
}
|
|
|
|
} // namespace llarp
|