Optimizations:
Logs:
Here's how the output will look like:
The debug output window:
The console:
00001 17:29.41 [app] hello, world 00002 17:29.41 [app] coolio 4 00003 17:29.41 [app] after logs have been initialized 10
The out.txt file:
00001 17:29.41 [app] hello, world 00002 17:29.41 [app] coolio 4 00003 17:29.41 [app] after logs have been initialized 10 00004 17:29.41 [dbg] some debug message after logs have been initialized 11
The err.txt file
00001 00060 #define BOOST_LOG_BEFORE_INIT_USE_CACHE_FILTER 00061 00062 // uncomment this, and all messages inside singleton's constructor will be logged! 00063 //#define BOOST_LOG_BEFORE_INIT_LOG_ALL 00064 00065 // uncomment this, and NO messages inside singleton's constructor will be logged 00066 //#define BOOST_LOG_BEFORE_INIT_IGNORE_BEFORE_INIT 00067 00068 #include <boost/logging/format_fwd.hpp> 00069 00070 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> ) 00071 00072 #include <boost/logging/format.hpp> 00073 00074 typedef boost::logging::logger_format_write< > logger_type; 00075 00076 00077 BOOST_DECLARE_LOG_FILTER(g_log_level, boost::logging::level::holder ) // holds the application log level 00078 BOOST_DECLARE_LOG(g_log_err, logger_type) 00079 BOOST_DECLARE_LOG(g_log_app, logger_type) 00080 BOOST_DECLARE_LOG(g_log_dbg, logger_type) 00081 00082 #define LDBG_ BOOST_LOG_USE_LOG_IF_LEVEL(g_log_dbg(), g_log_level(), debug ) << "[dbg] " 00083 #define LERR_ BOOST_LOG_USE_LOG_IF_LEVEL(g_log_err(), g_log_level(), error ) 00084 #define LAPP_ BOOST_LOG_USE_LOG_IF_LEVEL(g_log_app(), g_log_level(), info ) << "[app] " 00085 00086 BOOST_DEFINE_LOG_FILTER(g_log_level, boost::logging::level::holder ) 00087 BOOST_DEFINE_LOG(g_log_err, logger_type) 00088 BOOST_DEFINE_LOG(g_log_app, logger_type) 00089 BOOST_DEFINE_LOG(g_log_dbg, logger_type) 00090 00091 using namespace boost::logging; 00092 00093 struct singleton { 00094 singleton() { 00095 // note: these messages are written before logs are initialized 00096 int i = 1; 00097 LDBG_ << "this is so cool " << i++; 00098 LDBG_ << "this is so cool again " << i++; 00099 LERR_ << "first error " << i++; 00100 00101 std::string hello = "hello", world = "world"; 00102 LAPP_ << hello << ", " << world; 00103 00104 LAPP_ << "coolio " << i++; 00105 LERR_ << "second error " << i++; 00106 LDBG_ << "some debug message" << i++; 00107 } 00108 } s_; 00109 00110 void init_logs() { 00111 // Err log 00112 g_log_err()->writer().add_formatter( formatter::idx(), "[%] " ); 00113 g_log_err()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") ); 00114 g_log_err()->writer().add_formatter( formatter::append_newline() ); 00115 g_log_err()->writer().add_destination( destination::file("err.txt") ); 00116 00117 destination::file out("out.txt"); 00118 // App log 00119 g_log_app()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") ); 00120 g_log_app()->writer().add_formatter( formatter::append_newline() ); 00121 g_log_app()->writer().add_destination( out ); 00122 g_log_app()->writer().add_destination( destination::cout() ); 00123 00124 // Debug log 00125 g_log_dbg()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") ); 00126 g_log_dbg()->writer().add_formatter( formatter::append_newline() ); 00127 g_log_dbg()->writer().add_destination( out ); 00128 g_log_dbg()->writer().add_destination( destination::dbg_window() ); 00129 00130 // if you change this, you'll get a different output (more or less verbose) 00131 g_log_level()->set_enabled(level::info); 00132 00133 g_log_err()->mark_as_initialized(); 00134 g_log_app()->mark_as_initialized(); 00135 g_log_dbg()->mark_as_initialized(); 00136 } 00137 00138 void cache_before_init_example() { 00139 init_logs(); 00140 int i = 10; 00141 LAPP_ << "after logs have been initialized " << i++; 00142 g_log_level()->set_enabled(level::debug); 00143 LDBG_ << "some debug message after logs have been initialized " << i++; 00144 } 00145 00146 00147 00148 00149 int main() { 00150 cache_before_init_example(); 00151 } 00152 00153 00154 // End of file 00155