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:
A custom route means you don't want to first run all formatters, and then write to all destinations. Depending on the destination, you'll want a certain formatting of the message
In our example:
00001 to cout: [idx] [time] message [enter] 00002 to dbg_window: [time] message [enter] 00003 to file: [idx] message [enter]
We will use an apply_format_and_write
class that caches the formatting, so that it'll format faster (more specifically, the boost::logging::format_and_write::use_cache, together with boost::logging::optimize::cache_string_several_str).
The output will be similar to this:
The debug window
The file:
The console:
00001 [1] 12:15.12 this is so cool 1 00002 [2] 12:15.12 hello, world 00003 [3] 12:15.12 good to be back ;) 2
00001 00069 #include <boost/logging/format_fwd.hpp> 00070 00071 BOOST_LOG_FORMAT_MSG( optimize::cache_string_several_str<> ) 00072 00073 #include <boost/logging/format.hpp> 00074 00075 using namespace boost::logging; 00076 00077 00078 typedef logger_format_write< > logger_type; 00079 00080 BOOST_DECLARE_LOG_FILTER(g_log_filter, filter::no_ts ) 00081 BOOST_DECLARE_LOG(g_l, logger_type) 00082 00083 #define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() ) 00084 00085 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts ) 00086 BOOST_DEFINE_LOG(g_l, logger_type) 00087 00088 void no_levels_with_route_example() { 00089 // add formatters and destinations 00090 // That is, how the message is to be formatted... 00091 g_l()->writer().add_formatter( formatter::idx(), "[%] " ); 00092 g_l()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") ); 00093 g_l()->writer().add_formatter( formatter::append_newline() ); 00094 00095 // ... and where should it be written to 00096 g_l()->writer().add_destination( destination::cout() ); 00097 g_l()->writer().add_destination( destination::dbg_window() ); 00098 g_l()->writer().add_destination( destination::file("out.txt") ); 00099 00100 // Now, specify the route 00101 g_l()->writer().router().set_route() 00102 .fmt( formatter::time("$hh:$mm.$ss ") ) 00103 .fmt( formatter::append_newline() ) 00104 /* 00105 Not like this: .fmt( formatter::idx() ) 00106 00107 This is because 00108 add_formatter( formatter::idx(), "[%] " ); 00109 has surrounded formatter::idx() in a spacer - see formatter::spacer 00110 */ 00111 .fmt( formatter::spacer( formatter::idx(), "[%] ") ) 00112 .clear() 00113 .fmt( formatter::time("$hh:$mm.$ss ") ) 00114 .fmt( formatter::append_newline() ) 00115 .dest( destination::dbg_window() ) 00116 .clear() 00117 .fmt( formatter::spacer( formatter::idx(), "[%] ") ) 00118 .fmt( formatter::time("$hh:$mm.$ss ") ) 00119 .fmt( formatter::append_newline() ) 00120 .dest( destination::cout() ) 00121 .clear() 00122 .fmt( formatter::spacer( formatter::idx(), "[%] ") ) 00123 .fmt( formatter::append_newline() ) 00124 .dest( destination::file("out.txt") ); 00125 00126 g_l()->mark_as_initialized(); 00127 00128 int i = 1; 00129 L_ << "this is so cool " << i++; 00130 00131 std::string hello = "hello", world = "world"; 00132 L_ << hello << ", " << world; 00133 00134 g_log_filter()->set_enabled(false); 00135 L_ << "this will not be written anywhere"; 00136 L_ << "this won't be written anywhere either"; 00137 00138 g_log_filter()->set_enabled(true); 00139 L_ << "good to be back ;) " << i++; 00140 } 00141 00142 00143 00144 00145 int main() { 00146 no_levels_with_route_example(); 00147 } 00148 00149 00150 // End of file 00151