mul_loggers_one_filter.cpp

This usage:

Optimizations:

Logs:

Here's how the output will look like:

The debug output window:

00001 18:59.24 [dbg] this is so cool 1
00002 18:59.24 [dbg] this is so cool again 2

The console:

00001 18:59.24 [dbg] this is so cool 1
00002 18:59.24 [dbg] this is so cool again 2
00003 18:59.24 [app] hello, world
00004 18:59.24 [app] good to be back ;) 4

The out.txt file:

00001 18:59.24 [app] hello, world
00002 18:59.24 [app] good to be back ;) 4

The err.txt file

00001 18:59.24 [1] first error 3
00002 18:59.24 [2] second error 5

00001 
00061 #include <boost/logging/format_fwd.hpp>
00062 
00063 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> )
00064 
00065 #include <boost/logging/format.hpp>
00066 using namespace boost::logging;
00067 
00068 typedef logger_format_write< > logger_type;
00069 
00070 BOOST_DECLARE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00071 BOOST_DECLARE_LOG(g_log_err, logger_type) 
00072 BOOST_DECLARE_LOG(g_log_app, logger_type)
00073 BOOST_DECLARE_LOG(g_log_dbg, logger_type)
00074 
00075 #define LDBG_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_dbg(), g_log_filter()->is_enabled() ) << "[dbg] "
00076 #define LERR_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_err(), g_log_filter()->is_enabled() )
00077 #define LAPP_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_app(), g_log_filter()->is_enabled() ) << "[app] "
00078 
00079 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00080 BOOST_DEFINE_LOG(g_log_err, logger_type)
00081 BOOST_DEFINE_LOG(g_log_app, logger_type)
00082 BOOST_DEFINE_LOG(g_log_dbg, logger_type)
00083 
00084 void mul_logger_one_filter_example() {
00085     // Err log
00086     g_log_err()->writer().add_formatter( formatter::idx(), "[%] "  );
00087     g_log_err()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
00088     g_log_err()->writer().add_formatter( formatter::append_newline() );
00089     g_log_err()->writer().add_destination( destination::file("err.txt") );
00090 
00091     // App log
00092     g_log_app()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
00093     g_log_app()->writer().add_formatter( formatter::append_newline() );
00094     g_log_app()->writer().add_destination( destination::file("out.txt") );
00095     g_log_app()->writer().add_destination( destination::cout() );
00096 
00097     // Debug log
00098     g_log_dbg()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
00099     g_log_dbg()->writer().add_formatter( formatter::append_newline() );
00100     g_log_dbg()->writer().add_destination( destination::dbg_window() );
00101     g_log_dbg()->writer().add_destination( destination::cout() );
00102 
00103     g_log_app()->mark_as_initialized();
00104     g_log_err()->mark_as_initialized();
00105     g_log_dbg()->mark_as_initialized();
00106 
00107     int i = 1;
00108     LDBG_ << "this is so cool " << i++;
00109     LDBG_ << "this is so cool again " << i++;
00110     LERR_ << "first error " << i++;
00111 
00112     std::string hello = "hello", world = "world";
00113     LAPP_ << hello << ", " << world;
00114 
00115     g_log_filter()->set_enabled(false);
00116     LDBG_ << "this will not be written anywhere";
00117     LAPP_ << "this won't be written anywhere either";
00118     LERR_ << "this error is not logged " << i++;
00119 
00120     g_log_filter()->set_enabled(true);
00121     LAPP_ << "good to be back ;) " << i++;
00122     LERR_ << "second error " << i++;
00123 }
00124 
00125 
00126 
00127 
00128 int main() {
00129     mul_logger_one_filter_example();
00130 }
00131 
00132 
00133 // End of file
00134 


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