This usage:
Custom classes:
Optimizations:
The output will look similar to this one:
The console and the debug window will be the same:
00001 +6s [1] this is so cool 1 00002 +6s [2] this is so cool again 2 00003 +7s [3] hello, world 00004 +7s [4] good to be back ;) 3
The out.txt file will look like this:
00001 <msg>+6s [1] this is so cool 1 00002 </msg> 00003 <msg>+6s [2] this is so cool again 2 00004 </msg> 00005 <msg>+7s [3] hello, world 00006 </msg> 00007 <msg>+7s [4] good to be back ;) 3 00008 </msg>
00001 00060 #include <boost/logging/format_fwd.hpp> 00061 00062 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> ) 00063 00064 #include <boost/logging/format.hpp> 00065 using namespace boost::logging; 00066 00067 typedef logger_format_write< default_, default_, writer::threading::no_ts > logger_type; 00068 00069 00070 BOOST_DECLARE_LOG_FILTER(g_log_filter, filter::no_ts ) 00071 BOOST_DECLARE_LOG(g_l, logger_type) 00072 00073 #define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() ) 00074 00075 BOOST_DEFINE_LOG(g_l, logger_type) 00076 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts ) 00077 00078 00079 00080 // Example of custom formatter: 00081 // dump the no. of seconds since start of program 00082 struct secs_since_start : formatter::class_<secs_since_start, formatter::implement_op_equal::no_context> { 00083 ::time_t m_start; 00084 secs_since_start() : m_start( ::time(0) ) {} 00085 void operator()(param str) const { 00086 ::time_t now = ::time(0); 00087 std::stringstream out; 00088 out << "+" << (int)(now-m_start) << "s "; 00089 str.prepend_string( out.str() ); 00090 } 00091 }; 00092 00093 // Example of custom destination: 00094 // Dump each message as XML 00095 struct as_xml : 00096 destination::class_<as_xml, destination::implement_op_equal::has_context>, 00097 destination::non_const_context<std::ofstream> { 00098 00099 std::string m_name; 00100 as_xml(const char* name) : non_const_context_base(name), m_name(name) {} 00101 void operator()(param str) const { 00102 context() << "<msg>" << str << "</msg>" << std::endl; 00103 } 00104 00105 bool operator==(const as_xml& other) const { return m_name == other.m_name; } 00106 }; 00107 00108 00109 void custom_fmt_dest_example() { 00110 // add formatters and destinations 00111 // That is, how the message is to be formatted and where should it be written to 00112 g_l()->writer().add_formatter( formatter::idx(), "[%] " ); 00113 g_l()->writer().add_formatter( formatter::append_newline() ); 00114 g_l()->writer().add_formatter( secs_since_start() ); 00115 00116 g_l()->writer().add_destination( destination::cout() ); 00117 g_l()->writer().add_destination( destination::dbg_window() ); 00118 g_l()->writer().add_destination( as_xml("out.txt") ); 00119 g_l()->mark_as_initialized(); 00120 00121 int i = 1; 00122 L_ << "this is so cool " << i++; 00123 L_ << "this is so cool again " << i++; 00124 00125 std::string hello = "hello", world = "world"; 00126 L_ << hello << ", " << world; 00127 00128 g_log_filter()->set_enabled(false); 00129 L_ << "this will not be written to the log"; 00130 L_ << "this won't be written to the log"; 00131 00132 g_log_filter()->set_enabled(true); 00133 L_ << "good to be back ;) " << i++; 00134 } 00135 00136 00137 00138 int main() { 00139 custom_fmt_dest_example(); 00140 } 00141 00142 00143 // End of file