logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Josh Clark" <idispa...@gmail.com>
Subject Re: Abstraction and static
Date Wed, 22 Feb 2006 15:43:29 GMT
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 <string>

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->LogMessageWarn(message, "", SVLOG_LOCATION); }}
#define SVLOG_ERROR(logger, message) { if (logger->isErrorEnabled()) {
logger->LogMessageError(message, "", SVLOG_LOCATION); }}
#define SVLOG_FATAL(logger, message) { if (logger->isFatalEnabled()) {
logger->LogMessageFatal(message, "", SVLOG_LOCATION); }}

#define SVLOG_DEBUG1(logger, message, p1) { if (logger->isDebugEnabled()) {
logger->LogMessageDebug(message, p1, SVLOG_LOCATION); }}
#define SVLOG_INFO1(logger, message, p1)  { if (logger->isInfoEnabled())  {
logger->LogMessageInfo(message, p1, SVLOG_LOCATION); }}
#define SVLOG_WARN1(logger, message, p1)  { if (logger->isWarnEnabled())  {
logger->LogMessageWarn(message, p1, SVLOG_LOCATION); }}
#define SVLOG_ERROR1(logger, message, p1) { if (logger->isErrorEnabled()) {
logger->LogMessageError(message, p1, SVLOG_LOCATION); }}
#define SVLOG_FATAL1(logger, message, p1) { if (logger->isFatalEnabled()) {
logger->LogMessageFatal(message, p1, SVLOG_LOCATION); }}


//
// Simple version. Assumes SVLOG_DEFAULT_LOGGER is defined.
//
#define SVLOG_SIMPLE_DEBUG(message) SVLOG_DEBUG(
GetLogger(SVLOG_DEFAULT_LOGGER), message )
#define SVLOG_SIMPLE_INFO(message)  SVLOG_INFO(
GetLogger(SVLOG_DEFAULT_LOGGER), message )
#define SVLOG_SIMPLE_WARN(message)  SVLOG_WARN(
GetLogger(SVLOG_DEFAULT_LOGGER), message )
#define SVLOG_SIMPLE_ERROR(message) SVLOG_ERROR(
GetLogger(SVLOG_DEFAULT_LOGGER), message )
#define SVLOG_SIMPLE_FATAL(message) SVLOG_FATAL(
GetLogger(SVLOG_DEFAULT_LOGGER), message )

#define SVLOG_SIMPLE_DEBUG1(message, p1) SVLOG_DEBUG1(
GetLogger(SVLOG_DEFAULT_LOGGER), message, p1 )
#define SVLOG_SIMPLE_INFO1(message, p1)  SVLOG_INFO1(
GetLogger(SVLOG_DEFAULT_LOGGER), message, p1 )
#define SVLOG_SIMPLE_WARN1(message, p1)  SVLOG_WARN1(
GetLogger(SVLOG_DEFAULT_LOGGER), message, p1 )
#define SVLOG_SIMPLE_ERROR1(message, p1) SVLOG_ERROR1(
GetLogger(SVLOG_DEFAULT_LOGGER), message, p1 )
#define SVLOG_SIMPLE_FATAL1(message, p1) SVLOG_FATAL1(
GetLogger(SVLOG_DEFAULT_LOGGER), message, p1 )

Mime
View raw message