mirror of https://github.com/gabime/spdlog.git
file helper
parent
8fb32dcb65
commit
b94ca27ce4
@ -0,0 +1,90 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Helper class for file sink
|
||||||
|
// When failing to open a file, retry several times(5) with small delay between the tries(10 ms)
|
||||||
|
// Flush to file every X writes (or never if X==0)
|
||||||
|
// Throw fflog_ exception on errors
|
||||||
|
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
|
#include "../common.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace c11log
|
||||||
|
{
|
||||||
|
namespace details
|
||||||
|
{
|
||||||
|
|
||||||
|
class file_helper
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const int open_max_tries = 5;
|
||||||
|
static const int sleep_ms_bewteen_tries = 10;
|
||||||
|
|
||||||
|
explicit file_helper(const std::size_t flush_inverval):
|
||||||
|
_fd(nullptr),
|
||||||
|
_flush_inverval(flush_inverval),
|
||||||
|
_flush_countdown(flush_inverval) {};
|
||||||
|
|
||||||
|
file_helper(const file_helper&) = delete;
|
||||||
|
|
||||||
|
~file_helper()
|
||||||
|
{
|
||||||
|
if (_fd)
|
||||||
|
std::fclose(_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void open(const std::string& filename)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (_fd)
|
||||||
|
std::fclose(_fd);
|
||||||
|
|
||||||
|
_filename = filename;
|
||||||
|
for (int tries = 0; tries < open_max_tries; ++tries)
|
||||||
|
{
|
||||||
|
if(!os::fopen_s(&_fd, filename, "wb"))
|
||||||
|
return;
|
||||||
|
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms_bewteen_tries));
|
||||||
|
}
|
||||||
|
|
||||||
|
throw fflog_exception("Failed opening file " + filename + " for writing");
|
||||||
|
}
|
||||||
|
|
||||||
|
void close()
|
||||||
|
{
|
||||||
|
std::fclose(_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(const log_msg& msg)
|
||||||
|
{
|
||||||
|
auto& buf = msg.formatted.buf();
|
||||||
|
size_t size = buf.size();
|
||||||
|
if(std::fwrite(buf.data(), sizeof(char), size, _fd) != size)
|
||||||
|
throw fflog_exception("Failed writing to file " + _filename);
|
||||||
|
|
||||||
|
if(--_flush_countdown == 0)
|
||||||
|
{
|
||||||
|
std::fflush(_fd);
|
||||||
|
_flush_countdown = _flush_inverval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
FILE* _fd;
|
||||||
|
std::string _filename;
|
||||||
|
const std::size_t _flush_inverval;
|
||||||
|
std::size_t _flush_countdown;
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,36 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
// Flush to file every X writes..
|
|
||||||
// If X is zero than never flush..
|
|
||||||
|
|
||||||
namespace c11log
|
|
||||||
{
|
|
||||||
namespace details
|
|
||||||
{
|
|
||||||
|
|
||||||
class file_flush_helper
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit file_flush_helper(const std::size_t flush_every):
|
|
||||||
_flush_every(flush_every),
|
|
||||||
_flush_countdown(flush_every) {};
|
|
||||||
|
|
||||||
file_flush_helper(const file_flush_helper&) = delete;
|
|
||||||
|
|
||||||
void write(const log_msg& msg, std::ofstream& ofs)
|
|
||||||
{
|
|
||||||
auto& buf = msg.formatted.buf();
|
|
||||||
ofs.write(buf.data(), buf.size());
|
|
||||||
if(--_flush_countdown == 0)
|
|
||||||
{
|
|
||||||
ofs.flush();
|
|
||||||
_flush_countdown = _flush_every;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
const std::size_t _flush_every;
|
|
||||||
std::size_t _flush_countdown;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue