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


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