[log] How to derive from a logger

Hi Andrey As discussed earlier, we're kind of desperate to add functionality to a logger class provided by Boost.Log. Instead of duplicating severity_channel_logger_mt, I'm wondering whether it's a good idea to derive from it, as follows: class Logger : public ::boost::log::sources::severity_channel_logger_mt< Severity > { public: typedef ::boost::log::sources::severity_channel_logger_mt< Severity
logger_base; BOOST_LOG_FORWARD_LOGGER_CONSTRUCTORS(Tracer)
void SetMinimumSeverity(Severity severity); Severity GetMinimumSeverity(); private: Severity minimumSeverity; }; Elsewhere we'd use this class as follows: BOOST_LOG_DECLARE_GLOBAL_LOGGER_INIT(Whatever, Logger) { // ... } It seems to work, but is this a good idea? Thanks & Regards, -- Andreas Huber When replying by private email, please remove the words spam and trap from the address shown in the header.

I guess I can answer my own question now: It's probably not a good idea, because of this function in the base class FinalT& operator= (FinalT that) { base_type::swap_unlocked(that); return static_cast< FinalT& >(*this); } FinalT would in this case not point to the most-derived type, making this a very strange operator=. My question now is: How do I derive my own logger that has its own data member(s) without having to duplicate ctors, etc. So far I've come up with this ... class Logger : public ::boost::log::sources::basic_composite_logger< char, Logger, ::boost::log::sources::multi_thread_model< ::boost::log::aux::light_rw_mutex >, typename ::boost::log::sources::features< ::boost::log::sources::severity< Severity >, ::boost::log::sources::channel< std::string > >::type > { public: BOOST_LOG_FORWARD_LOGGER_CONSTRUCTORS(Logger) void SetMinimumSeverity(Severity severity); Severity GetMinimumSeverity(); bool AllowRecord(Severity severity); private: volatile Severity minimumSeverity; }; ... but this of course leaves minimumSeverity uninitialized. I tried to implement default & copy ctor myself & use BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_FORWARD, but that left me with strange errors. Thanks, -- Andreas Huber When replying by private email, please remove the words spam and trap from the address shown in the header.

On 22.03.2010 19:45, Andreas Huber wrote:
I guess I can answer my own question now: It's probably not a good idea, because of this function in the base class
FinalT& operator= (FinalT that) { base_type::swap_unlocked(that); return static_cast< FinalT& >(*this); }
FinalT would in this case not point to the most-derived type, making this a very strange operator=.
Right. There are also other members that you'd have to override in order to bring your members into play with standard macros.
My question now is: How do I derive my own logger that has its own data member(s) without having to duplicate ctors, etc. So far I've come up with this ...
... but this of course leaves minimumSeverity uninitialized. I tried to implement default & copy ctor myself & use BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_FORWARD, but that left me with strange errors.
Composite loggers are not intended to be derived from. I wonder why you need to derive in the first place. It is much easier and safer to create a logger feature and inject it into the logger: template< typename BaseT > class MyFeature : public BaseT { public: typedef typename BaseT::record_type record_type; public: MyFeature() : minimumSeverity(Default) {} MyFeature(MyFeature const& that) : BaseT(static_cast< BaseT const& >(that)), minimumSeverity(that.minimumSeverity) { } template< typename ArgsT > MyFeature(ArgsT const& args) : BaseT(args) { SetMinimumSeverity(args[keywords::severity | Default]); } void SetMinimumSeverity(Severity severity); Severity GetMinimumSeverity(); protected: template< typename ArgsT > record_type open_record_unlocked(ArgsT const& args) { Severity sev = args[keywords::severity | Default]; if (sev >= minimumSeverity) return BaseT::open_record_unlocked(args); else return record_type(); } private: volatile Severity minimumSeverity; }; Then you can define a composite logger with it: class MyLogger : public basic_composite_logger< char, MyLogger, multi_thread_model< shared_mutex >, features< mpl::quote1< MyFeature > > > { BOOST_LOG_FORWARD_LOGGER_CONSTRUCTORS(MyLogger) };

Composite loggers are not intended to be derived from. I wonder why you need to derive in the first place.
Because I hadn't understood the features design :-).
It is much easier and safer to create a logger feature and inject it into the logger: [snip]
This solves the problem very nicely, thanks. BTW, congratulations for the acceptance of the library! Well deserved. Regards, -- Andreas Huber When replying by private email, please remove the words spam and trap from the address shown in the header.
participants (2)
-
Andreas Huber
-
Andrey Semashev