Boost.log: set_channel name dynamically for a basic_composite_logger

Hi, how to set attribute dynamically for a composite logger? lass my_logger_mt : public src::basic_composite_logger< char, my_logger_mt, src::multi_thread_model<boost::log_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) }; -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Fri, Oct 12, 2012 at 3:35 PM, syvyi <alexander.svk@gmail.com> wrote:
Hi,
how to set attribute dynamically for a composite logger?
Channel name is immutable in v1 but in v2 it can be changed by calling "logger.channel(name)".

In boost v. 1.51 there is no logging, so when I downloaded boost.log from sourceforge, I needed to get static library for it and compiled it with all files from libs/src. It worked. I have what a version it has :) -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Fri, Oct 12, 2012 at 8:03 PM, syvyi <alexander.svk@gmail.com> wrote:
In boost v. 1.51 there is no logging, so when I downloaded boost.log from sourceforge, I needed to get static library for it and compiled it with all files from libs/src. It worked. I have what a version it has :)
If you downloaded from files section then most probably that was v1 (as the file name suggests). v2 is in SVN trunk and branches/bleeding-edge.

I downloaded new version from http://sourceforge.net/projects/boost-log/files/boost-log2-snapshot-628.zip/... <http://sourceforge.net/projects/boost-log/files/boost-log2-snapshot-628.zip/download> Same code was not compiled // 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::log_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 <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 ); } 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(50): error C3083: 'log_mt_nt5': the symbol to the left of a '::' must be a type 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(50): error C3083: 'aux': the symbol to the left of a '::' must be a type 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(50): error C2039: 'light_rw_mutex' : is not a member of 'boost' 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(50): error C2065: 'light_rw_mutex' : undeclared identifier 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(56): error C3203: 'multi_thread_model' : unspecialized class template can't be used as a template argument for template parameter 'ThreadingModelT', expected a real type 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(56): error C2955: 'boost::log2_mt_nt5::sources::multi_thread_model' : use of class template requires template argument list ... and so on. -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Oct 12, 2012 9:13 PM, "syvyi" <alexander.svk@gmail.com> wrote:
I downloaded new version from
http://sourceforge.net/projects/boost-log/files/boost-log2-snapshot-628.zip/...
< http://sourceforge.net/projects/boost-log/files/boost-log2-snapshot-628.zip/...
That was a snapshot of v2 at some point. I would recommend using trunk instead, if you want v2. By the compilation errors it looks like you mixed v1 and v2 somehow. Please verify that Boost.Log sources are consistent. Also, make sure you didn't leave precompiled headers lying somewhere around.

I've been to http://svn.boost.org/svn/boost/trunk/ <http://svn.boost.org/svn/boost/trunk/> However, I did not find log in a neither in libs nor in trunk. It will save time to us both if you give me a link. -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Oct 13, 2012 12:28 AM, "syvyi" <alexander.svk@gmail.com> wrote:
I've been to http://svn.boost.org/svn/boost/trunk/ <http://svn.boost.org/svn/boost/trunk/> However, I did not find log in a neither in libs nor in trunk. It will save time to us both if you give me a link.
Boost.Log is hosted on Sourceforge. http://sourceforge.net/scm/?type=svn&group_id=199644

Andrey, do you now recommend using the bleeding edge branch or is it still too...bleeding? Joel Lamotte

On Oct 13, 2012 1:35 AM, "Klaim - Joël Lamotte" <mjklaim@gmail.com> wrote:
Andrey, do you now recommend using the bleeding edge branch or is it still too...bleeding?
No, bleeding-edge will always be my playground. As soon as its features become more stable I will merge them to trunk.

Andrey, here https://boost-log.svn.sourceforge.net/svnroot/boost-log/trunk/ <https://boost-log.svn.sourceforge.net/svnroot/boost-log/trunk/> is a page, but I do not know how to download all the source files once. Tell me please how, or put one .zip file to sourceforge with a v.2. -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Saturday 13 October 2012 01:44:16 syvyi wrote:
Andrey, here https://boost-log.svn.sourceforge.net/svnroot/boost-log/trunk/ <https://boost-log.svn.sourceforge.net/svnroot/boost-log/trunk/> is a page, but I do not know how to download all the source files once.
Tell me please how, or put one .zip file to sourceforge with a v.2.
The link I posted in my previous reply leads to a page that describes how to checkout the source code. You will have to install an SVN client locally to do that.

