mirror of https://github.com/oxen-io/lokinet
add disk worker based file flusher logger
make format remove package.jsonpull/523/head
parent
a45d6db0e0
commit
9503cc66f0
@ -1,12 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "lokinet",
|
|
||||||
"version": "0.0.0",
|
|
||||||
"description": "lokinet god awful node binding",
|
|
||||||
"main": "lokinet.js",
|
|
||||||
"private": true,
|
|
||||||
"gypfile": true,
|
|
||||||
"dependencies": {
|
|
||||||
"bindings": "~1.2.1",
|
|
||||||
"node-addon-api": "^1.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,89 @@
|
|||||||
|
#include <util/file_logger.hpp>
|
||||||
|
#include <util/logger_internal.hpp>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
FileLogStream::FileLogStream(llarp_threadpool *disk, FILE *f,
|
||||||
|
llarp_time_t flushInterval)
|
||||||
|
: m_Disk(disk), m_File(f), m_FlushInterval(flushInterval)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
FileLogStream::~FileLogStream()
|
||||||
|
{
|
||||||
|
fflush(m_File);
|
||||||
|
fclose(m_File);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
FileLogStream::ShouldFlush(llarp_time_t now) const
|
||||||
|
{
|
||||||
|
if(m_LastFlush >= now)
|
||||||
|
return false;
|
||||||
|
const auto dlt = now - m_LastFlush;
|
||||||
|
return dlt >= m_FlushInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
FileLogStream::PreLog(std::stringstream &ss, LogLevel lvl, const char *fname,
|
||||||
|
int lineno) const
|
||||||
|
{
|
||||||
|
switch(lvl)
|
||||||
|
{
|
||||||
|
case eLogNone:
|
||||||
|
break;
|
||||||
|
case eLogDebug:
|
||||||
|
ss << "[DBG] ";
|
||||||
|
break;
|
||||||
|
case eLogInfo:
|
||||||
|
ss << "[NFO] ";
|
||||||
|
break;
|
||||||
|
case eLogWarn:
|
||||||
|
|
||||||
|
ss << "[WRN] ";
|
||||||
|
break;
|
||||||
|
case eLogError:
|
||||||
|
ss << "[ERR] ";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ss << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname
|
||||||
|
<< ":" << lineno << "\t";
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
FileLogStream::Print(LogLevel, const char *, const std::string &msg)
|
||||||
|
{
|
||||||
|
m_Lines.emplace_back(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
FileLogStream::Tick(llarp_time_t now)
|
||||||
|
{
|
||||||
|
if(ShouldFlush(now))
|
||||||
|
FlushLinesToDisk(now);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
FileLogStream::FlushLinesToDisk(llarp_time_t now)
|
||||||
|
{
|
||||||
|
FlushEvent *ev = new FlushEvent(std::move(m_Lines), m_File);
|
||||||
|
llarp_threadpool_queue_job(m_Disk, {ev, &FlushEvent::HandleFlush});
|
||||||
|
m_LastFlush = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
FileLogStream::FlushEvent::HandleFlush(void *user)
|
||||||
|
{
|
||||||
|
static_cast< FileLogStream::FlushEvent * >(user)->Flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
FileLogStream::FlushEvent::Flush()
|
||||||
|
{
|
||||||
|
for(const auto &line : lines)
|
||||||
|
fprintf(f, "%s\n", line.c_str());
|
||||||
|
fflush(f);
|
||||||
|
delete this;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace llarp
|
@ -0,0 +1,61 @@
|
|||||||
|
#ifndef LLARP_UTIL_FILE_LOGGER_HPP
|
||||||
|
#define LLARP_UTIL_FILE_LOGGER_HPP
|
||||||
|
|
||||||
|
#include <util/logstream.hpp>
|
||||||
|
#include <util/threadpool.h>
|
||||||
|
#include <util/time.hpp>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
/// fluhsable file based log stream
|
||||||
|
struct FileLogStream : public ILogStream
|
||||||
|
{
|
||||||
|
FileLogStream(llarp_threadpool* disk, FILE* f, llarp_time_t flushInterval);
|
||||||
|
|
||||||
|
~FileLogStream();
|
||||||
|
|
||||||
|
void
|
||||||
|
PreLog(std::stringstream& out, LogLevel lvl, const char* fname,
|
||||||
|
int lineno) const override;
|
||||||
|
|
||||||
|
void
|
||||||
|
Print(LogLevel, const char*, const std::string& msg) override;
|
||||||
|
|
||||||
|
void
|
||||||
|
Tick(llarp_time_t now) override;
|
||||||
|
|
||||||
|
void
|
||||||
|
PostLog(std::stringstream&) const override{};
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct FlushEvent
|
||||||
|
{
|
||||||
|
FlushEvent(std::deque< std::string > l, FILE* file)
|
||||||
|
: lines(std::move(l)), f(file)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::deque< std::string > lines;
|
||||||
|
FILE* const f;
|
||||||
|
|
||||||
|
void
|
||||||
|
Flush();
|
||||||
|
static void
|
||||||
|
HandleFlush(void*);
|
||||||
|
};
|
||||||
|
|
||||||
|
bool
|
||||||
|
ShouldFlush(llarp_time_t now) const;
|
||||||
|
|
||||||
|
void
|
||||||
|
FlushLinesToDisk(llarp_time_t now);
|
||||||
|
|
||||||
|
llarp_threadpool* m_Disk;
|
||||||
|
FILE* m_File;
|
||||||
|
const llarp_time_t m_FlushInterval;
|
||||||
|
llarp_time_t m_LastFlush = 0;
|
||||||
|
std::deque< std::string > m_Lines;
|
||||||
|
};
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue