no_levels_with_route.cpp

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

00001 12:15.12 this is so cool 1
00002 12:15.12 hello, world
00003 12:15.12 good to be back ;) 2

The file:

00001 [1] this is so cool 1
00002 [2] hello, world
00003 [3] good to be back ;) 2

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 
00054 #include <boost/logging/format_fwd.hpp>
00055 
00056 BOOST_LOG_FORMAT_MSG( optimize::cache_string_several_str<> )
00057 
00058 #include <boost/logging/format.hpp>
00059 
00060 using namespace boost::logging;
00061 
00062 
00063 typedef logger_format_write< > logger_type;
00064 
00065 BOOST_DECLARE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00066 BOOST_DECLARE_LOG(g_l, logger_type) 
00067 
00068 #define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() )
00069 
00070 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00071 BOOST_DEFINE_LOG(g_l, logger_type)
00072 
00073 void no_levels_with_route_example() {
00074     //         add formatters and destinations
00075     //         That is, how the message is to be formatted...
00076     g_l()->writer().add_formatter( formatter::idx(), "[%] "  );
00077     g_l()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
00078     g_l()->writer().add_formatter( formatter::append_newline() );
00079 
00080     //        ... and where should it be written to
00081     g_l()->writer().add_destination( destination::cout() );
00082     g_l()->writer().add_destination( destination::dbg_window() );
00083     g_l()->writer().add_destination( destination::file("out.txt") );
00084 
00085     // Now, specify the route
00086     g_l()->writer().router().set_route()
00087         .fmt( formatter::time("$hh:$mm.$ss ") ) 
00088         .fmt( formatter::append_newline() )
00089         /* 
00090         Not like this: .fmt( formatter::idx() )
00091 
00092         This is because 
00093         add_formatter( formatter::idx(), "[%] "  );
00094         has surrounded formatter::idx() in a spacer - see formatter::spacer
00095         */
00096         .fmt( formatter::spacer( formatter::idx(), "[%] ") )
00097         .clear()
00098         .fmt( formatter::time("$hh:$mm.$ss ") ) 
00099         .fmt( formatter::append_newline() )
00100         .dest( destination::dbg_window() )
00101         .clear()
00102         .fmt( formatter::spacer( formatter::idx(), "[%] ") )
00103         .fmt( formatter::time("$hh:$mm.$ss ") ) 
00104         .fmt( formatter::append_newline() )
00105         .dest( destination::cout() )
00106         .clear()
00107         .fmt( formatter::spacer( formatter::idx(), "[%] ") )
00108         .fmt( formatter::append_newline() )
00109         .dest( destination::file("out.txt") );
00110 
00111     g_l()->mark_as_initialized();
00112 
00113     int i = 1;
00114     L_ << "this is so cool " << i++;
00115 
00116     std::string hello = "hello", world = "world";
00117     L_ << hello << ", " << world;
00118 
00119     g_log_filter()->set_enabled(false);
00120     L_ << "this will not be written anywhere";
00121     L_ << "this won't be written anywhere either";
00122 
00123     g_log_filter()->set_enabled(true);
00124     L_ << "good to be back ;) " << i++;
00125 }
00126 
00127 
00128 
00129 
00130 int main() {
00131     no_levels_with_route_example();
00132 }
00133 
00134 
00135 // End of file
00136 


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