[bind10-dev] are static Logger objects safe?

JINMEI Tatuya / 神明達哉 jinmei at isc.org
Fri Jun 10 18:47:37 UTC 2011


At Fri, 10 Jun 2011 15:47:29 +0100,
Stephen Morris <stephen at isc.org> wrote:

> The loggers are built on the "pimpl" idiom.  All the declaration does is
> store the name of the logger within the object - there are no
> dependencies on other objects.
> 
> The implementation class is instantiated using the stored name the first
> time a method within the logger is called (which should be when
> initialization has finished and the program is running).

I'm not so confident about that.  The following indirectly relies on a
std::string object:

isc::log::Logger logger("some_module");

so, if a particular std::string implementation depends on some another
static object, a strange thing will happen.

We should (in general) also worry about the other direction of
dependency, i.e. dependency *to* the static Logger object.  What if
(though admittedly as a result of a bad practice) an application does
this?

namespace {
class SomeInitializer {
public:
    SomeInitializer() {
        LOG_DEBUG(logger, DBG_TRACE_BASIC, SOMETHING_INITIALIZED);
    }
};

SomeInitializer init;
}

As you know this type of fiasco can be avoided relatively easily via a
proxy factory function:

isc::log::Logger&
getLogger() {
  static isc::log::Logger logger("some_module");
  return (logger);
}

SomeInitializer::SomeInitializer() {
    LOG_DEBUG(getLogger(), DBG_TRACE_BASIC, SOMETHING_INITIALIZED);
}

This approach has its own drawbacks such as being less intuitive or
slightly higher cost due to the call overhead.  But I personally
believe it's worth considering until the overhead is proven to be a
significant performance bottleneck.

---
JINMEI, Tatuya



More information about the bind10-dev mailing list