fastest_no_ostr_like.cpp

This usage:

Here's what the output will be:

The console:

00001 this is so cool
00002 hello, world
00003 good to be back ;)

The err.txt file:

00001 first error 
00002 second error 

00001 
00037 #define BOOST_LOG_COMPILE_FAST_OFF
00038 #include <boost/logging/logging.hpp>
00039 #include <boost/logging/format.hpp>
00040 
00041 using namespace boost::logging;
00042 
00043 struct no_gather {
00044     typedef const char* msg_type ;
00045     const char * m_msg;
00046     no_gather() : m_msg(0) {}
00047     const char * msg() const { return m_msg; }
00048     void *out(const char* msg) { m_msg = msg; return this; }
00049     void *out(const std::string& msg) { m_msg = msg.c_str(); return this; }
00050 };
00051 
00052 
00053 typedef logger< no_gather, destination::cout > app_log_type;
00054 typedef logger< no_gather, destination::file > err_log_type;
00055 
00056 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00057 
00058 BOOST_DEFINE_LOG(g_log_app, app_log_type)
00059 BOOST_DEFINE_LOG_WITH_ARGS( g_log_err, err_log_type, ("err.txt") )
00060 
00061 #define LAPP_ BOOST_LOG_USE_SIMPLE_LOG_IF_FILTER(g_log_app(), g_log_filter()->is_enabled() ) 
00062 #define LERR_ BOOST_LOG_USE_SIMPLE_LOG_IF_FILTER(g_log_err(), g_log_filter()->is_enabled() ) 
00063 
00064 void fastest_no_ostr_like_example() {
00065     g_log_app()->mark_as_initialized();
00066     g_log_err()->mark_as_initialized();
00067 
00068     LAPP_("this is so cool\n");
00069     LERR_("first error \n");
00070 
00071     std::string hello = "hello", world = "world";
00072     LAPP_(hello + ", " + world + "\n");
00073 
00074     g_log_filter()->set_enabled(false);
00075     LAPP_("this will not be written to the log");
00076     LAPP_("this won't be written to the log");
00077     LERR_("this error is not logged ");
00078 
00079     g_log_filter()->set_enabled(true);
00080     LAPP_("good to be back ;) \n" );
00081     LERR_("second error \n" );
00082 }
00083 
00084 
00085 
00086 
00087 int main() {
00088     fastest_no_ostr_like_example();
00089 }
00090 
00091 
00092 // End of file
00093 


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