2014-02-22 11:57:53 +02:00
|
|
|
#pragma once
|
|
|
|
|
// Flush to file every X writes..
|
2014-10-10 21:17:26 +03:00
|
|
|
// If X is zero than never flush..
|
2014-02-22 11:57:53 +02:00
|
|
|
|
2014-03-07 00:52:50 +02:00
|
|
|
namespace c11log
|
|
|
|
|
{
|
|
|
|
|
namespace details
|
|
|
|
|
{
|
2014-03-14 14:35:46 +02:00
|
|
|
|
2014-03-07 00:52:50 +02:00
|
|
|
class file_flush_helper
|
|
|
|
|
{
|
2014-02-22 11:57:53 +02:00
|
|
|
public:
|
2014-03-14 14:35:46 +02:00
|
|
|
explicit file_flush_helper(const std::size_t flush_every):
|
|
|
|
|
_flush_every(flush_every),
|
2014-05-09 17:09:25 +03:00
|
|
|
_flush_countdown(flush_every) {};
|
2014-05-09 18:00:10 +03:00
|
|
|
|
2014-10-10 21:17:26 +03:00
|
|
|
file_flush_helper(const file_flush_helper&) = delete;
|
|
|
|
|
|
2014-05-09 18:00:10 +03:00
|
|
|
void write(const std::string& msg, std::ofstream& ofs)
|
2014-03-14 14:35:46 +02:00
|
|
|
{
|
2014-05-08 02:23:07 +03:00
|
|
|
ofs.write(msg.data(), msg.size());
|
2014-05-09 17:09:25 +03:00
|
|
|
if(--_flush_countdown == 0)
|
2014-03-14 14:35:46 +02:00
|
|
|
{
|
2014-02-22 11:57:53 +02:00
|
|
|
ofs.flush();
|
2014-05-09 17:09:25 +03:00
|
|
|
_flush_countdown = _flush_every;
|
2014-02-22 11:57:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2014-03-14 14:35:46 +02:00
|
|
|
const std::size_t _flush_every;
|
2014-05-09 17:09:25 +03:00
|
|
|
std::size_t _flush_countdown;
|
2014-02-22 11:57:53 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|