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:
In this example, all output will be written to the console, debug window, and "out.txt" file. It will look similar to:
00001 [T5884] [1] message 0 00002 [T7168] [2] message 0 00003 [T7932] [3] message 0 00004 [T740] [4] message 0 00005 [T8124] [5] message 0 00006 [T5884] [6] message 1 00007 [T5884] [7] message 2 00008 [T740] [8] message 1 00009 [T7168] [9] message 1 00010 [T7932] [10] message 1 00011 [T8124] [11] message 1 00012 [T5884] [12] message 3 00013 [T7168] [13] message 2 00014 [T5884] [14] message 4 00015 [T740] [15] message 2 00016 [T7932] [16] message 2 00017 [T8124] [17] message 2 00018 [T7168] [18] message 3 00019 [T5884] [19] message 5 00020 ...
00001 00065 #include <boost/logging/format_fwd.hpp> 00066 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> ) 00067 00068 #include <boost/logging/format_ts.hpp> 00069 #include <boost/logging/format/formatter/thread_id.hpp> 00070 #include <boost/thread/thread.hpp> 00071 #include <boost/thread/xtime.hpp> 00072 00073 using namespace boost::logging; 00074 00075 typedef logger_format_write< default_, default_, writer::threading::ts_write > logger_type; 00076 00077 #define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() ) 00078 00079 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts ) 00080 BOOST_DEFINE_LOG(g_l, logger_type) 00081 00082 void do_sleep(int ms) { 00083 using namespace boost; 00084 xtime next; 00085 xtime_get( &next, TIME_UTC); 00086 next.nsec += (ms % 1000) * 1000000; 00087 00088 int nano_per_sec = 1000000000; 00089 next.sec += next.nsec / nano_per_sec; 00090 next.sec += ms / 1000; 00091 next.nsec %= nano_per_sec; 00092 thread::sleep( next); 00093 } 00094 00095 void use_log_thread() { 00096 for ( int i = 0; i < 50; ++i) { 00097 L_ << "message " << i ; 00098 do_sleep(1); 00099 } 00100 } 00101 00102 void ts_logger_one_filter_example() { 00103 // add formatters and destinations 00104 // That is, how the message is to be formatted and where should it be written to 00105 g_l()->writer().add_formatter( formatter::idx(), "[%] " ); 00106 g_l()->writer().add_formatter( formatter::thread_id(), "[T%] " ); 00107 g_l()->writer().add_formatter( formatter::append_newline() ); 00108 g_l()->writer().add_destination( destination::file("out.txt") ); 00109 g_l()->writer().add_destination( destination::cout() ); 00110 g_l()->writer().add_destination( destination::dbg_window() ); 00111 g_l()->mark_as_initialized(); 00112 00113 for ( int i = 0 ; i < 5; ++i) 00114 boost::thread t( &use_log_thread); 00115 00116 // allow for all threads to finish 00117 do_sleep( 5000); 00118 } 00119 00120 00121 00122 00123 int main() { 00124 ts_logger_one_filter_example(); 00125 } 00126 00127 00128 // End of file 00129