2019-04-16 13:20:48 +00:00
|
|
|
#ifndef LLARP_UTIL_FILE_LOGGER_HPP
|
|
|
|
#define LLARP_UTIL_FILE_LOGGER_HPP
|
|
|
|
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logstream.hpp>
|
|
|
|
|
2019-11-15 21:10:51 +00:00
|
|
|
#include <util/thread/queue.hpp>
|
2019-04-16 13:20:48 +00:00
|
|
|
#include <util/time.hpp>
|
|
|
|
|
2019-05-18 18:46:49 +00:00
|
|
|
#include <deque>
|
|
|
|
|
2019-04-16 13:20:48 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-04-24 23:27:31 +00:00
|
|
|
/// flushable file based log stream
|
2019-04-16 13:20:48 +00:00
|
|
|
struct FileLogStream : public ILogStream
|
|
|
|
{
|
2020-06-11 11:44:02 +00:00
|
|
|
using Work_t = std::function<void(void)>;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
FileLogStream(
|
2020-06-11 11:44:02 +00:00
|
|
|
std::function<void(Work_t)> io, FILE* f, llarp_time_t flushInterval, bool closefile = true);
|
2019-04-16 13:20:48 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
~FileLogStream() override;
|
2019-04-16 13:20:48 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
PreLog(
|
|
|
|
std::stringstream& out,
|
|
|
|
LogLevel lvl,
|
|
|
|
const char* fname,
|
|
|
|
int lineno,
|
|
|
|
const std::string& nodename) const override;
|
2019-04-16 13:20:48 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Print(LogLevel, const char*, const std::string& msg) override;
|
|
|
|
|
|
|
|
void
|
|
|
|
Tick(llarp_time_t now) override;
|
|
|
|
|
|
|
|
void
|
2019-11-15 21:16:46 +00:00
|
|
|
PostLog(std::stringstream&) const override{};
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
AppendLog(
|
|
|
|
LogLevel lvl,
|
|
|
|
const char* fname,
|
|
|
|
int lineno,
|
|
|
|
const std::string& nodename,
|
|
|
|
const std::string msg) override;
|
2019-04-16 13:20:48 +00:00
|
|
|
|
2020-04-02 16:47:56 +00:00
|
|
|
virtual void
|
|
|
|
ImmediateFlush() override;
|
|
|
|
|
2020-04-07 20:41:11 +00:00
|
|
|
using Lines_t = thread::Queue<std::string>;
|
2019-11-15 21:10:51 +00:00
|
|
|
|
2019-06-13 13:26:34 +00:00
|
|
|
protected:
|
2019-11-15 21:10:51 +00:00
|
|
|
Lines_t m_Lines;
|
2019-06-13 13:26:34 +00:00
|
|
|
|
2019-04-16 13:20:48 +00:00
|
|
|
private:
|
2019-11-15 21:16:46 +00:00
|
|
|
static void
|
|
|
|
Flush(Lines_t* const, FILE* const);
|
|
|
|
|
2019-04-16 13:20:48 +00:00
|
|
|
bool
|
|
|
|
ShouldFlush(llarp_time_t now) const;
|
|
|
|
|
|
|
|
void
|
|
|
|
FlushLinesToDisk(llarp_time_t now);
|
|
|
|
|
2020-06-11 11:44:02 +00:00
|
|
|
const std::function<void(Work_t)> m_Disk;
|
2019-06-13 13:26:34 +00:00
|
|
|
FILE* const m_File;
|
2019-04-16 13:20:48 +00:00
|
|
|
const llarp_time_t m_FlushInterval;
|
2020-02-24 19:40:45 +00:00
|
|
|
llarp_time_t m_LastFlush = 0s;
|
2019-06-13 13:26:34 +00:00
|
|
|
const bool m_Close;
|
2019-04-16 13:20:48 +00:00
|
|
|
};
|
|
|
|
} // namespace llarp
|
|
|
|
|
2019-04-24 23:27:31 +00:00
|
|
|
#endif
|