#define WIN32_LEAN_AND_MEAN #include #include #include #include #include #include #include #include #include #include #include #include namespace src = boost::log::sources; namespace sinks = boost::log::sinks; namespace attrs = boost::log::attributes; class PreciseTimer : public boost::log::attribute { public: typedef attrs::timer::value_type value_type; PreciseTimer() : boost::log::attribute(new PreciseTimerImpl) {} explicit PreciseTimer(boost::log::attributes::cast_source const& source) : boost::log::attribute(source.as< PreciseTimerImpl >()) { } struct PreciseTimerImpl : public boost::log::attribute::impl { PreciseTimerImpl() { QueryPerformanceCounter(&m_startCounter); QueryPerformanceFrequency(&m_freq); } virtual boost::log::attribute_value get_value() { LARGE_INTEGER nowCounter; QueryPerformanceCounter(&nowCounter); double e = (nowCounter.QuadPart - m_startCounter.QuadPart) / (double) m_freq.QuadPart; return boost::log::attribute_value(new boost::log::attributes::attribute_value_impl< value_type > (boost::posix_time::microsec(static_cast(e*1000000.0)))); } LARGE_INTEGER m_freq, m_startCounter; }; }; BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp_id, "TimeStamp", attrs::local_clock::value_type) BOOST_LOG_ATTRIBUTE_KEYWORD(timer_id, "Timer", attrs::timer::value_type) BOOST_LOG_ATTRIBUTE_KEYWORD(timer_precise_id, "PreciseTimer", PreciseTimer::value_type) int protectedMain(int argc, char ** argv) { attrs::timer timerObject; boost::posix_time::ptime timerReference = boost::posix_time::microsec_clock::local_time(); auto core = boost::log::core::get(); core->add_global_attribute("TimeStamp", attrs::local_clock()); core->add_global_attribute("Timer", timerObject); core->add_global_attribute("PreciseTimer", PreciseTimer()); core->remove_all_sinks(); auto sink = boost::make_shared< sinks::synchronous_sink< sinks::text_ostream_backend > >(); boost::shared_ptr out(new std::ofstream("output.log")); sink->locked_backend()->add_stream(out); sink->set_formatter([&timerReference] (boost::log::record_view const& rec, boost::log::formatting_ostream& strm) { auto & timerVal1 = rec[timer_id]; auto & timerVal2 = rec[timer_precise_id]; auto & clockVal = rec[timestamp_id]; auto estimatedClock1 = timerReference + timerVal1.get(); auto estimatedClock2 = timerReference + timerVal2.get(); strm << "clock: " << boost::posix_time::to_iso_extended_string(clockVal.get()) << " " << "timer1: " << boost::posix_time::to_iso_extended_string(estimatedClock1) << " " << "diff1: " << (clockVal.get() - estimatedClock1).total_milliseconds() << "ms " << "timer2: " << boost::posix_time::to_iso_extended_string(estimatedClock2) << " " << "diff2: " << (clockVal.get() - estimatedClock2).total_milliseconds() << "ms " ; }); core->add_sink(sink); boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time() + boost::posix_time::seconds(1); while(boost::posix_time::microsec_clock::local_time() < end){ BOOST_LOG_TRIVIAL(info) << "Test"; } core->remove_all_sinks(); return 0; } int main(int argc, char ** argv) { try { return protectedMain(argc, argv); } catch(std::exception const & e){ std::cerr << e.what() << std::endl; } return 0; }