Boost logo

Boost :

Subject: Re: [boost] Boost.log: set_channel name dynamically for a basic_composite_logger
From: syvyi (alexander.svk_at_[hidden])
Date: 2012-10-14 12:15:09


Here is the whole code:

// boost_logging.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#pragma comment(lib,"libboost_log-vc100-mt-gd-1_51.lib")
#pragma comment(lib,"libboost_date_time-vc100-mt-gd-1_51.lib")
#include <boost/log/core.hpp>
//#include <boost/log/sources/channel_logger.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/filters.hpp>
#include <boost/log/formatters.hpp>
#include <boost/log/exceptions.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sources/exception_handler_feature.hpp>
//#include <boost/log/support/exception.hpp>
#include <boost/log/utility/exception_handler.hpp>
#include <boost/log/sources/channel_feature.hpp>

#include <boost/make_shared.hpp>

#include <boost/thread.hpp>
#include <fstream>

namespace logging = boost::log;
namespace sinks = boost::log::sinks;
namespace src = boost::log::sources;
namespace fmt = boost::log::formatters;
namespace flt = boost::log::filters;
namespace attrs = boost::log::attributes;
namespace keywords = boost::log::keywords;

template <class T>
void init( T& );

enum severity_levels
{
    normal,
    warning,
    error
};

class my_logger_mt :
    public src::basic_composite_logger<
        char,
        my_logger_mt,
                src::multi_thread_model<boost::log2_mt_nt5::aux::light_rw_mutex>,
        src::features<
            src::severity< severity_levels >,
            src::exception_handler,
                        src::channel< std::string >
>
>
{
    BOOST_LOG_FORWARD_LOGGER_CONSTRUCTORS(my_logger_mt)
};

BOOST_LOG_DECLARE_GLOBAL_LOGGER_INIT(my_logger, my_logger_mt)
{
        my_logger_mt lg( keywords::channel = "channel!" );
// my_logger_mt lg;
    // Set up exception handler: all exceptions that occur while
    // logging through this logger, will be suppressed
    lg.set_exception_handler(logging::make_exception_suppressor());

    return lg;
}

// The formatting logic for the severity level
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
    std::basic_ostream< CharT, TraitsT >& strm, severity_levels lvl)
{
    static const char* const str[] =
    {
        "normal",
        "notification",
        "warning",
        "error",
        "critical"
    };
    if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
        strm << str[lvl];
    else
        strm << static_cast< int >(lvl);
    return strm;
}

inline int foo(src::logger& lg)
{
    BOOST_LOG_FUNCTION();
    BOOST_LOG(lg) << "foo is being called";
    return 10;
}

int _tmain(int argc, _TCHAR* argv[])
{
        typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
        boost::shared_ptr< file_sink > sink_ptr = boost::make_shared< file_sink >
                (
                        keywords::file_name = ".//log//%N.log"
                        //keywords::rotation_size = 5 * 1024 * 1024
                );
        init< boost::shared_ptr< file_sink > >( sink_ptr );
        src::logger lg;
        boost::shared_ptr< logging::attribute > pCounter(new attrs::counter<
unsigned int >(1));
        logging::core::get()->add_global_attribute("LineID", pCounter);
        boost::shared_ptr< logging::attribute > pTimeStamp(new
attrs::local_clock());
    logging::core::get()->add_global_attribute("Date_time", pTimeStamp);
        boost::shared_ptr< logging::attribute > pNamedScope(new
attrs::named_scope());
    logging::core::get()->add_global_attribute("Scope", pNamedScope);
        BOOST_LOG_FUNCTION();
// my_logger::logger_type l = my_logger::get();
// std::cout << my_logger::get().channel();
// my_logger::get().set_attributes(
my_logger::logger_type::attribute_set_type
// my_logger::get().
        my_logger::get().channel("NEW_CHANNEL");
        BOOST_LOG(my_logger::get()) << "Some log line with a counter";
    BOOST_LOG(lg) << "Another log line with the counter";
        sink_ptr->set_filter( flt::attr< severity_levels >("Severity",
std::nothrow) >= warning );
        src::severity_logger< severity_levels > slg;
        BOOST_LOG_SEV(slg, normal) << "A normal severity message, will not pass to
the output";
    BOOST_LOG_SEV(slg, error) << "An error severity message, will pass to
the output";
        sink_ptr->reset_filter( );
        try{
        BOOST_LOG(lg) << "The result of foo is " << foo(lg);
        }
        catch(std::exception& e){std::cout<<e.what();}
        return 0;
}

template &lt;class T>
void init( T & sink_ptr )
{
        boost::shared_ptr< logging::core > core( logging::core::get( ) );

// sink_ptr->locked_backend( )->set_file_name_pattern( ".//log//%N.log" );
        //boost::shared_ptr< std::ofstream > pStream2(new std::ofstream("%N.log"));
        sink_ptr->locked_backend( )->set_formatter
        (
                fmt::format( "%1% : [%2%] [%3%] [%4%] [%5%] %6%" )
                % fmt::attr< boost::uint32_t > ( "LineID" )
                % fmt::attr< std::string > ( "Channel", std::nothrow )
                % fmt::date_time< boost::posix_time::ptime > ( "Date_time",
keywords::format = "%Y-%m-%d-%H-%M" )
                % fmt::attr< severity_levels >("Severity", std::nothrow)
                % fmt::named_scope( "Scope", keywords::iteration = fmt::reverse,
keywords::delimiter = " | ")
                % fmt::message( )
        );
        sink_ptr->locked_backend( )->auto_flush( true );
// sink_ptr->set_filter( flt::attr< logging::trivial::severity_level
>("Severity") >= logging::trivial::info );

        sink_ptr->locked_backend( )->set_file_collector(
sinks::file::make_collector(
                keywords::target = ".//log",
                keywords::min_free_space = 50 * 1024 * 1024,
                keywords::max_size = 5 * 1024 * 1024
                ));
        sink_ptr->locked_backend( )->scan_for_files( );

        core->add_sink( sink_ptr );
}

--
View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-for-a-basic-composite-logger-tp4637012p4637141.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk