Discussion:
[Boost-users] How to wrap boost::log class to user?
Bruce
2016-11-14 11:21:15 UTC
Permalink
Hi,
I am coding a project and I use boost::log.
I need to supply the log function to user, but I don't want the user use boost::log directly.
I want to wrap the boost::log into my class. But I still want to keep the << cascade feature.

Basically, I need to implement a MyLogger class, which support:

MyLogger(info) << bla bla bla;
MyLogger(trace) << bla bla bla;

and in the operator << function, I can use:
BOOST_LOG_SEV macro.

I don't know how to implement the operator << in MyLogger class.
Can anybody helps me, Thanks.
Leon Mlakar
2016-11-14 12:05:09 UTC
Permalink
Post by Bruce
Hi,
I am coding a project and I use boost::log.
I need to supply the log function to user, but I don't want the user
use boost::log directly.
I want to wrap the boost::log into my class. But I still want to keep
the << cascade feature.
MyLogger(info) << bla bla bla;
MyLogger(trace) << bla bla bla;
I'm not sure this is the best way to use Boost.Log. There are at least
two reasons why logging macros might be a better idea:
1. Using own operators you cannot implicitly enforce __LINE__,
__FILE__ and other compiler generated macros to supplement logged
information
2. Boost.Log will not create log record if no sink will accept it. By
using logging macros it is easy to ensure that logged information (bla
bla bla) is not evaluated if it's not going to be recorded; using own
operator overload the logging information will always be evaluated. And
in my experience logging information will quickly enough get non-trivial
and expensive to calculate. So in consequence logging will slow down you
code even in production use with logging thresholds raised to log errors
and warnings only.

Cheers,
Leon
GMX
2016-11-14 13:50:07 UTC
Permalink
  Originalnachricht  
Von: Leon Mlakar
Gesendet: Montag, 14. November 2016 13:05
An: boost-***@lists.boost.org
Antwort an: boost-***@lists.boost.org
Betreff: Re: [Boost-users] How to wrap boost::log class to user?
Post by Bruce
Hi,
I am coding a project and I use boost::log.
I need to supply the log function to user, but I don't want the user
use boost::log directly.
I want to wrap the boost::log into my class. But I still want to keep
the << cascade feature.
MyLogger(info) << bla bla bla;
MyLogger(trace) << bla bla bla;
I'm not sure this is the best way to use Boost.Log. There are at least
two reasons why logging macros might be a better idea:
1. Using own operators you cannot implicitly enforce __LINE__,
__FILE__ and other compiler generated macros to supplement logged
information
2. Boost.Log will not create log record if no sink will accept it. By
using logging macros it is easy to ensure that logged information (bla
bla bla) is not evaluated if it's not going to be recorded; using own
operator overload the logging information will always be evaluated. And
in my experience logging information will quickly enough get non-trivial
and expensive to calculate. So in consequence logging will slow down you
code even in production use with logging thresholds raised to log errors
and warnings only.

Cheers,
Leon

Loading...