I checked out a trunk. After replacement there're still compiler complains: first of all I needed to edit src::multi_thread_model<boost::log2_mt_nt5::aux::light_rw_mutex> where I put log2_mt_nt5 instead of log_mt_nt5. If there's a new documentation for v.2, tell. 1>------ Build started: Project: boost_logging, Configuration: Debug Win32 ------ 1>Build started 10/13/2012 6:00:05 PM. 1>InitializeBuildStatus: 1> Touching "Debug\boost_logging.unsuccessfulbuild". 1>ClCompile: 1> stdafx.cpp 1> boost_logging.cpp 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(61): error C2065: 'my_logger' : undeclared identifier 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(61): error C2275: 'my_logger_mt' : illegal use of this type as an expression 1> c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(46) : see declaration of 'my_logger_mt' 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(62): error C2448: 'BOOST_LOG_DECLARE_GLOBAL_LOGGER_INIT' : function-style initializer appears to be a function definition 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(92): error C2039: 'logger' : is not a member of 'boost::log2_mt_nt5::sources' 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(92): error C2065: 'logger' : undeclared identifier 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(92): error C2065: 'lg' : undeclared identifier 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(93): error C2448: 'foo' : function-style initializer appears to be a function definition 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(108): error C2039: 'logger' : is not a member of 'boost::log2_mt_nt5::sources' 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(108): error C2065: 'logger' : undeclared identifier 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(108): error C2146: syntax error : missing ';' before identifier 'lg' 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(108): error C2065: 'lg' : undeclared identifier 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(110): error C2664: 'boost::log2_mt_nt5::basic_core<CharT>::add_global_attribute' : cannot convert parameter 2 from 'boost::shared_ptr<T>' to 'const boost::log2_mt_nt5::attribute &' 1> with 1> [ 1> CharT=char 1> ] 1> and 1> [ 1> T=boost::log2_mt_nt5::attribute 1> ] 1> Reason: cannot convert from 'boost::shared_ptr<T>' to 'const boost::log2_mt_nt5::attribute' 1> with 1> [ 1> T=boost::log2_mt_nt5::attribute 1> ] 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(112): error C2664: 'boost::log2_mt_nt5::basic_core<CharT>::add_global_attribute' : cannot convert parameter 2 from 'boost::shared_ptr<T>' to 'const boost::log2_mt_nt5::attribute &' 1> with 1> [ 1> CharT=char 1> ] 1> and 1> [ 1> T=boost::log2_mt_nt5::attribute 1> ] 1> Reason: cannot convert from 'boost::shared_ptr<T>' to 'const boost::log2_mt_nt5::attribute' 1> with 1> [ 1> T=boost::log2_mt_nt5::attribute 1> ] 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(114): error C2664: 'boost::log2_mt_nt5::basic_core<CharT>::add_global_attribute' : cannot convert parameter 2 from 'boost::shared_ptr<T>' to 'const boost::log2_mt_nt5::attribute &' 1> with 1> [ 1> CharT=char 1> ] 1> and 1> [ 1> T=boost::log2_mt_nt5::attribute 1> ] 1> Reason: cannot convert from 'boost::shared_ptr<T>' to 'const boost::log2_mt_nt5::attribute' 1> with 1> [ 1> T=boost::log2_mt_nt5::attribute 1> ] 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(120): error C2653: 'my_logger' : is not a class or namespace name 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(120): error C2228: left of '.channel' must have class/struct/union 1> type is ''unknown-type'' 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(120): error C3861: 'get': identifier not found 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(121): error C2653: 'my_logger' : is not a class or namespace name 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(121): error C2228: left of '.open_record' must have class/struct/union 1> type is ''unknown-type'' 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(121): error C3861: 'get': identifier not found 1>c:\users\alexander\documents\visual studio 2010\projects\sankom\boost_logging\boost_logging\boost_logging.cpp(121): fatal error C1903: unable to recover from previous error(s); stopping compilation 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:11.70 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Oct 13, 2012 7:02 PM, "syvyi" <alexander.svk@gmail.com> wrote:
I checked out a trunk. After replacement there're still compiler
complains:
first of all I needed to edit src::multi_thread_model<boost::log2_mt_nt5::aux::light_rw_mutex> where I
put
log2_mt_nt5 instead of log_mt_nt5.
First, don't use these namespace names, they will change. Always use boost::log namespace.
If there's a new documentation for v.2, tell.
It's not available yet, sorry. Replace BOOST_LOG_DECLARE_GLOBAL_LOGGER_INIT with BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT. Also, create and pass attributes by value to add_*() methods instead of creating a shared_ptr to the attribute. I cannot tell more without seeing the code.

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 <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-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

