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

@@ -1,18 +1,18 @@
#pragma once
#include<string>
#include<chrono>
#include<functional>
#include <string>
#include <chrono>
#include <functional>
#include <sstream>
#include <iomanip>
#include "level.h"
#include "common_types.h"
#include "details/os.h"
namespace c11log {
namespace formatters {
typedef std::chrono::system_clock clock;
typedef clock::time_point time_point;
typedef std::function<std::string(const std::string& logger_name, const std::string&, level::level_enum, const time_point&)> format_fn;
typedef std::function<std::string(const std::string& logger_name, const std::string&, level::level_enum, const c11log::log_clock::time_point&)> format_fn;
std::string to_hex(const unsigned char* buf, std::size_t size);
@@ -21,19 +21,19 @@ class formatter {
public:
formatter() {}
virtual ~formatter() {}
virtual void format_header(const std::string& logger_name, level::level_enum level, const time_point& tp, std::ostream& dest) = 0;
virtual void format_header(const std::string& logger_name, level::level_enum level, const log_clock::time_point& tp, std::ostream& dest) = 0;
};
class default_formatter: public formatter {
public:
// Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body
void format_header(const std::string& logger_name, level::level_enum level, const time_point& tp, std::ostream& dest) override {
void format_header(const std::string& logger_name, level::level_enum level, const log_clock::time_point& tp, std::ostream& dest) override {
_format_time(tp, dest);
dest << " [" << logger_name << ":" << c11log::level::to_str(level) << "] ";
}
private:
void _format_time(const time_point& tp, std::ostream &dest);
void _format_time(const log_clock::time_point& tp, std::ostream &dest);
};
} //namespace formatter
@@ -41,23 +41,19 @@ private:
inline void c11log::formatters::default_formatter::_format_time(const time_point& tp, std::ostream &dest)
inline void c11log::formatters::default_formatter::_format_time(const log_clock::time_point& tp, std::ostream &dest)
{
using namespace std::chrono;
static thread_local c11log::formatters::time_point last_tp;
static thread_local char timestamp_cache[64];
auto tm = details::os::localtime(log_clock::to_time_t(tp));
char buff[64];
int size = snprintf(buff, sizeof(buff), "[%d-%02d-%02d %02d:%02d:%02d]",
tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec);
if(duration_cast<milliseconds>(tp-last_tp).count() > 950) {
auto tm = details::os::localtime(clock::to_time_t(tp));
sprintf(timestamp_cache, "[%d-%02d-%02d %02d:%02d:%02d]", tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec);
last_tp = tp;
}
dest << timestamp_cache;
dest.write(buff, size);
}