using_tags.cpp

This usage:

Optimizations:

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

00001 logging\samples\scenarios\using_tags.cpp:94 [T7204] [1] 14:55 this is so cool 1
00002 logging\samples\scenarios\using_tags.cpp:95 [T7204] [2] 14:55 this is so cool again 2

00001 
00031 #include <boost/logging/format_fwd.hpp>
00032 
00033 namespace bl = boost::logging;
00034 typedef bl::tag::holder< bl::optimize::cache_string_one_str<>, bl::tag::file_line, bl::tag::thread_id, bl::tag::time> log_string;
00035 BOOST_LOG_FORMAT_MSG( log_string )
00036 
00037 
00038 #include <boost/logging/format_ts.hpp>
00039 #include <boost/logging/format/formatter/tags.hpp>
00040 #include <boost/logging/format/formatter/named_spacer.hpp>
00041 
00042 using namespace boost::logging;
00043 
00044 using namespace boost::logging::scenario::usage;
00045 typedef use<
00046         //  the filter is always accurate (but slow)
00047         filter_::change::always_accurate, 
00048         //  filter does not use levels
00049         filter_::level::no_levels, 
00050         // the logger is initialized once, when only one thread is running
00051         logger_::change::set_once_when_one_thread, 
00052         // the logger favors speed (on a dedicated thread)
00053         logger_::favor::speed> finder;
00054 
00055 BOOST_DECLARE_LOG_FILTER(g_log_filter, finder::filter ) 
00056 BOOST_DECLARE_LOG(g_l, finder::logger) 
00057 
00058 #define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() ) .set_tag( BOOST_LOG_TAG_FILELINE)
00059 
00060 BOOST_DEFINE_LOG_FILTER(g_log_filter, finder::filter ) 
00061 BOOST_DEFINE_LOG(g_l, finder::logger) 
00062 
00063 
00064 void using_tags_example() {
00065     //         add formatters and destinations
00066     //         That is, how the message is to be formatted and where should it be written to
00067 
00068     g_l()->writer().add_formatter( formatter::named_spacer( "%fileline% [T%thread_id%] [%idx%] %time%" )
00069         .add( "time", formatter::tag::time("$mm:$ss ") )                // time tag
00070         .add( "idx", formatter::idx() )                            
00071         .add( "thread_id", formatter::tag::thread_id() )                // thread_id tag
00072         .add( "fileline", formatter::tag::file_line() ) );              // file/line tag
00073 
00074     g_l()->writer().add_formatter( formatter::append_newline() );     
00075     g_l()->writer().add_destination( destination::cout() );
00076     g_l()->writer().add_destination( destination::file("out.txt") );
00077     g_l()->mark_as_initialized();
00078 
00079     int i = 1;
00080     L_ << "this is so cool " << i++;
00081     L_ << "this is so cool again " << i++;
00082 }
00083 
00084 
00085 
00086 
00087 int main() {
00088     using_tags_example();
00089 }
00090 
00091 
00092 // End of file
00093 


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