custom_fmt_dest.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 example shows you how easy it is to add your custom formatter /destination classes.

This usage:



Custom classes:



Optimizations:



The output will look similar to this one:

The console and the debug window will be the same:

00001 +6s [1] this is so cool 1
00002 +6s [2] this is so cool again 2
00003 +7s [3] hello, world
00004 +7s [4] good to be back ;) 3

The out.txt file will look like this:

00001 <msg>+6s [1] this is so cool 1
00002 </msg>
00003 <msg>+6s [2] this is so cool again 2
00004 </msg>
00005 <msg>+7s [3] hello, world
00006 </msg>
00007 <msg>+7s [4] good to be back ;) 3
00008 </msg>

00001 
00075 #include <boost/logging/format_fwd.hpp>
00076 
00077 BOOST_LOG_FORMAT_MSG( optimize::cache_string_one_str<> )
00078 
00079 #include <boost/logging/format.hpp>
00080 using namespace boost::logging;
00081 
00082 typedef logger_format_write< default_, default_, writer::threading::no_ts > logger_type;
00083 
00084 
00085 BOOST_DECLARE_LOG_FILTER(g_log_filter, filter::no_ts )
00086 BOOST_DECLARE_LOG(g_l, logger_type) 
00087 
00088 #define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() ) 
00089 
00090 BOOST_DEFINE_LOG(g_l, logger_type)
00091 BOOST_DEFINE_LOG_FILTER(g_log_filter, filter::no_ts )
00092 
00093 
00094 
00095 // Example of custom formatter:
00096 // dump the no. of seconds since start of program
00097 struct secs_since_start : formatter::class_<secs_since_start, formatter::implement_op_equal::no_context> {
00098     ::time_t m_start;
00099     secs_since_start() : m_start( ::time(0) ) {}
00100     void operator()(param str) const {
00101         ::time_t now = ::time(0);
00102         std::stringstream out;
00103         out << "+" << (int)(now-m_start) << "s ";
00104         str.prepend_string( out.str() );
00105     }
00106 };
00107 
00108 // Example of custom destination:
00109 // Dump each message as XML
00110 struct as_xml : 
00111         destination::class_<as_xml, destination::implement_op_equal::has_context>, 
00112         destination::non_const_context<std::ofstream> {
00113 
00114     std::string m_name;
00115     as_xml(const char* name) : non_const_context_base(name), m_name(name) {}
00116     void operator()(param str) const {
00117         context() << "<msg>" << str << "</msg>" << std::endl; 
00118     }
00119 
00120     bool operator==(const as_xml& other) const { return m_name == other.m_name; }
00121 };
00122 
00123 
00124 void custom_fmt_dest_example() {
00125     //         add formatters and destinations
00126     //         That is, how the message is to be formatted and where should it be written to
00127     g_l()->writer().add_formatter( formatter::idx(), "[%] " );
00128     g_l()->writer().add_formatter( formatter::append_newline() );
00129     g_l()->writer().add_formatter( secs_since_start() );
00130 
00131     g_l()->writer().add_destination( destination::cout() );
00132     g_l()->writer().add_destination( destination::dbg_window() );
00133     g_l()->writer().add_destination( as_xml("out.txt") );
00134     g_l()->mark_as_initialized();
00135 
00136     int i = 1;
00137     L_ << "this is so cool " << i++;
00138     L_ << "this is so cool again " << i++;
00139 
00140     std::string hello = "hello", world = "world";
00141     L_ << hello << ", " << world;
00142 
00143     g_log_filter()->set_enabled(false);
00144     L_ << "this will not be written to the log";
00145     L_ << "this won't be written to the log";
00146 
00147     g_log_filter()->set_enabled(true);
00148     L_ << "good to be back ;) " << i++;
00149 }
00150 
00151 
00152 
00153 int main() {
00154     custom_fmt_dest_example();
00155 }
00156 
00157 
00158 // End of file


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