ded_loger_one_filter.cpp

This usage:

Optimizations:

In this example, all output will be written to the console, debug window, and "out.txt" file. It will look similar to:

00001 ...
00002 30:33 [10] message 1
00003 30:33 [11] message 2
00004 30:33 [12] message 2
00005 30:33 [13] message 2
00006 30:33 [14] message 2
00007 30:33 [15] message 3
00008 30:33 [16] message 2
00009 30:33 [17] message 3
00010 30:33 [18] message 3
00011 30:33 [19] message 4
00012 30:33 [20] message 3
00013 30:33 [21] message 3
00014 30:33 [22] message 4
00015 30:33 [23] message 4
00016 30:33 [24] message 4
00017 30:33 [25] message 4
00018 30:33 [26] message 5
00019 30:33 [27] message 5
00020 30:33 [28] message 6
00021 30:33 [29] message 6
00022 30:33 [30] message 5
00023 30:33 [31] message 5
00024 30:33 [32] message 5
00025 30:33 [33] message 6
00026 30:33 [34] message 7
00027 ...

00001 
00057 #include <boost/logging/format_fwd.hpp>
00058 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> )
00059 
00060 #include <boost/logging/format_ts.hpp>
00061 #include <boost/logging/format/formatter/thread_id.hpp>
00062 #include <boost/thread/thread.hpp>
00063 #include <boost/thread/xtime.hpp>
00064 
00065 using namespace boost::logging;
00066 
00067 typedef logger_format_write< default_, default_, writer::threading::on_dedicated_thread > logger_type;
00068 
00069 BOOST_DECLARE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00070 BOOST_DECLARE_LOG(g_l, logger_type) 
00071 
00072 #define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() ) 
00073 
00074 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00075 BOOST_DEFINE_LOG(g_l, logger_type)
00076 
00077 void do_sleep(int ms) {
00078     using namespace boost;
00079     xtime next;
00080     xtime_get( &next, TIME_UTC);
00081     next.nsec += (ms % 1000) * 1000000;
00082 
00083     int nano_per_sec = 1000000000;
00084     next.sec += next.nsec / nano_per_sec;
00085     next.sec += ms / 1000;
00086     next.nsec %= nano_per_sec;
00087     thread::sleep( next);
00088 }
00089 
00090 void use_log_thread() {
00091     for ( int i = 0; i < 20; ++i) {
00092         L_ << "message " << i ;
00093         do_sleep(1);
00094     }
00095 }
00096 
00097 void ts_logger_one_filter_example() {
00098     g_l()->writer().add_formatter( formatter::idx(), "[%] "  );
00099     g_l()->writer().add_formatter( formatter::time("$mm:$ss ") );
00100     g_l()->writer().add_formatter( formatter::append_newline() );
00101     g_l()->writer().add_destination( destination::file("out.txt") );
00102     g_l()->writer().add_destination( destination::dbg_window() );
00103     g_l()->mark_as_initialized();
00104 
00105     for ( int i = 0 ; i < 5; ++i)
00106         boost::thread t( &use_log_thread);
00107 
00108     // allow for all threads to finish
00109     std::cout << "sleep 5s " << std::endl;
00110     do_sleep( 5000);
00111 }
00112 
00113 
00114 
00115 
00116 int main() {
00117     ts_logger_one_filter_example();
00118 }
00119 
00120 
00121 // End of file
00122 


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