Andrey, tell if I have to make any changes in a code to use v.2 -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Sunday, October 14, 2012, syvyi <alexander.svk@gmail.com> wrote:
Here is the whole code:
[snip]
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>, Don't use anything from aux namespace, it's implementation detail. I suggest using boost::shared_mutex instead.
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)
Replace with BOOST_LOG_INLINE_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; }
[snip]
int _tmain(int argc, _TCHAR* argv[]) {
[snip]
boost::shared_ptr< logging::attribute > pCounter(new
attrs::counter<
unsigned int >(1)); logging::core::get()->add_global_attribute("LineID", pCounter);
Replace with: logging::core::get()->add_global_attribute("LineID", attrs::counter< unsigned int >(1)); Similarly modify the following attribute adding code.
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);
[snip]

manster_logging_test.rar <http://boost.2283326.n4.nabble.com/file/n4637182/manster_logging_test.rar> Andrey, I am using v.1.1 in code attached in file. It is a test of 2 files. Please, take a look at those files, tell if you succeed to compile them. This is the same code as above but without adapter, which worked. Info: after tracing I get Here is the problem, WatForSingleObject is called and after one BOOST_LOG macros app. is frozen. basic_time_mutex.cpp void lock() { if(try_lock()) { return; } long old_count=active_count; mark_waiting_and_try_lock(old_count); if(old_count&lock_flag_value) { bool lock_acquired=false; void* const sem=get_event(); do { BOOST_VERIFY(win32::WaitForSingleObject( sem,::boost::detail::win32::infinite)==0); clear_waiting_and_try_lock(old_count); lock_acquired=!(old_count&lock_flag_value); } while(!lock_acquired); } } Then function below returns false. sink_frontends.cpp template< typename CharT > BOOST_LOG_EXPORT bool synchronous_frontend< CharT >::try_consume(record_type const& record) { register implementation* pImpl = this->BOOST_NESTED_TEMPLATE get_impl< implementation >(); try { unique_lock< mutex > lock(pImpl->m_BackendMutex, try_to_lock); if (lock.owns_lock()) { (pImpl->m_Consume)(pImpl->m_pBackend.get(), record); return true; } else return false; // returns } in locks.hpp bool try_lock() { if(m==0) { boost::throw_exception(boost::lock_error(system::errc::operation_not_permitted, "boost unique_lock has no mutex")); } if(owns_lock()) // failed is_locked=false; -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Tue, Oct 16, 2012 at 12:48 AM, syvyi <alexander.svk@gmail.com> wrote:
manster_logging_test.rar <http://boost.2283326.n4.nabble.com/file/n4637182/manster_logging_test.rar> Andrey, I am using v.1.1 in code attached in file. It is a test of 2 files. Please, take a look at those files, tell if you succeed to compile them. This is the same code as above but without adapter, which worked.
Info:
after tracing I get
Here is the problem, WatForSingleObject is called and after one BOOST_LOG macros app. is frozen.
The problem is because you keep a locked_backend_ptr within the logger. While this pointer exists the sink backend is locked and thus you deadlock when you try to log. This pointer should only be kept for the scope where you setup the sink.

