2014-02-22 17:57:53 +08:00
|
|
|
#pragma once
|
2014-03-01 20:06:58 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <iostream>
|
2014-02-22 17:57:53 +08:00
|
|
|
// Flush to file every X writes..
|
|
|
|
|
2014-03-07 06:52:50 +08:00
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
class file_flush_helper
|
|
|
|
{
|
2014-02-22 17:57:53 +08:00
|
|
|
public:
|
2014-03-01 20:06:58 +08:00
|
|
|
explicit file_flush_helper(const std::chrono::milliseconds &flush_every): _flush_every(flush_every), _last_flush() {};
|
2014-02-22 17:57:53 +08:00
|
|
|
|
|
|
|
void write(std::ofstream& ofs, const std::string& msg) {
|
2014-03-01 20:06:58 +08:00
|
|
|
|
|
|
|
|
2014-02-22 17:57:53 +08:00
|
|
|
ofs << msg;
|
2014-03-01 20:06:58 +08:00
|
|
|
//If zero - flush every time
|
|
|
|
if(_flush_every == std::chrono::milliseconds::min()) {
|
2014-02-22 17:57:53 +08:00
|
|
|
ofs.flush();
|
2014-03-01 20:06:58 +08:00
|
|
|
} else {
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
if(now - _last_flush >= _flush_every) {
|
|
|
|
ofs.flush();
|
|
|
|
_last_flush = now;
|
|
|
|
}
|
2014-02-22 17:57:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-03-01 20:06:58 +08:00
|
|
|
std::chrono::milliseconds _flush_every;
|
|
|
|
std::chrono::system_clock::time_point _last_flush;
|
2014-02-22 17:57:53 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|