lokinet/llarp/util/logging/file_logger.cpp

119 lines
2.4 KiB
C++
Raw Normal View History

#include <chrono>
#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>
namespace llarp
{
2019-11-15 21:16:46 +00:00
void
FileLogStream::Flush(Lines_t* lines, FILE* const f)
{
2019-11-15 21:16:46 +00:00
bool wrote_stuff = false;
do
{
2019-11-15 21:16:46 +00:00
auto maybe_line = lines->tryPopFront();
if (not maybe_line)
2019-11-15 21:16:46 +00:00
break;
if (fprintf(f, "%s\n", maybe_line->c_str()) >= 0)
2019-11-15 21:16:46 +00:00
wrote_stuff = true;
} while (true);
2019-11-15 21:10:51 +00:00
if (wrote_stuff)
2019-11-15 21:16:46 +00:00
fflush(f);
}
2019-11-15 21:16:46 +00:00
// namespace
FileLogStream::FileLogStream(
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-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();
if (not line)
2019-11-15 21:16:46 +00:00
break;
} while (true);
2019-11-15 21:16:46 +00:00
fflush(m_File);
if (m_Close)
2019-11-15 21:16:46 +00:00
fclose(m_File);
}
2019-11-15 21:16:46 +00:00
bool
FileLogStream::ShouldFlush(llarp_time_t now) const
{
if (m_Lines.full())
2019-11-15 21:16:46 +00:00
return true;
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-11-15 21:16:46 +00:00
void
FileLogStream::PreLog(
std::stringstream& ss,
LogLevel lvl,
std::string_view filename,
int lineno,
const std::string& nodename) const
2019-11-15 21:16:46 +00:00
{
ss << "[" << LogLevelToString(lvl) << "] ";
ss << "[" << nodename << "]"
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << filename << ":" << lineno
<< "\t";
2019-11-15 21:16:46 +00:00
}
2019-11-15 21:16:46 +00:00
void
FileLogStream::Print(LogLevel, std::string_view, const std::string& msg)
2019-11-15 21:16:46 +00:00
{
m_Lines.pushBack(msg);
}
2019-11-15 21:16:46 +00:00
void
FileLogStream::AppendLog(
LogLevel lvl,
std::string_view filename,
int lineno,
const std::string& nodename,
const std::string msg)
2019-11-15 21:16:46 +00:00
{
ILogStream::AppendLog(lvl, filename, lineno, nodename, msg);
2019-11-15 21:16:46 +00:00
Tick(llarp::time_now_ms());
}
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)
{
if (ShouldFlush(now))
2019-11-15 21:16:46 +00:00
FlushLinesToDisk(now);
}
void
FileLogStream::FlushLinesToDisk(llarp_time_t now)
{
FILE* const f = m_File;
auto lines = &m_Lines;
m_Disk([f, lines]() { Flush(lines, f); });
2019-11-15 21:16:46 +00:00
m_LastFlush = now;
}
} // namespace llarp