Boost logo

Boost :

From: Andrey Semashev (andysem_at_[hidden])
Date: 2007-05-21 12:27:56


Hello Ruediger,

Sunday, May 20, 2007, 11:21:30 PM, you wrote:

> Dear Andrey, dear all,

> I was wondering whether it would make sense to extend the log library to an
> exception framework. This, I believe, could be done with minimal effort.

> In my opinion every exception should lead to a log message, as an exception
> will usually represent the most extreme condition available in an
> application.

Although I don't quite agree with part "_every_ exception should lead
to a log message", I think exception loggability is a good thing.

> One way of raising an exception is to stream the error messages to a sink,
> in the following way:

> int errorCode = 3;
> myExceptionType exc;
> exc << "An error occured: " << errorCode << raiseException;

[snip]

I'm not sure I understood your suggestion correctly, are you saying
there's a logger inside myExceptionType that collects the error
message? What message is available in the thrown exception object
then?

You see, logging record may not make it to the log and, actually, may
not even get formatted, while the exception, obviously, has to have a
valid error description inside anyway. It's easier to compose the
exception first and then put it to log. I see it as a convenient macro
like this:

#define BOOST_LOG_THROW(logger, svty, exception_type, strm)\
  if (true) {\
    exception_type e;\
    wrap(e) << strm;\
    BOOST_LOG_SEV(logger, svty)\
      << "Exception occurred at "\
      << __FILE__ << ':' << __LINE__\
      << ". " << e;\
    throw e;\
  } else ((void)0)

BOOST_LOG_THROW(lg, minor,
  std::runtime_error, "CPU not found");

BOOST_LOG_THROW(lg, major,
  std::invalid_argument, "The argument X has invalid value " << X);

BOOST_LOG_THROW(lg, fatal,
  boost::exception, "I'm dying, here's my dump: " << dump);

(I'm not familiar with Boost.Exception, does it have an ability of such
message formatting?)

While this looks a quite nice addition to the library, I'm afraid
final users will most likely want to extend or modify the
BOOST_LOG_THROW above to suit better their needs (i.e. to add a
specific tag to such messages or to put them into a separate channel),
and I don't see the way to do it. Although I'll add it to the Wiki
page since it fits well for simple use cases. Thanks for the
suggestion!
  

-- 
Best regards,
 Andrey                            mailto:andysem_at_[hidden]

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