logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Assaf Lavie <assafla...@gmail.com>
Subject Re: Caching LoggerPtr using local statics (the singleton pattern)
Date Tue, 26 May 2009 14:54:16 GMT
Thanks, I'll try.
But wouldn't it also make sense to just initialize APR before anything else
in the application (e.g. before configuring the log)?
Because this business with default vs. one-param constructor is very easy to
forget and get wrong (since both compile fine and it will only crash at
runtime)...


On Tue, May 26, 2009 at 4:20 PM, Curt Arnold <carnold@apache.org> wrote:

> Try using the one parameter constructor for LoggerPtr, instead of the
> default constructor and then an assignment:
>
>   static LoggerPtr
>> singleton(log4cxx::LogManager::getLogger("some.logger"));
>>
>>
> There is a slight difference in order of events between the two.  Using the
> default constructor, it is
>
> 1. Construct singleton
> 2. Call LogManager::getLogger()
>    2a. initialize APR, etc
> 3. Assign logger to singleton
>
> With the one parameter it is
>
> 1 Call LogManager::getLogger()
>   1a initialize APR
> 3. Construct singleton
>
> Since destruction events occur in reverse order, using the one-parameter
> constructor results in APR being destroyed last.
>
>
>
> On May 26, 2009, at 3:37 AM, Assaf Lavie wrote:
>
>  My program always crashes when I try to cache the LoggerPtr returned from
>> getLogger in a local static variable (i.e. singleton) like this:
>>
>> LoggerPtr MyLogger()
>> {
>>  static LoggerPtr singleton =
>> log4cxx::LogManager::getLogger("some.logger");
>>  return singleton;
>> }
>>
>>
>> I assume this happens because of some APR related effect. Can someone
>> please shed light on this? Does this happen to you?
>>
>
>

Mime
View raw message