logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: Logging in templates?
Date Sun, 30 Jan 2005 20:05:40 GMT
I don't think the problem is unsurmountable, but I haven't tried it 
myself.  I'm just freehanding the following code, but maybe it will 
help you get started and you can post back to the list what works for 
you.

I think you'd so something like:

template <class T> class Example {
       Example() {}
       void doSomething() {
             LOG4CXX_DEBUG(getLogger(), "Hello, World");
       }

private:
        log4cxx::LoggerRef& getLogger() {
             static log4cxx::LoggerRef logger(log4cxx::Logger::getLogger(
                 LOG4CXX_LOCATION.getClassName()));
             return logger;
       }
};

The code fragment depends on the LocationInfo class introduced after 
0.9.7 to get the class name.  The class name may be returned as "" for 
early Microsoft compilers ( _MSC_VER < 1300).  Please feel free to 
enhance include/log4cxx/spi/location/locationinfo.h if your compiler 
can return class location but the macros don't handle it.


On Jan 30, 2005, at 10:51 AM, Daniel Einspanjer wrote:

> Does no one else have a problem with needing to log messages from
> templated classes?  If anyone else has solved this problem, I would
> very much like to hear your solution.
>
> Thanks,
> Daniel
>
>
> On Thu, 20 Jan 2005 15:51:41 -0500, Daniel Einspanjer
> <deinspanjer@gmail.com> wrote:
>> Could anyone share their methods for logging within a templated class?
>>
>> Specifically, the problem I don't have a good solution for is the
>> static instantiation of a logger object at all, let alone one that has
>> a name giving the particular class for which the template is being
>> used.
>


Mime
View raw message