Linux log4cpp 使用单列模式封装

//使用单例模式
                class Mylog
                {
                public:
                    Mylog();
                    
                    ~Mylog();
                
                    void warn(const char * msg);
                    void error(const char * msg);
                    void debug(const char * msg);
                    void info(const char * msg);
                private:
                  //......
                };
                
                Mylog log;
                log.warn("hello");

/*!
 * \file Mylog.h
 * \date 2017/02/24 8:43
 *
 * \author henry
 * Contact: [email protected]
 *
 * \brief 
 *
 * TODO: long description
 *
 * \note
*/

# ifndef _MYLOG_H_
#define _MYLOG_H_ 
using 
#include 
#include 

using std::cout;
using std::endl;
using std::string;
using namespace log4cpp;

class Mylog
{
public:
    static Mylog * getInstance();
    static void destroy();
    void Warn(const string &msg);
    void Error(const string &msg);
    void Info(const string &msg);
    void Debug(const string &msg);

private:
    Mylog();
    ~Mylog();

private:
    static Mylog * _pInstance;
    Category & _cat;
};

inline string int2str(int num) {
    std::ostringstream oss;
    oss << num;
    return oss.str();
}

#define postfix(msg) \
    string(msg).append("[").append(__FILE__)\
                .append(":").append(__FUNCTION__)\
                .append(":").append(int2str(__LINE__)).append("]");

// warn
inline void logWarn(const string &msg) {
    Mylog * plog = Mylog::getInstance();
    plog->Warn(msg);

}
//Error
inline void logError(const string &msg) {
    Mylog * plog = Mylog::getInstance();
    plog->Error(msg);

}
//Info
inline void logInfo(const string &msg) {
    Mylog * plog = Mylog::getInstance();
    plog->Info(msg);

}
//Debug
inline void logDebug(const string &msg) {
    Mylog * plog = Mylog::getInstance();
    plog->Debug(msg);
}

#define logWarn(msg) logWarn(postfix(msg))
#define logError(msg) logError(postfix(msg))
#define logInfo(msg) logInfo(postfix(msg))
#define logDebug(msg) logDebug(postfix(msg))

#endif



#include "Mylog.h"
#include 
#include 
#include 
#include 

Mylog * Mylog::_pInstance = NULL;
Mylog *Mylog::getInstance() {
    if (_pInstance==NULL)
    {
        _pInstance = new Mylog;
    }
    else
    {
        return _pInstance;
    }
}

Mylog::Mylog()
    :_cat(Category::getRoot().getInstance("mycat")) {
    PatternLayout * ptn1 = new PatternLayout();
    ptn1->setConversionPattern("%d:%c %p %x: %m%n");
    PatternLayout *ptn2 = new PatternLayout();
    ptn2->setConversionPattern("%d:%c %p %x: %m%n");

    OstreamAppender * osAppender = new OstreamAppender("osAppender", &cout);
    osAppender->setLayout(ptn1);

    FileAppender *fileAppender = new FileAppender("fileAppender", "mylog.log");
    fileAppender->setLayout(ptn2);

    _cat.addAppender(osAppender);
    _cat.addAppender(fileAppender);
    _cat.setPriority(Priority::DEBUG);

}

Mylog::~Mylog(){

}


void Mylog::Debug(const string &msg) {
    _cat.Debug(msg.c_str());
}

void Mylog::Error(const string &msg) {
    _cat.Error(msg.c_str());
}
void Mylog::Warn(const string &msg) {
    _cat.Warn(msg.c_str());
}
void Mylog::Info(const string &msg) {
    _cat.Info(msg.c_str());
}


// testlog.cpp

#include "Mylog.h"
#include 
#include 
#include 

using std::cout;
using std::endl;
using std::string;

int main() {
#if 0
    Mylog *log = Mylog::getInstance();
    log->Warn(postfix("warn msg"));
    log->Error(postfix("error msg"));
    log->Info(postfix("info msg"));
    log->Debug(postfix("debug msg"));
#endif
#if 0
    logWarn(postfix("warn msg"));
    logError(postfix("error mso"));
    logInfo(postfix("info msg"));
    logDebug(postfix("debug msg"));
#endif
    LogWarn("warn msg");
    LogInfo("info msg");
    LogDebug("debug msg");
    LogError("error msg");

#if 0
    cout << __FILE__ << endl;
    cout << __FUNCTION__ << endl;
    cout << __LINE__ << endl;
#endif
    return 0;
}

你可能感兴趣的:(Linux log4cpp 使用单列模式封装)