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

Optimizations:

In this example, all output will be written to the console, debug window, and "out.txt" file. It will be:

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

00001 
00049 #include <boost/logging/format_fwd.hpp>
00050 
00051 #include <boost/logging/format/named_write.hpp>
00052 typedef boost::logging::named_logger<>::type logger_type;
00053 
00054 #define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() ) 
00055 
00056 // Define the filters and loggers you'll use (usually in a source file)
00057 BOOST_DEFINE_LOG_FILTER(g_log_filter, boost::logging::filter::no_ts ) 
00058 BOOST_DEFINE_LOG(g_l, logger_type)
00059 
00060 
00061 void one_logger_one_filter_example() {
00062     // formatting    : [idx] message \n
00063     // destinations  : console, file "out.txt" and debug window
00064     g_l()->writer().write("[%idx%] |\n", "cout file(out.txt) debug");
00065     g_l()->mark_as_initialized();
00066 
00067     int i = 1;
00068     L_ << "this is so cool " << i++;
00069     L_ << "this is so cool again " << i++;
00070 
00071     std::string hello = "hello", world = "world";
00072     L_ << hello << ", " << world;
00073 
00074     g_log_filter()->set_enabled(false);
00075     L_ << "this will not be written to the log";
00076     L_ << "this won't be written to the log";
00077 
00078     g_log_filter()->set_enabled(true);
00079     L_ << "good to be back ;) " << i++;
00080 }
00081 
00082 
00083 
00084 
00085 int main() {
00086     one_logger_one_filter_example();
00087 }
00088 
00089 
00090 // End of file
00091 


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