boost::logging::writer::format_write< formatter_base, destination_base, lock_resource, apply_format_and_write, router_type, formatter_array, destination_array > Struct Template Reference

Allows custom formatting of the message before logging it, and writing it to several destinations. More...

List of all members.

Public Member Functions

template<class formatter>
void add_formatter (formatter fmt)
 Adds a formatter.
template<class formatter>
void add_formatter (formatter fmt, const char_type *format_str)
 Adds a formatter. Also, the second argument is the spacer string.
template<class formatter>
void del_formatter (formatter fmt)
 Deletes a formatter.
template<class destination>
void add_destination (destination dest)
 Adds a destination.
template<class destination>
void del_destination (destination dest)
 Deletes a destination.
router_type & router ()
const router_type & router () const
template<class msg_type>
void operator() (msg_type &msg) const


Detailed Description

template<class formatter_base, class destination_base, class lock_resource = default_, class apply_format_and_write = default_, class router_type = msg_route::simple<formatter_base, destination_base, lock_resource>, class formatter_array = array::shared_ptr_holder<formatter_base>, class destination_array = array::shared_ptr_holder<destination_base>>
struct boost::logging::writer::format_write< formatter_base, destination_base, lock_resource, apply_format_and_write, router_type, formatter_array, destination_array >

Allows custom formatting of the message before logging it, and writing it to several destinations.

Once the message has been gathered, it's time to write it. The current class defines the following concepts:

You can add several formatters and destinations. Note that each formatter class and each destination class is a manipulator. Make sure you know what a manipulator is before using formatters and destinations.



The router object

Once you've added the formatters and destinations, the router comes into play. The router specifies how formatters and destinations are called. By default, all formatters are called first, in the order they were added, and then all destinations are called, in the order they were added. You can easily access the router() instance.

typedef logger< gather::ostream_like::return_cache_str<> , format_write< ... > > logger_type;
BOOST_DECLARE_LOG(g_l, logger_type) 
BOOST_DECLARE_LOG_FILTER(g_log_filter, filter::no_ts ) 
#define L_ BOOST_LOG_USE_LOG_IF_FILTER(g_l(), g_log_filter()->is_enabled() ) 

// add formatters : [idx] [time] message [enter]
g_l()->writer().add_formatter( formatter::idx() );
g_l()->writer().add_formatter( formatter::time("$hh:$mm.$ss ") );
g_l()->writer().add_formatter( formatter::append_newline() );

// write to cout and file
g_l()->writer().add_destination( destination::cout() );
g_l()->writer().add_destination( destination::file("out.txt") );

// usage
int i = 1;
L_ << "testing " << i << i+1 << i+2;

In the above case, formatter::idx() is called, then formatter::time(), then formatter::append_newline(). Now, the destinations are called: destination::cout(), and then destination::file().

Most of the time this is ok, and this is what the default router does. However, there are other routers in the msg_route namespace. For instance, take a look at msg_route::with_route class.



The apply_format_and_write object

Once the formatters and destinations are added, and you know the route, you have an extra object - the format_and_write - which contains the logic for calling the formatters and destinations. The format_and_write class knows how to call the formatters and destinations objects. Usually you'll be happy with the format_and_write::simple class - which simply calls operator() on the formatters , and operator() on the destinations. Otherwise, take a look at format_and_write namespace.

An object of this type (apply_format_and_write) is created for each new logged message.



Note:
This class is not thread-safe. If you want thread-safety, check out the other writer classes: on_dedicated_thread and ts_write


Parameters:
format_base The base class for all formatter classes from your application. See manipulator.
destination_base The base class for all destination classes from your application. See manipulator.
lock_resource How will we lock important resources - routing them (msg_route)
apply_format_and_write [optional] The class that knows how to call the formatters and destinations. See The apply_format_and_write object
router_type [optional] The class that knows when to call the formatters, and when to call the destinations. See The router object.


Remarks:
Normally the router could own the formatters and destination objects. However, then, it would need to own the objects, which would mean needing to come up with a smart pointer strategy. This would complicate the router logic. Also, iterating over formatters/destinations would be slower, if we were to keep smart pointers within the router itself.
Bug:
adding a spaced generic formatter and deleting the formatter - it won't happen

Member Function Documentation

template<class formatter_base, class destination_base, class lock_resource = default_, class apply_format_and_write = default_, class router_type = msg_route::simple<formatter_base, destination_base, lock_resource>, class formatter_array = array::shared_ptr_holder<formatter_base>, class destination_array = array::shared_ptr_holder<destination_base>>
template<class formatter>
void boost::logging::writer::format_write< formatter_base, destination_base, lock_resource, apply_format_and_write, router_type, formatter_array, destination_array >::add_formatter ( formatter  fmt  )  [inline]

Adds a formatter.

Parameters:
fmt The formatter

template<class formatter_base, class destination_base, class lock_resource = default_, class apply_format_and_write = default_, class router_type = msg_route::simple<formatter_base, destination_base, lock_resource>, class formatter_array = array::shared_ptr_holder<formatter_base>, class destination_array = array::shared_ptr_holder<destination_base>>
template<class formatter>
void boost::logging::writer::format_write< formatter_base, destination_base, lock_resource, apply_format_and_write, router_type, formatter_array, destination_array >::add_formatter ( formatter  fmt,
const char_type *  format_str 
) [inline]

Adds a formatter. Also, the second argument is the spacer string.

Parameters:
fmt The formatter
format_str The spacer string

template<class formatter_base, class destination_base, class lock_resource = default_, class apply_format_and_write = default_, class router_type = msg_route::simple<formatter_base, destination_base, lock_resource>, class formatter_array = array::shared_ptr_holder<formatter_base>, class destination_array = array::shared_ptr_holder<destination_base>>
template<class formatter>
void boost::logging::writer::format_write< formatter_base, destination_base, lock_resource, apply_format_and_write, router_type, formatter_array, destination_array >::del_formatter ( formatter  fmt  )  [inline]

Deletes a formatter.

Parameters:
fmt The formatter to delete

template<class formatter_base, class destination_base, class lock_resource = default_, class apply_format_and_write = default_, class router_type = msg_route::simple<formatter_base, destination_base, lock_resource>, class formatter_array = array::shared_ptr_holder<formatter_base>, class destination_array = array::shared_ptr_holder<destination_base>>
router_type& boost::logging::writer::format_write< formatter_base, destination_base, lock_resource, apply_format_and_write, router_type, formatter_array, destination_array >::router (  )  [inline]

returns the object that actually routes the message

template<class formatter_base, class destination_base, class lock_resource = default_, class apply_format_and_write = default_, class router_type = msg_route::simple<formatter_base, destination_base, lock_resource>, class formatter_array = array::shared_ptr_holder<formatter_base>, class destination_array = array::shared_ptr_holder<destination_base>>
const router_type& boost::logging::writer::format_write< formatter_base, destination_base, lock_resource, apply_format_and_write, router_type, formatter_array, destination_array >::router (  )  const [inline]

returns the object that actually routes the message

template<class formatter_base, class destination_base, class lock_resource = default_, class apply_format_and_write = default_, class router_type = msg_route::simple<formatter_base, destination_base, lock_resource>, class formatter_array = array::shared_ptr_holder<formatter_base>, class destination_array = array::shared_ptr_holder<destination_base>>
template<class msg_type>
void boost::logging::writer::format_write< formatter_base, destination_base, lock_resource, apply_format_and_write, router_type, formatter_array, destination_array >::operator() ( msg_type &  msg  )  const [inline]

does the actual write


The documentation for this struct was generated from the following file:

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