Abstraction and static

2006-02-22 Thread John Reynolds








Hi all,

 

As a newbie to log4cxx please
forgive me if I am talking out of my arse and I am having difficulty in formulating
my thoughts this morning (need more caffine).

 

For most of my code I like to abstract third party libraries
(by wrapping such library functionality) from my code so I can switch such
libraries with ease. With log4cxx it seems I can not do this due to the logger
name being tied to my source code class by 

 

class Bar{ static log4cxx::LoggerPtr logger;

}

 

which is static
as getLogger requires a static.

 

Is there a way of creating an instance of my abstraction
logger class (an instance for each class) and pass in the name and call getLogger with that name.

 

I hope that this is clear enough to understand.

 

Many Thanks

 

John 








Re: Abstraction and static

2006-02-22 Thread Josh Clark
I did the same thing.  Here is the interface I made we use.  I'll leave the implementation as an exercise for the reader :)--
#pragma once
    Logging Framework
  --
    Logging - The simple way:
    SVLOG_SIMPLE_INFO("My Log Statement");
//    Logging - The slightly more complicated way:
    ILogger* pLogger = GetLogger("
My.Logger.Name");//    SVLOG_DEBUG( pLogger, "A debug log statement" );

//    Application setup:
    You need to call LoggingInitialize() at startup and 
//    LoggingDeInitialize() at shutdown.  LoggingDeInitialize() needs to//    be called before globals are destructed - otherwise you'll blow up.

//    Thread setup:
    At the start of each thread, before any logging is done, you need to set
//    the user id and site id.  This information is thread specific so you'll need
//    to set this for both the main thread and any threads you create.  If you forget,//    your logging statements will assert in debug mode.  The value of these does not
//    matter. They just need to be set.
//#ifdef SVLOG_EXPORTS
#define SVLOG_API __declspec(dllexport)#else
#define SVLOG_API __declspec(dllimport)#pragma comment(lib, "
svlog.lib")#endif
#include 
struct LogLocationInfo
{    LogLocationInfo( const char* szFile, const char* szFunc, int nLine )
        : lineNumber(nLine), fileName(szFile), methodName(szFunc) {}
    int lineNumber;    const char * fileName;
    const char * methodName;};
struct ILogger{
    virtual bool isDebugEnabled() = 0;
    virtual bool isInfoEnabled() = 0;    virtual bool isWarnEnabled() = 0;
    virtual bool isErrorEnabled() = 0;    virtual bool isFatalEnabled() = 0;
    // Single int parameter version
    virtual void LogMessageDebug(const std::string& message, int nParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageInfo(const std::string& message, int nParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageWarn(const std::string& message, int nParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageError(const std::string& message, int nParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageFatal(const std::string& message, int nParam, const LogLocationInfo& location) = 0;
    // Single string parameter version
    virtual void LogMessageDebug(const std::string& message, const char* szParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageInfo(const std::string& message, const char* szParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageWarn(const std::string& message, const char* szParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageError(const std::string& message, const char* szParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageFatal(const std::string& message, const char* szParamm, const LogLocationInfo& location) = 0;
    // Single double parameter version    virtual void LogMessageDebug(const std::string& message, double dParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageInfo(const std::string& message, double dParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageWarn(const std::string& message, double dParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageError(const std::string& message, double dParam, const LogLocationInfo& location) = 0;
    virtual void LogMessageFatal(const std::string& message, double dParam, const LogLocationInfo& location) = 0;
};
 The exported functions.
//SVLOG_API void LoggingInitialize(const char* szLoggingConfigFile);
SVLOG_API void LoggingDeInitialize();
SVLOG_API void LoggingThreadInitialize();SVLOG_API void LoggingThreadDeInitialize();
SVLOG_API ILogger* GetLogger(const char* szLoggerName);
SVLOG_API void LoggingSetContext(const char* szContext, const char* szValue);
SVLOG_API void LoggingSetUser(const char* szUserName);SVLOG_API void LoggingSetSiteId(long nSiteId);
//
// Helper class to handle thread initialization and deinitialization//
class CLoggingThreadInitializer{
public:    CLoggingThreadInitializer()  { LoggingThreadInitialize(); }
    ~CLoggingThreadInitializer() { LoggingThreadDeInitialize(); }
    CLoggingThreadInitializer(const char* szUserName, long nSiteId)
    {        LoggingThreadInitialize();
        LoggingSetUser(szUserName);        LoggingSetSiteId(nSiteId);
    }};
 Logging Macros. Only use the message parameter if that logging level is active.
//#define SVLOG_LOCATION LogLocationInfo(__FILE__, __FUNCSIG__, __LINE__)
#define SVLOG_DEBUG(logger, message) { if (logger->isDebugEnabled()) { logger->LogMessageDebug(message, "", SVLOG_LOCATION); }}
#define SVLOG_INFO(logger, message)  { if (logger->isInfoEnabled())  { logger->LogMessageInfo(message, "", SVLOG_LOCATION); }}
#define SVLOG_WARN(logger, message)  { if (logger->isWarnEnabled())  { logger->LogMessageWar