Thank you. -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

One more thing, why when I put BOOST_LOG_SEV(lg->get_logman_inet(), manster::logger::error) << "Severe msg"; in a file I see digits of a severity level instead of alphabetical-ids? -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Tue, Oct 16, 2012 at 10:23 PM, syvyi <alexander.svk@gmail.com> wrote:
One more thing, why when I put BOOST_LOG_SEV(lg->get_logman_inet(), manster::logger::error) << "Severe msg"; in a file I see digits of a severity level instead of alphabetical-ids?
I compiled your code (after modifications) and it worked correctly for me. Are you sure you didn't accidentally hide the operator<< from the formatter initialization?

Andrey, I deleted the previous post at once after posting it. Yes, I made output operator friend to class and of course it did not find it in class namespace for severity level output way. -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

Why do you recommend using boost::shared_mutex instead of boost::log::aux::light_rw_mutex ? -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Oct 19, 2012 11:52 PM, "syvyi" <alexander.svk@gmail.com> wrote:
Why do you recommend using boost::shared_mutex instead of boost::log::aux::light_rw_mutex ?
Like I said, it's not for public use. I can remove or change it any moment.

I need to be UNICODE friendly. What is analog to BOOST_LOG for UNICODE? #pragma once /* * Copyright Alexander Sivak 2012 - 2013. */ /*! * \file logger\manster_logger.h * \author Alexander Sivak * \date 16.10.2012 * * This header contains logger class declaration. */ #pragma comment(lib, "Shlwapi.lib") // PathStripPath #include <tchar.h> #include <Shlwapi.h> #include <string> #include <boost/typeof/typeof.hpp> #include <boost/type_traits/is_convertible.hpp> #include <boost/type_traits/is_same.hpp> // SYSTEM // #include <string> #include <map> #include <Windows.h> // PROJECT // #include <boost/log/core.hpp> #include <boost/log/attributes.hpp> #include <boost/log/filters.hpp> #include <boost/log/formatters.hpp> #include <boost/log/sinks.hpp> #include <boost/log/utility/exception_handler.hpp> #include <boost/log/sources/exception_handler_feature.hpp> #include <boost/log/sources/channel_feature.hpp> #include <boost/thread.hpp> #include <boost/make_shared.hpp> #include <boost/scoped_ptr.hpp> #include <boost/log/trivial.hpp> #include <boost/thread/shared_mutex.hpp> // LOCAL // 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; #define MANSTER_LOG_DECLARE_GLOBAL_LOGGER_ARGS_INIT( tag_name, lg, args )\ struct tag_name\ {\ typedef lg logger_type;\ enum registration_line_t { registration_line = __LINE__ };\ static const char* registration_file() { return __FILE__; }\ static logger_type construct_logger()\ {\ return logger_type(BOOST_PP_SEQ_ENUM(args));\ }\ static inline logger_type& get()\ {\ return ::boost::log::sources::aux::logger_singleton< tag_name
::get();\ }\ };\ inline tag_name::logger_type& BOOST_JOIN(get_, tag_name)()\ {\ return tag_name::get();\ }
enum severity_level { information, warning, error, fatal }; template< typename CharT, typename TraitsT > inline std::basic_ostream< CharT, TraitsT >& operator<< ( std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl) { static const char* const str[] = { "information", "warning", "error", "fatal" }; if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str))) strm << str[lvl]; else strm << static_cast< int >(lvl); return strm; } namespace manster { class logger // Non-copyconstractable and non-copyable, since scoped_ptr is used. { public: // TYPES typedef boost::shared_ptr< logger > logger_ptr; enum logger_type { db, sys, gui, inet }; // Compiler copy_ctor/dtor/operator= // OPERATIONS static logger_ptr const & instance() { if ( !instance_.get( ) ) instance_.reset( new logger( ) ); return instance_; } class manster_logger_mt; void log( logger_type const & lg_type, std::string const & logged_data ) { if ( initialized ) BOOST_LOG( loggers_map[ lg_type ] ) << logged_data.data( ); } void log( logger_type const & lg_type, std::string const & logged_data, severity_level const & lvl ) { if ( initialized ) BOOST_LOG_SEV( loggers_map[ lg_type ], lvl ) << logged_data.data( ); } void log( logger_type const & lg_type, std::wstring const & logged_data ) { if ( initialized ) BOOST_LOG( wloggers_map[ lg_type ] ) << logged_data.data( ); } void log( logger_type const & lg_type, std::wstring const & logged_data, severity_level const & lvl ) { if ( initialized ) BOOST_LOG_SEV( wloggers_map[ lg_type ], lvl ) << logged_data.data( ); } void initialize( ); // ACCESS void set_low_bound_filter( severity_level const & lvl ) { sink_ptr->set_filter( flt::attr< severity_level >( "Severity", std::nothrow) >= lvl ); } public: // TYPES // Multibyte character logger type. class manster_logger_mt : public src::basic_composite_logger< char, manster_logger_mt, // src::multi_thread_model<logging::aux::light_rw_mutex>, src::multi_thread_model< boost::shared_mutex >, src::features< src::severity< severity_level >, // src::exception_handler, src::channel< std::string > > > { BOOST_LOG_FORWARD_LOGGER_CONSTRUCTORS( manster_logger_mt ) }; // MULTIBYTE CHARACTER LOGGERS MANSTER_LOG_DECLARE_GLOBAL_LOGGER_ARGS_INIT( logman_gui, manster_logger_mt, (keywords::severity = information)(keywords::channel = "GUI")) MANSTER_LOG_DECLARE_GLOBAL_LOGGER_ARGS_INIT( logman_inet, manster_logger_mt, (keywords::severity = information)(keywords::channel = "Inet")) MANSTER_LOG_DECLARE_GLOBAL_LOGGER_ARGS_INIT( logman_db, manster_logger_mt, (keywords::severity = information)(keywords::channel = "DataBase")) MANSTER_LOG_DECLARE_GLOBAL_LOGGER_ARGS_INIT( logman_sys, manster_logger_mt, (keywords::severity = information)(keywords::channel = "Sys")) // Wide-character logger type. class manster_wlogger_mt : public src::basic_composite_logger< wchar_t, manster_wlogger_mt, src::multi_thread_model< boost::shared_mutex >, src::features< src::severity< severity_level >, src::channel< std::wstring > > > { BOOST_LOG_FORWARD_LOGGER_CONSTRUCTORS( manster_wlogger_mt ) }; // WIDE-BYTE LOGGERS MANSTER_LOG_DECLARE_GLOBAL_LOGGER_ARGS_INIT( wlogman_gui, manster_wlogger_mt, (keywords::severity = information)(keywords::channel = L"GUI")) MANSTER_LOG_DECLARE_GLOBAL_LOGGER_ARGS_INIT( wlogman_inet, manster_wlogger_mt, (keywords::severity = information)(keywords::channel = L"Inet")) MANSTER_LOG_DECLARE_GLOBAL_LOGGER_ARGS_INIT( wlogman_db, manster_wlogger_mt, (keywords::severity = information)(keywords::channel = L"DataBase")) MANSTER_LOG_DECLARE_GLOBAL_LOGGER_ARGS_INIT( wlogman_sys, manster_wlogger_mt, (keywords::severity = information)(keywords::channel = L"Sys")) // FIELDS static bool initialized; private: // LIFECYCLE logger() : sink_ptr( boost::make_shared< file_sink > ( keywords::file_name = ".//log//%N.log" ) ) { } // FIELDS && TYPES typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink; boost::shared_ptr< file_sink > sink_ptr; static logger_ptr instance_; enum byteness { unicode, multi }; typedef std::map< logger_type, manster_logger_mt > log_type_to_log; static log_type_to_log loggers_map; typedef std::map< logger_type, manster_wlogger_mt > log_type_to_wlog; static log_type_to_wlog wloggers_map; }; } typedef std::basic_string< TCHAR > stl_tchs; BOOST_TYPEOF_REGISTER_TYPE( severity_level ); BOOST_TYPEOF_REGISTER_TYPE( manster::logger::logger_type ); //! Adds line number and file name to a message. inline std::basic_string< TCHAR > locate_msg( stl_tchs const & msg, stl_tchs const & line, stl_tchs const & file ) { PathStripPath( const_cast< LPTSTR >( file.data( ) ) ); stl_tchs lm = msg; static stl_tchs const fhead = _T( " File: " ); static stl_tchs const lhead = _T( " Line: " ); lm += fhead + file + lhead + line; return lm; } //! Types. conseq: std::basic_string<TCHAR>; reason: std::basic_string<TCHAR>||char[const]*[const]. #define CONS_REAS_CONCAT( conseq, reason ) conseq + _T( ":" ) + reason typedef std::basic_string< TCHAR > stl_str; inline void LOG( stl_str const & reason, stl_str const & conseq, manster::logger::logger_type const & lgr_type, severity_level const & sev_lvl, bool const & with_file_line )\ {\ if ( ( (boost::is_convertible< BOOST_TYPEOF( reason ), std::string
::value && \ boost::is_convertible< BOOST_TYPEOF( conseq ), std::string >::value) ||\ (boost::is_convertible< BOOST_TYPEOF( reason ), std::wstring >::value && \ boost::is_convertible< BOOST_TYPEOF( conseq ), std::wstring >::value ) ) &&\ ( boost::is_same< BOOST_TYPEOF( lgr_type ), manster::logger::logger_type ::value ) &&\ ( boost::is_same< BOOST_TYPEOF( sev_lvl ), severity_level >::value ) &&\ ( boost::is_same< BOOST_TYPEOF( with_file_line ), bool >::value ) )\ manster::logger::instance( )->log(\ lgr_type,\ with_file_line ?\ locate_msg( CONS_REAS_CONCAT( conseq, reason ), boost::lexical_cast< stl_tchs >( __LINE__ ), _T( __FILE__ ) ) :\ CONS_REAS_CONCAT( conseq, reason ),\ sev_lvl );\ }
/////////////////////////////////////////////////////////// // GUI LOGGING #define LOG_GUI( reason, conseq, sev_lvl, with_file_line )\ LOG( reason, conseq, manster::logger::gui, sev_lvl, with_file_line ) #define LOG_GUI_FATAL( reason, conseq, with_file_line )\ LOG_GUI( reason, conseq, severity_level::fatal, with_file_line ) #define LOG_GUI_ERR( reason, conseq, with_file_line )\ LOG_GUI( reason, conseq, severity_level::error, with_file_line ) #define LOG_GUI_WARN( reason, conseq, with_file_line )\ LOG_GUI( reason, conseq, severity_level::warning, with_file_line ) #define LOG_GUI_INFO( reason, conseq, with_file_line )\ LOG_GUI( reason, conseq, severity_level::information, with_file_line ) /////////////////////////////////////////////////////////// // SYS LOGGING //! System type logger used. #define LOG_SYS( reason, conseq, sev_lvl, with_file_line )\ LOG( reason, conseq, manster::logger::sys, sev_lvl, with_file_line ) //! System type logger with fatal severity level used. #define LOG_SYS_FATAL( reason, conseq, with_file_line )\ LOG_SYS( reason, conseq, severity_level::fatal, with_file_line ) //! System type logger with error severity level used. #define LOG_SYS_ERR( reason, conseq, with_file_line )\ LOG_SYS( reason, conseq, severity_level::error, with_file_line ) //! System type logger with warning severity level used. #define LOG_SYS_WARN( reason, conseq, with_file_line )\ LOG_SYS( reason, conseq, severity_level::warning, with_file_line ) //! System type logger with information severity level used. #define LOG_SYS_INFO( reason, conseq, with_file_line )\ LOG_SYS( reason, conseq, severity_level::information, with_file_line ) /////////////////////////////////////////////////////////////// // CUSTOM LOGGING //! System type logger with error severity level and prede used. #define LOG_MEM_ALLOC_ERR( conseq, with_file_line )\ LOG_SYS_ERR( stl_tchs( _T( "Memory allocation failed" ) ), conseq, with_file_line ) -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Saturday 20 October 2012 08:35:30 syvyi wrote:
I need to be UNICODE friendly. What is analog to BOOST_LOG for UNICODE?
BOOST_LOG can be used for both narrow and wide character logging. The logger type defines the character type.

