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:
The console:
00001 18:59.24 this is so cool 1 00002 18:59.24 this is so cool again 2 00003 18:59.24 hello, world 00004 18:59.24 good to be back ;) 4
The out.txt file:
The err.txt file
00001 00081 #include <boost/logging/format_fwd.hpp> 00082 00083 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> ) 00084 00085 #include <boost/logging/format_ts.hpp> 00086 #include <boost/thread/xtime.hpp> 00087 using namespace boost::logging; 00088 00089 using namespace boost::logging::scenario::usage; 00090 typedef use< 00091 // the filter is always accurate (but slow) 00092 filter_::change::always_accurate, 00093 // filter does not use levels 00094 filter_::level::no_levels, 00095 // the logger is initialized once, when only one thread is running 00096 logger_::change::set_once_when_one_thread, 00097 // the logger favors speed (on a dedicated thread) 00098 logger_::favor::speed> finder; 00099 00100 BOOST_DECLARE_LOG_FILTER(g_log_filter, finder::filter ) 00101 BOOST_DECLARE_LOG(g_log_err, finder::logger ) 00102 BOOST_DECLARE_LOG(g_log_app, finder::logger ) 00103 BOOST_DECLARE_LOG(g_log_dbg, finder::logger ) 00104 00105 #define LDBG_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_dbg(), g_log_filter()->is_enabled() ) 00106 #define LERR_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_err(), g_log_filter()->is_enabled() ) 00107 #define LAPP_ BOOST_LOG_USE_LOG_IF_FILTER(g_log_app(), g_log_filter()->is_enabled() ) 00108 00109 BOOST_DEFINE_LOG_FILTER(g_log_filter, finder::filter ) 00110 BOOST_DEFINE_LOG(g_log_err, finder::logger ) 00111 BOOST_DEFINE_LOG(g_log_app, finder::logger ) 00112 BOOST_DEFINE_LOG(g_log_dbg, finder::logger ) 00113 00114 void do_sleep(int ms) { 00115 using namespace boost; 00116 xtime next; 00117 xtime_get( &next, TIME_UTC); 00118 next.nsec += (ms % 1000) * 1000000; 00119 00120 int nano_per_sec = 1000000000; 00121 next.sec += next.nsec / nano_per_sec; 00122 next.sec += ms / 1000; 00123 next.nsec %= nano_per_sec; 00124 thread::sleep( next); 00125 } 00126 00127 void your_scenario_example() { 00128 // add formatters and destinations 00129 // That is, how the message is to be formatted and where should it be written to 00130 00131 // Err log 00132 g_log_err()->writer().add_formatter( formatter::idx(), "[%] " ); 00133 g_log_err()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") ); 00134 g_log_err()->writer().add_formatter( formatter::append_newline() ); 00135 g_log_err()->writer().add_destination( destination::file("err.txt") ); 00136 00137 // App log 00138 g_log_app()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") ); 00139 g_log_app()->writer().add_formatter( formatter::append_newline() ); 00140 g_log_app()->writer().add_destination( destination::file("out.txt") ); 00141 g_log_app()->writer().add_destination( destination::cout() ); 00142 00143 // Debug log 00144 g_log_dbg()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") ); 00145 g_log_dbg()->writer().add_formatter( formatter::append_newline() ); 00146 g_log_dbg()->writer().add_destination( destination::dbg_window() ); 00147 g_log_dbg()->writer().add_destination( destination::cout() ); 00148 g_log_app()->mark_as_initialized(); 00149 g_log_err()->mark_as_initialized(); 00150 g_log_dbg()->mark_as_initialized(); 00151 00152 int i = 1; 00153 LDBG_ << "this is so cool " << i++; 00154 LDBG_ << "this is so cool again " << i++; 00155 LERR_ << "first error " << i++; 00156 00157 std::string hello = "hello", world = "world"; 00158 LAPP_ << hello << ", " << world; 00159 00160 g_log_filter()->set_enabled(false); 00161 LDBG_ << "this will not be written anywhere"; 00162 LAPP_ << "this won't be written anywhere either"; 00163 LERR_ << "this error is not logged " << i++; 00164 00165 g_log_filter()->set_enabled(true); 00166 LAPP_ << "good to be back ;) " << i++; 00167 LERR_ << "second error " << i++; 00168 00169 // just so that we can see the output to the console as well (the messages are written no a different thread)... 00170 do_sleep(1000); 00171 } 00172 00173 00174 00175 00176 int main() { 00177 your_scenario_example(); 00178 } 00179 00180 00181 // End of file 00182