your_scenario.cpp

This usage:

Optimizations:

Logs:

Here's how the output will look like:

The debug output window:

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

The console:

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

The out.txt file:

00001 18:59.24 hello, world
00002 18:59.24 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 
00066 #include <boost/logging/format_fwd.hpp>
00067 
00068 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> )
00069 
00070 #include <boost/logging/format_ts.hpp>
00071 #include <boost/thread/xtime.hpp>
00072 using namespace boost::logging;
00073 
00074 using namespace boost::logging::scenario::usage;
00075 typedef use<
00076         //  the filter is always accurate (but slow)
00077         filter_::change::always_accurate, 
00078         //  filter does not use levels
00079         filter_::level::no_levels, 
00080         // the logger is initialized once, when only one thread is running
00081         logger_::change::set_once_when_one_thread, 
00082         // the logger favors speed (on a dedicated thread)
00083         logger_::favor::speed> finder;
00084 
00085 BOOST_DECLARE_LOG_FILTER(g_log_filter, finder::filter ) 
00086 BOOST_DECLARE_LOG(g_log_err, finder::logger ) 
00087 BOOST_DECLARE_LOG(g_log_app, finder::logger )
00088 BOOST_DECLARE_LOG(g_log_dbg, finder::logger )
00089 
00090 #define LDBG_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_dbg(), g_log_filter()->is_enabled() ) 
00091 #define LERR_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_err(), g_log_filter()->is_enabled() )
00092 #define LAPP_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_app(), g_log_filter()->is_enabled() ) 
00093 
00094 BOOST_DEFINE_LOG_FILTER(g_log_filter, finder::filter ) 
00095 BOOST_DEFINE_LOG(g_log_err, finder::logger )
00096 BOOST_DEFINE_LOG(g_log_app, finder::logger )
00097 BOOST_DEFINE_LOG(g_log_dbg, finder::logger )
00098 
00099 void do_sleep(int ms) {
00100     using namespace boost;
00101     xtime next;
00102     xtime_get( &next, TIME_UTC);
00103     next.nsec += (ms % 1000) * 1000000;
00104 
00105     int nano_per_sec = 1000000000;
00106     next.sec += next.nsec / nano_per_sec;
00107     next.sec += ms / 1000;
00108     next.nsec %= nano_per_sec;
00109     thread::sleep( next);
00110 }
00111 
00112 void your_scenario_example() {
00113     //         add formatters and destinations
00114     //         That is, how the message is to be formatted and where should it be written to
00115 
00116     // Err log
00117     g_log_err()->writer().add_formatter( formatter::idx(), "[%] "  );
00118     g_log_err()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
00119     g_log_err()->writer().add_formatter( formatter::append_newline() );
00120     g_log_err()->writer().add_destination( destination::file("err.txt") );
00121 
00122     // App log
00123     g_log_app()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
00124     g_log_app()->writer().add_formatter( formatter::append_newline() );
00125     g_log_app()->writer().add_destination( destination::file("out.txt") );
00126     g_log_app()->writer().add_destination( destination::cout() );
00127 
00128     // Debug log
00129     g_log_dbg()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
00130     g_log_dbg()->writer().add_formatter( formatter::append_newline() );
00131     g_log_dbg()->writer().add_destination( destination::dbg_window() );
00132     g_log_dbg()->writer().add_destination( destination::cout() );
00133     g_log_app()->mark_as_initialized();
00134     g_log_err()->mark_as_initialized();
00135     g_log_dbg()->mark_as_initialized();
00136 
00137     int i = 1;
00138     LDBG_ << "this is so cool " << i++;
00139     LDBG_ << "this is so cool again " << i++;
00140     LERR_ << "first error " << i++;
00141 
00142     std::string hello = "hello", world = "world";
00143     LAPP_ << hello << ", " << world;
00144 
00145     g_log_filter()->set_enabled(false);
00146     LDBG_ << "this will not be written anywhere";
00147     LAPP_ << "this won't be written anywhere either";
00148     LERR_ << "this error is not logged " << i++;
00149 
00150     g_log_filter()->set_enabled(true);
00151     LAPP_ << "good to be back ;) " << i++;
00152     LERR_ << "second error " << i++;
00153 
00154     // just so that we can see the output to the console as well (the messages are written no a different thread)...
00155     do_sleep(1000);
00156 }
00157 
00158 
00159 
00160 
00161 int main() {
00162     your_scenario_example();
00163 }
00164 
00165 
00166 // End of file
00167 


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