timepoint

This commit is contained in:
gabime 2014-02-06 01:21:40 +02:00
parent 6400b1887e
commit 1579b24e78
4 changed files with 23 additions and 42 deletions

View File

@ -10,9 +10,10 @@
namespace c11log { namespace c11log {
namespace formatters { namespace formatters {
typedef std::chrono::system_clock::time_point timepoint; typedef std::chrono::system_clock clock;
typedef std::function<std::string(const std::string& logger_name, const std::string&, level::level_enum, const timepoint&)> format_fn; typedef clock::time_point time_point;
void format_time(const timepoint& tp, std::ostream &dest); typedef std::function<std::string(const std::string& logger_name, const std::string&, level::level_enum, const time_point&)> format_fn;
void format_time(const time_point& tp, std::ostream &dest);
void format_time(std::ostream &dest); void format_time(std::ostream &dest);
std::string to_hex(const unsigned char* buf, std::size_t size); std::string to_hex(const unsigned char* buf, std::size_t size);
@ -20,14 +21,14 @@ class formatter {
public: public:
formatter() {} formatter() {}
virtual ~formatter() {} virtual ~formatter() {}
virtual void format_header(const std::string& logger_name, level::level_enum level, const timepoint& tp, std::ostream& dest) = 0; virtual void format_header(const std::string& logger_name, level::level_enum level, const time_point& tp, std::ostream& dest) = 0;
}; };
class default_formatter: public formatter { class default_formatter: public formatter {
public: public:
// Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body // 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 timepoint& tp, std::ostream& dest) override void format_header(const std::string& logger_name, level::level_enum level, const time_point& tp, std::ostream& dest) override
{ {
format_time(tp, dest); format_time(tp, dest);
dest << " [" << logger_name << ":" << c11log::level::to_str(level) << "] "; dest << " [" << logger_name << ":" << c11log::level::to_str(level) << "] ";

View File

@ -6,23 +6,26 @@
static thread_local std::tm last_tm;
static thread_local c11log::formatters::time_point last_tp;
static thread_local char timestamp_cache[64]; static thread_local char timestamp_cache[64];
void c11log::formatters::format_time(const c11log::formatters::timepoint& tp, std::ostream &dest) void c11log::formatters::format_time(const time_point& tp, std::ostream &dest)
{ {
auto tm = details::os::localtime(std::chrono::system_clock::to_time_t(tp)); using namespace std::chrono;
// Cache timestamp string of last second // Cache timestamp string of last second
if(memcmp(&tm, &last_tm, sizeof(tm))) if(duration_cast<seconds>(tp-last_tp).count() >= 1)
{ {
auto tm = details::os::localtime(std::chrono::system_clock::to_time_t(tp));
sprintf(timestamp_cache, "[%d-%02d-%02d %02d:%02d:%02d]", tm.tm_year + 1900, sprintf(timestamp_cache, "[%d-%02d-%02d %02d:%02d:%02d]", tm.tm_year + 1900,
tm.tm_mon + 1, tm.tm_mon + 1,
tm.tm_mday, tm.tm_mday,
tm.tm_hour, tm.tm_hour,
tm.tm_min, tm.tm_min,
tm.tm_sec); tm.tm_sec);
last_tm = tm; last_tp = tp;
} }
dest << timestamp_cache; dest << timestamp_cache;
@ -30,7 +33,7 @@ void c11log::formatters::format_time(const c11log::formatters::timepoint& tp, st
void c11log::formatters::format_time(std::ostream& dest) void c11log::formatters::format_time(std::ostream& dest)
{ {
return format_time(std::chrono::system_clock::now(), dest); return format_time(c11log::formatters::clock::now(), dest);
} }

View File

@ -7,7 +7,7 @@ c11log::details::line_logger::line_logger(logger* callback_logger, level::level_
if (callback_logger) { if (callback_logger) {
callback_logger->formatter_->format_header(callback_logger->logger_name_, callback_logger->formatter_->format_header(callback_logger->logger_name_,
msg_level, msg_level,
c11log::formatters::timepoint::clock::now(), c11log::formatters::clock::now(),
_oss); _oss);
} }
} }

View File

@ -1,6 +1,5 @@
// test.cpp : Defines the entry point for the console application. // test.cpp : Defines the entry point for the console application.
// //
#include "stdafx.h" #include "stdafx.h"
#include <functional> #include <functional>
@ -28,30 +27,11 @@ void pusher(Q* q)
{ {
logger.info()<<"Hello logger!"; logger.info()<<"Hello logger!";
++push_count; ++push_count;
} }
/*
string a = "Hello";
while(active)
{
q->push(a);
++push_count;
}
*/
} }
void popper(Q* q)
{
string output;
while(active)
{
q->pop(output);
++pop_count;
}
}
void testq(int size, int pushers, int poppers) void testq(int size, int pushers, int poppers)
{ {
@ -108,10 +88,7 @@ int main(int argc, char* argv[])
auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("newlog", "txt", 1024*1024*10 , 2); auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("newlog", "txt", 1024*1024*10 , 2);
//auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt"); //auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt");
async->add_sink(fsink);
//Async logger
async->add_sink(null_sink);
auto &logger = c11log::get_logger("async"); auto &logger = c11log::get_logger("async");
logger.add_sink(async); logger.add_sink(async);