fastest_use_ostr_like.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:

Here's what the output will be:

The console:

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

The err.txt file:

00001 first error 3
00002 second error 5

00001 
00053 #define BOOST_LOG_COMPILE_FAST_OFF
00054 #include <boost/logging/logging.hpp>
00055 #include <boost/logging/format.hpp>
00056 
00057 using namespace boost::logging;
00058 
00059 typedef logger< default_, destination::cout> app_log_type;
00060 typedef logger< default_, destination::file> err_log_type;
00061 
00062 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts )
00063 
00064 BOOST_DEFINE_LOG(g_log_app, app_log_type )
00065 BOOST_DEFINE_LOG_WITH_ARGS( g_log_err, err_log_type, ("err.txt") )
00066 
00067 #define LAPP_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_app(), g_log_filter()->is_enabled() ) 
00068 #define LERR_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_err(), g_log_filter()->is_enabled() ) 
00069 
00070 void fastest_use_ostr_like_example() {
00071     g_log_app()->mark_as_initialized();
00072     g_log_err()->mark_as_initialized();
00073 
00074     int i = 1;
00075     LAPP_ << "this is so cool " << i++ << "\n";
00076     LAPP_ << "this is so cool again " << i++ << "\n";
00077     LERR_ << "first error " << i++ << "\n";
00078 
00079     std::string hello = "hello", world = "world";
00080     LAPP_ << hello << ", " << world << "\n";
00081 
00082     g_log_filter()->set_enabled(false);
00083     LAPP_ << "this will not be written to the log";
00084     LAPP_ << "this won't be written to the log";
00085     LERR_ << "this error is not logged " << i++;
00086 
00087     g_log_filter()->set_enabled(true);
00088     LAPP_ << "good to be back ;) " << i++ << "\n";
00089     LERR_ << "second error " << i++ << "\n";
00090 }
00091 
00092 
00093 
00094 
00095 int main() {
00096     fastest_use_ostr_like_example();
00097 }
00098 
00099 
00100 // End of file
00101 


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