ded_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 look similar to:

00001 ...
00002 30:33 [10] message 1
00003 30:33 [11] message 2
00004 30:33 [12] message 2
00005 30:33 [13] message 2
00006 30:33 [14] message 2
00007 30:33 [15] message 3
00008 30:33 [16] message 2
00009 30:33 [17] message 3
00010 30:33 [18] message 3
00011 30:33 [19] message 4
00012 30:33 [20] message 3
00013 30:33 [21] message 3
00014 30:33 [22] message 4
00015 30:33 [23] message 4
00016 30:33 [24] message 4
00017 30:33 [25] message 4
00018 30:33 [26] message 5
00019 30:33 [27] message 5
00020 30:33 [28] message 6
00021 30:33 [29] message 6
00022 30:33 [30] message 5
00023 30:33 [31] message 5
00024 30:33 [32] message 5
00025 30:33 [33] message 6
00026 30:33 [34] message 7
00027 ...

00001 
00072 #include <boost/logging/format_fwd.hpp>
00073 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> )
00074 
00075 #include <boost/logging/format_ts.hpp>
00076 #include <boost/logging/format/formatter/thread_id.hpp>
00077 #include <boost/thread/thread.hpp>
00078 #include <boost/thread/xtime.hpp>
00079 
00080 using namespace boost::logging;
00081 
00082 typedef logger_format_write< default_, default_, writer::threading::on_dedicated_thread > logger_type;
00083 
00084 BOOST_DECLARE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00085 BOOST_DECLARE_LOG(g_l, logger_type) 
00086 
00087 #define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() ) 
00088 
00089 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts ) 
00090 BOOST_DEFINE_LOG(g_l, logger_type)
00091 
00092 void do_sleep(int ms) {
00093     using namespace boost;
00094     xtime next;
00095     xtime_get( &next, TIME_UTC);
00096     next.nsec += (ms % 1000) * 1000000;
00097 
00098     int nano_per_sec = 1000000000;
00099     next.sec += next.nsec / nano_per_sec;
00100     next.sec += ms / 1000;
00101     next.nsec %= nano_per_sec;
00102     thread::sleep( next);
00103 }
00104 
00105 void use_log_thread() {
00106     for ( int i = 0; i < 20; ++i) {
00107         L_ << "message " << i ;
00108         do_sleep(1);
00109     }
00110 }
00111 
00112 void ts_logger_one_filter_example() {
00113     g_l()->writer().add_formatter( formatter::idx(), "[%] "  );
00114     g_l()->writer().add_formatter( formatter::time("$mm:$ss ") );
00115     g_l()->writer().add_formatter( formatter::append_newline() );
00116     g_l()->writer().add_destination( destination::file("out.txt") );
00117     g_l()->writer().add_destination( destination::dbg_window() );
00118     g_l()->mark_as_initialized();
00119 
00120     for ( int i = 0 ; i < 5; ++i)
00121         boost::thread t( &use_log_thread);
00122 
00123     // allow for all threads to finish
00124     std::cout << "sleep 5s " << std::endl;
00125     do_sleep( 5000);
00126 }
00127 
00128 
00129 
00130 
00131 int main() {
00132     ts_logger_one_filter_example();
00133 }
00134 
00135 
00136 // End of file
00137 


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