mul_loggers_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:

Logs:

Here's how the output will look like:

The debug output window:

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

The console:

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

The out.txt file:

00001 18:59.24 [app] hello, world
00002 18:59.24 [app] 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 
00076 #include <boost/logging/format_fwd.hpp>
00077 
00078 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> )
00079 
00080 #include <boost/logging/format.hpp>
00081 using namespace boost::logging;
00082 
00083 typedef logger_format_write< > logger_type;
00084 
00085 BOOST_DECLARE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00086 BOOST_DECLARE_LOG(g_log_err, logger_type) 
00087 BOOST_DECLARE_LOG(g_log_app, logger_type)
00088 BOOST_DECLARE_LOG(g_log_dbg, logger_type)
00089 
00090 #define LDBG_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_dbg(), g_log_filter()->is_enabled() ) << "[dbg] "
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() ) << "[app] "
00093 
00094 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00095 BOOST_DEFINE_LOG(g_log_err, logger_type)
00096 BOOST_DEFINE_LOG(g_log_app, logger_type)
00097 BOOST_DEFINE_LOG(g_log_dbg, logger_type)
00098 
00099 void mul_logger_one_filter_example() {
00100     // Err log
00101     g_log_err()->writer().add_formatter( formatter::idx(), "[%] "  );
00102     g_log_err()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
00103     g_log_err()->writer().add_formatter( formatter::append_newline() );
00104     g_log_err()->writer().add_destination( destination::file("err.txt") );
00105 
00106     // App log
00107     g_log_app()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
00108     g_log_app()->writer().add_formatter( formatter::append_newline() );
00109     g_log_app()->writer().add_destination( destination::file("out.txt") );
00110     g_log_app()->writer().add_destination( destination::cout() );
00111 
00112     // Debug log
00113     g_log_dbg()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
00114     g_log_dbg()->writer().add_formatter( formatter::append_newline() );
00115     g_log_dbg()->writer().add_destination( destination::dbg_window() );
00116     g_log_dbg()->writer().add_destination( destination::cout() );
00117 
00118     g_log_app()->mark_as_initialized();
00119     g_log_err()->mark_as_initialized();
00120     g_log_dbg()->mark_as_initialized();
00121 
00122     int i = 1;
00123     LDBG_ << "this is so cool " << i++;
00124     LDBG_ << "this is so cool again " << i++;
00125     LERR_ << "first error " << i++;
00126 
00127     std::string hello = "hello", world = "world";
00128     LAPP_ << hello << ", " << world;
00129 
00130     g_log_filter()->set_enabled(false);
00131     LDBG_ << "this will not be written anywhere";
00132     LAPP_ << "this won't be written anywhere either";
00133     LERR_ << "this error is not logged " << i++;
00134 
00135     g_log_filter()->set_enabled(true);
00136     LAPP_ << "good to be back ;) " << i++;
00137     LERR_ << "second error " << i++;
00138 }
00139 
00140 
00141 
00142 
00143 int main() {
00144     mul_logger_one_filter_example();
00145 }
00146 
00147 
00148 // End of file
00149 


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