The code I inserted above is the code which worked without adding loggers handling wchar_t. When I add manster::logger::instance( )->initialize( ); std::basic_string< TCHAR> consequence = _T( "FormatMessage failure reason is not extracted" );\ LOG_MEM_ALLOC_ERR( consequence, true ); to test added code debugger gets void log( logger_type const & lg_type, std::wstring const & logged_data, severity_level const & lvl ) { if ( initialized ) BOOST_LOG_SEV( wloggers_map[ lg_type ], lvl ) << logged_data.data( ); } , however I do not see created folder log with file inside it. -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Saturday 20 October 2012 09:11:45 syvyi wrote:
The code I inserted above is the code which worked without adding loggers handling wchar_t.
When I add
manster::logger::instance( )->initialize( ); std::basic_string< TCHAR> consequence = _T( "FormatMessage failure reason is not extracted" );\ LOG_MEM_ALLOC_ERR( consequence, true );
to test added code debugger gets
void log( logger_type const & lg_type, std::wstring const & logged_data, severity_level const & lvl ) { if ( initialized ) BOOST_LOG_SEV( wloggers_map[ lg_type ], lvl ) << logged_data.data( ); } , however I do not see created folder log with file inside it.
Probably, because you didn't initialize the wide character part of the library? Logging library components for different character types are not related in the current trunk.

