flush helper count writes instead of time

This commit is contained in:
gabime
2014-03-14 14:35:46 +02:00
parent 9d687d1634
commit 38670cef27
14 changed files with 139 additions and 94 deletions

View File

@@ -1,36 +1,31 @@
#pragma once
#include <chrono>
#include <iostream>
// Flush to file every X writes..
namespace c11log
{
namespace details
{
class file_flush_helper
{
public:
explicit file_flush_helper(const std::chrono::milliseconds &flush_every): _flush_every(flush_every), _last_flush() {};
explicit file_flush_helper(const std::size_t flush_every):
_flush_every(flush_every),
_write_counter(0) {};
void write(std::ofstream& ofs, const std::string& msg) {
ofs << msg;
//If zero - flush every time
if(_flush_every == std::chrono::milliseconds::min()) {
void write(std::ofstream& ofs, const std::string& msg)
{
ofs.write(msg.c_str(), msg.size());
if(++_write_counter == _flush_every)
{
ofs.flush();
} else {
auto now = std::chrono::system_clock::now();
if(now - _last_flush >= _flush_every) {
ofs.flush();
_last_flush = now;
}
_write_counter = 0;
}
}
private:
std::chrono::milliseconds _flush_every;
std::chrono::system_clock::time_point _last_flush;
const std::size_t _flush_every;
std::size_t _write_counter;
};
}
}