make it compile

This commit is contained in:
Jeff Becker 2019-11-15 16:16:46 -05:00
parent fdbaaa8188
commit 1fa0a0aab2
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
3 changed files with 95 additions and 86 deletions

View File

@ -5,95 +5,99 @@
namespace llarp namespace llarp
{ {
namespace void
FileLogStream::Flush(Lines_t *lines, FILE *const f)
{ {
template < typename T > bool wrote_stuff = false;
static void do
Flush(T *lines, FILE *const f)
{ {
bool wrote_stuff = false; auto maybe_line = lines->tryPopFront();
do if(not maybe_line.has_value())
{ break;
auto maybe_line = lines->tryPopFront(); const auto &line = maybe_line.value();
if(not maybe_line.has_value()) if(fprintf(f, "%s\n", line.c_str()) >= 0)
break; wrote_stuff = true;
const auto &line = maybe_line.value(); } while(true);
if(fprintf(f, "%s\n", line.c_str()) > 0)
wrote_stuff = true;
} while(true);
if(wrote_stuff) if(wrote_stuff)
fflush(f); fflush(f);
} }
// namespace
FileLogStream::FileLogStream(std::shared_ptr< thread::ThreadPool > disk,
FILE *f, llarp_time_t flushInterval,
bool closeFile)
: m_Lines(512)
, m_Disk(std::move(disk))
, m_File(f)
, m_FlushInterval(flushInterval)
, m_Close(closeFile)
{
m_Lines.enable();
}
FileLogStream::~FileLogStream() // namespace
{ FileLogStream::FileLogStream(std::shared_ptr< thread::ThreadPool > disk,
m_Lines.disable(); FILE *f, llarp_time_t flushInterval,
do bool closeFile)
{ : m_Lines(1024 * 8)
auto line = m_Lines.tryPopFront(); , m_Disk(std::move(disk))
if(not line.has_value()) , m_File(f)
break; , m_FlushInterval(flushInterval)
} while(true); , m_Close(closeFile)
fflush(m_File); {
if(m_Close) m_Lines.enable();
fclose(m_File); }
}
bool FileLogStream::~FileLogStream()
FileLogStream::ShouldFlush(llarp_time_t now) const {
m_Lines.disable();
do
{ {
if(m_Lines.full()) auto line = m_Lines.tryPopFront();
return true; if(not line.has_value())
if(m_LastFlush >= now) break;
return false; } while(true);
const auto dlt = now - m_LastFlush; fflush(m_File);
return dlt >= m_FlushInterval; if(m_Close)
} fclose(m_File);
}
void bool
FileLogStream::PreLog(std::stringstream &ss, LogLevel lvl, FileLogStream::ShouldFlush(llarp_time_t now) const
const char *fname, int lineno, {
const std::string &nodename) const if(m_Lines.full())
{ return true;
ss << "[" << LogLevelToString(lvl) << "] "; if(m_LastFlush >= now)
ss << "[" << nodename << "]" return false;
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << fname const auto dlt = now - m_LastFlush;
<< ":" << lineno << "\t"; return dlt >= m_FlushInterval;
} }
void void
FileLogStream::Print(LogLevel, const char *, const std::string &msg) FileLogStream::PreLog(std::stringstream &ss, LogLevel lvl, const char *fname,
{ int lineno, const std::string &nodename) const
m_Lines.pushBack(msg); {
Tick(llarp::time_now_ms()); ss << "[" << LogLevelToString(lvl) << "] ";
} ss << "[" << nodename << "]"
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << fname
<< ":" << lineno << "\t";
}
void void
FileLogStream::Tick(llarp_time_t now) FileLogStream::Print(LogLevel, const char *, const std::string &msg)
{ {
if(ShouldFlush(now)) m_Lines.pushBack(msg);
FlushLinesToDisk(now); }
}
void void
FileLogStream::FlushLinesToDisk(llarp_time_t now) FileLogStream::AppendLog(LogLevel lvl, const char *fname, int lineno,
{ const std::string &nodename, const std::string msg)
FILE *const f = m_File; {
auto lines = &m_Lines; ILogStream::AppendLog(lvl, fname, lineno, nodename, msg);
m_Disk->addJob([f, lines]() { Flush(lines, f); }); Tick(llarp::time_now_ms());
m_LastFlush = now; }
}
} // namespace void
FileLogStream::Tick(llarp_time_t now)
{
if(ShouldFlush(now))
FlushLinesToDisk(now);
}
void
FileLogStream::FlushLinesToDisk(llarp_time_t now)
{
FILE *const f = m_File;
auto lines = &m_Lines;
m_Disk->addJob([f, lines]() { Flush(lines, f); });
m_LastFlush = now;
}
} // namespace llarp

View File

@ -30,9 +30,11 @@ namespace llarp
Tick(llarp_time_t now) override; Tick(llarp_time_t now) override;
void void
PostLog(std::stringstream&) const override PostLog(std::stringstream&) const override{};
{
} void
AppendLog(LogLevel lvl, const char* fname, int lineno,
const std::string& nodename, const std::string msg) override;
using Lines_t = thread::Queue< std::string >; using Lines_t = thread::Queue< std::string >;
@ -40,6 +42,9 @@ namespace llarp
Lines_t m_Lines; Lines_t m_Lines;
private: private:
static void
Flush(Lines_t* const, FILE* const);
bool bool
ShouldFlush(llarp_time_t now) const; ShouldFlush(llarp_time_t now) const;

View File

@ -14,7 +14,7 @@ namespace llarp
obj["line"] = lineno; obj["line"] = lineno;
obj["level"] = LogLevelToString(lvl); obj["level"] = LogLevelToString(lvl);
obj["message"] = msg; obj["message"] = msg;
m_Lines.emplace_back(obj.dump()); m_Lines.pushBack(obj.dump());
} }
} // namespace llarp } // namespace llarp