O.K., but you said BOOST_LOG support wide character type. How to initialize boost.log? -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

class manster_wlogger_mt : public src::basic_composite_logger< wchar_t, manster_wlogger_mt, src::multi_thread_model< boost::shared_mutex >, src::features< src::severity< severity_level >, src::channel< std::wstring > > > { BOOST_LOG_FORWARD_LOGGER_CONSTRUCTORS( manster_wlogger_mt ) }; If you mean code above under initialization, then this is done. -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Saturday 20 October 2012 10:02:49 syvyi wrote:
class manster_wlogger_mt : public src::basic_composite_logger< wchar_t, manster_wlogger_mt, src::multi_thread_model< boost::shared_mutex >, src::features< src::severity< severity_level >, src::channel< std::wstring >
{ BOOST_LOG_FORWARD_LOGGER_CONSTRUCTORS( manster_wlogger_mt ) };
If you mean code above under initialization, then this is done.
No, I mean sinks, filters and formatters. You have to choose what character type you want to use in your application and setup the library for that character type.

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:%S.%f" ) % fmt::attr< severity_level >( "Severity", std::nothrow ) % fmt::named_scope( "Scope", keywords::iteration = fmt::reverse, keywords::delimiter = " | " ) % fmt::message( ) ); That is how I do this. That what my question was about. How to use boost.log to have both char and wchar_t for an output to in file backend? -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Saturday 20 October 2012 10:15:23 syvyi wrote:
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:%S.%f" ) % fmt::attr< severity_level >( "Severity", std::nothrow ) % fmt::named_scope( "Scope", keywords::iteration = fmt::reverse, keywords::delimiter = " | " ) % fmt::message( ) );
That is how I do this. That what my question was about. How to use boost.log to have both char and wchar_t for an output to in file backend?
With the current trunk you can't use both, the library will act as if there are two different libraries for two different character types. This changed in bleeding-edge, but I don't recommend using it now. Initialization of the wide-character logging is similar to the narrow- character one. You just have to use wide-character string for attribute names and format strings and use w-prefixed typedefs for sinks, core and loggers (like wtext_file_backend instead of text_file_backend).

When is it expected to announce a new version with both char and wchar_t dynamical detection? -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-log-set-channel-name-dynamically-fo... Sent from the Boost - Dev mailing list archive at Nabble.com.
participants (3)
-
Andrey Semashev
-
Klaim - Joël Lamotte
-
syvyi