mul_levels_mul_logers.cpp

This usage:

Optimizations:

Logs:

Here's how the output will look like:

The debug output window:

00001 07:52.30 [dbg] this is so cool 1
00002 07:52.30 [dbg] this is so cool again 2

The console:

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

The out.txt file:

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

The err.txt file

00001 07:52.30 [1] first error 3
00002 07:52.30 [2] second error 5

00001 
00062 #include <boost/logging/format/named_write.hpp>
00063 typedef boost::logging::named_logger<>::type logger_type;
00064 
00065 #define LDBG_ BOOST_LOG_USE_LOG_IF_LEVEL(g_log_dbg(), g_log_level(), debug ) << "[dbg] "
00066 #define LERR_ BOOST_LOG_USE_LOG_IF_LEVEL(g_log_err(), g_log_level(), error )
00067 #define LAPP_ BOOST_LOG_USE_LOG_IF_LEVEL(g_log_app(), g_log_level(), info ) << "[app] "
00068 
00069 BOOST_DEFINE_LOG_FILTER(g_log_level, boost::logging::level::holder ) 
00070 BOOST_DEFINE_LOG(g_log_err, logger_type)
00071 BOOST_DEFINE_LOG(g_log_app, logger_type)
00072 BOOST_DEFINE_LOG(g_log_dbg, logger_type)
00073 
00074 using namespace boost::logging;
00075 
00076 void mul_levels_mul_logers_example() {
00077     // reuse the same destination for 2 logs
00078     destination::file out("out.txt");
00079     g_log_app()->writer().replace_destination("file", out);
00080     g_log_dbg()->writer().replace_destination("file", out);
00081     // formatting (first param) and destinations (second param)
00082     g_log_err()->writer().write("[%idx%] %time%($hh:$mm.$ss) |\n", "cout file(err.txt)"); // line A
00083     g_log_app()->writer().write("%time%($hh:$mm.$ss) |\n", "file cout");
00084     g_log_dbg()->writer().write("%time%($hh:$mm.$ss) |\n", "file cout debug");
00085 
00086     /* 
00087     Note : the "line A" above originally was:
00088     g_log_err()->writer().write("[%idx%] %time%($hh:$mm.$ss) |\n", "file(err.txt)");
00089 
00090     This caused a very strange assertion failure on Fedora8, when the program exits, while destroying the global variables.
00091     I've spent some time debugging it but to no avail. I will certainly look more into this.
00092     */
00093 
00094     g_log_app()->mark_as_initialized();
00095     g_log_err()->mark_as_initialized();
00096     g_log_dbg()->mark_as_initialized();
00097 
00098 
00099     int i = 1;
00100     LDBG_ << "this is so cool " << i++;
00101     LDBG_ << "this is so cool again " << i++;
00102     LERR_ << "first error " << i++;
00103 
00104     std::string hello = "hello", world = "world";
00105     LAPP_ << hello << ", " << world;
00106 
00107     g_log_level()->set_enabled(level::error);
00108     LDBG_ << "this will not be written anywhere";
00109     LAPP_ << "this won't be written anywhere either";
00110 
00111     g_log_level()->set_enabled(level::info);
00112     LAPP_ << "good to be back ;) " << i++;
00113     LERR_ << "second error " << i++;
00114 }
00115 
00116 
00117 
00118 
00119 int main() {
00120     mul_levels_mul_logers_example();
00121 }
00122 
00123 
00124 // End of file
00125 


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