use_profiler.cpp

This usage:

Optimizations:

If logging on dedicated thread, the output for "profile.txt" could look like:

00001 gather time:      0.796875 seconds 
00002 write time:       0.78125 seconds 
00003 filter time:      0.15625 seconds 
00004 otherthread time: 1.156250 seconds 



If logging on same thread, the output for "profile.txt" could look like:

00001 gather time:      5.562500 seconds 
00002 write time:       5.265625 seconds 
00003 filter time:      0.31250 seconds 
00004 otherthread time: 0.0 seconds 

00001 
00046 // if this is defined, we do the logging on a dedicated thread
00047 // otherwise, logging happens in the thread that does the logging
00048 //
00049 // comment/uncomment this and play with this sample, to see the differences...
00050 #define PROFILE_ON_DEDICATED_THREAD
00051 
00052 
00053 #include <boost/logging/format_fwd.hpp>
00054 
00055 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> )
00056 
00057 #include <boost/logging/format_ts.hpp>
00058 #include <boost/logging/profile.hpp>
00059 
00060 namespace bl = boost::logging;
00061 
00063 // Profiling code
00064 
00065 #if defined(PROFILE_ON_DEDICATED_THREAD)
00066 typedef bl::logger_format_write< bl::default_, bl::default_, bl::writer::threading::on_dedicated_thread > raw_log_type;
00067 #else
00068 typedef bl::logger_format_write< > raw_log_type;
00069 #endif
00070 typedef bl::profile::compute_for_logger<raw_log_type>::type logger_type;
00071 
00072 typedef bl::profile::compute_for_filter<bl::filter::no_ts>::type filter_type;
00073 
00074 // END OF Profiling code
00076 
00077 
00078 
00079 
00080 #define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() ) 
00081 
00082 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter_type ) 
00083 BOOST_DEFINE_LOG(g_l, logger_type)
00084 
00085 void do_sleep(int ms) {
00086     using namespace boost;
00087     xtime next;
00088     xtime_get( &next, TIME_UTC);
00089     next.nsec += (ms % 1000) * 1000000;
00090 
00091     int nano_per_sec = 1000000000;
00092     next.sec += next.nsec / nano_per_sec;
00093     next.sec += ms / 1000;
00094     next.nsec %= nano_per_sec;
00095     thread::sleep( next);
00096 }
00097 
00098 
00099 void one_logger_one_filter_example() {
00100     g_l()->writer().add_formatter(      bl::formatter::idx(), "[%] "  );
00101     g_l()->writer().add_formatter(      bl::formatter::append_newline() );
00102     g_l()->writer().add_destination(    bl::destination::file("out.txt") );
00103     g_l()->writer().add_destination(    bl::destination::cout() );
00104     g_l()->writer().add_destination(    bl::destination::dbg_window() );
00105     g_l()->mark_as_initialized();
00106 
00107     // where shall the profile results be outputted?
00108     bl::profile::compute::inst().log_results( bl::destination::file("profile.txt") );
00109 
00110     for ( int i = 0; i < 5000; ++i)
00111         L_ << "this is so cool " << i;
00112 
00113 #if defined(PROFILE_ON_DEDICATED_THREAD)
00114     std::cout << "waiting for logging to finish" << std::endl;
00115     // wait for the logging to take place
00116     do_sleep(1000);
00117 #endif
00118 }
00119 
00120 
00121 
00122 
00123 int main() {
00124     one_logger_one_filter_example();
00125 }
00126 
00127 
00128 // End of file
00129 


Copyright John Torjo © 2007
Have a question/ suggestion/ comment? Send me feedback