use_profiler.cpp

Boost Logging library

Author: John Torjo, www.torjo.com

Copyright (C) 2007 John Torjo (see www.torjo.com for email)

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

See http://www.boost.org for updates, documentation, and revision history. See http://www.torjo.com/log2/ for more details

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


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