cache_before_init.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 17:29.41 [dbg] some debug message after logs have been initialized 11

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 17:29.41 [1] first error 3
00002 17:29.41 [2] second error 5

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


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