From 1579b24e7892dda6aa95e77206ffb11ad4c3b38a Mon Sep 17 00:00:00 2001 From: gabime Date: Thu, 6 Feb 2014 01:21:40 +0200 Subject: [PATCH] timepoint --- include/c11log/formatters/formatters.h | 11 +++++---- src/formatters.cpp | 17 +++++++------ src/line_logger.cpp | 4 ++-- src/test.cpp | 33 ++++---------------------- 4 files changed, 23 insertions(+), 42 deletions(-) diff --git a/include/c11log/formatters/formatters.h b/include/c11log/formatters/formatters.h index 49c1d662..6ad3ccff 100644 --- a/include/c11log/formatters/formatters.h +++ b/include/c11log/formatters/formatters.h @@ -10,9 +10,10 @@ namespace c11log { namespace formatters { -typedef std::chrono::system_clock::time_point timepoint; -typedef std::function format_fn; -void format_time(const timepoint& tp, std::ostream &dest); +typedef std::chrono::system_clock clock; +typedef clock::time_point time_point; +typedef std::function format_fn; +void format_time(const time_point& tp, std::ostream &dest); void format_time(std::ostream &dest); std::string to_hex(const unsigned char* buf, std::size_t size); @@ -20,14 +21,14 @@ class formatter { public: 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 { 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 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); dest << " [" << logger_name << ":" << c11log::level::to_str(level) << "] "; diff --git a/src/formatters.cpp b/src/formatters.cpp index 7233b69f..b6eb5f1f 100644 --- a/src/formatters.cpp +++ b/src/formatters.cpp @@ -6,31 +6,34 @@ -static thread_local std::tm last_tm; + +static thread_local c11log::formatters::time_point last_tp; 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 - if(memcmp(&tm, &last_tm, sizeof(tm))) + if(duration_cast(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, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - last_tm = tm; + last_tp = tp; } - + dest << timestamp_cache; } 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); } diff --git a/src/line_logger.cpp b/src/line_logger.cpp index 77c8708c..e037b084 100644 --- a/src/line_logger.cpp +++ b/src/line_logger.cpp @@ -7,7 +7,7 @@ c11log::details::line_logger::line_logger(logger* callback_logger, level::level_ if (callback_logger) { callback_logger->formatter_->format_header(callback_logger->logger_name_, msg_level, - c11log::formatters::timepoint::clock::now(), + c11log::formatters::clock::now(), _oss); } } @@ -18,4 +18,4 @@ c11log::details::line_logger::~line_logger() _oss << '\n'; _callback_logger->log_it_(_oss.str_ref()); } -} \ No newline at end of file +} diff --git a/src/test.cpp b/src/test.cpp index 03dc178b..71e64af1 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -1,6 +1,5 @@ // test.cpp : Defines the entry point for the console application. // - #include "stdafx.h" #include @@ -27,32 +26,13 @@ void pusher(Q* q) while(active) { 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) { @@ -107,11 +87,8 @@ int main(int argc, char* argv[]) auto async = std::make_shared(1000); auto fsink = std::make_shared("newlog", "txt", 1024*1024*10 , 2); //auto fsink = std::make_shared("daily", "txt"); - - - - //Async logger - async->add_sink(null_sink); + + async->add_sink(fsink); auto &logger = c11log::get_logger("async"); logger.add_sink(async);