using_tags.cpp

Boost Logging library

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


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