flush every period

This commit is contained in:
gabime
2014-03-01 14:06:58 +02:00
parent 6f4c780089
commit ddcf45d65f
11 changed files with 105 additions and 76 deletions

View File

@@ -58,7 +58,7 @@ public:
// If the queue is full, block the calling thread until there is room.
template<typename TT>
void push(TT&& item) {
while (!push(std::forward<TT>(item), one_hour));
while (!push(std::forward<TT>(item), _one_hour));
}
// Pop a copy of the front item in the queue into the given item ref.
@@ -85,7 +85,7 @@ public:
// Pop a copy of the front item in the queue into the given item ref.
// If the queue is empty, block the calling thread util there is item to pop.
void pop(T& item) {
while (!pop(item, one_hour));
while (!pop(item, _one_hour));
}
// Clear the queue
@@ -103,7 +103,7 @@ private:
std::mutex _mutex;
std::condition_variable _item_pushed_cond;
std::condition_variable _item_popped_cond;
static constexpr auto one_hour = std::chrono::hours(1);
static constexpr auto _one_hour = std::chrono::hours(1);
};
}

View File

@@ -1,24 +1,33 @@
#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(std::size_t flush_every): _flush_every(flush_every), _write_counter(0) {};
explicit file_flush_helper(const std::chrono::milliseconds &flush_every): _flush_every(flush_every), _last_flush() {};
void write(std::ofstream& ofs, const std::string& msg) {
ofs << msg;
if(++_write_counter >= _flush_every) {
//If zero - flush every time
if(_flush_every == std::chrono::milliseconds::min()) {
ofs.flush();
_write_counter = 0;
} else {
auto now = std::chrono::system_clock::now();
if(now - _last_flush >= _flush_every) {
ofs.flush();
_last_flush = now;
}
}
}
private:
std::size_t _flush_every;
std::size_t _write_counter;
std::chrono::milliseconds _flush_every;
std::chrono::system_clock::time_point _last_flush;
};
}
}

View File

@@ -1,6 +1,6 @@
#pragma once
#include "../level.h"
#include "../common_types.h"
#include "../logger.h"
#include "fast_oss.h"
@@ -16,7 +16,7 @@ public:
_level(msg_level) {
callback_logger->_formatter->format_header(callback_logger->_logger_name,
msg_level,
c11log::formatters::clock::now(),
log_clock::now(),
_oss);
}
line_logger(logger*):_callback_logger(nullptr) {};