C++封装log4cpp

封装__FILE__时,没有考虑所处函数行数,所以有需要可自行修改封装该函数。void add(string &s)

#include <log4cpp/BasicLayout.hh>
#include <log4cpp/SimpleLayout.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/Priority.hh>
#include <iostream>
#include <stdio.h>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/SimpleLayout.hh>
#include <sstream>
#include <string>

using namespace std;
using namespace log4cpp;


enum setLogGrade
{
    ERROR,
    WARN,
    INFO,
    DEBUG,
};
class Mylogger
{
public:
    static Mylogger *getInstance()
    {
        if (nullptr == m_ptr)
        {
            m_ptr = new Mylogger();
        }
        return m_ptr;
    }
    void ClassSet(setLogGrade Greade);
    void warn(const char *msg);
    void error(const char *msg);
    void debug(const char *msg);
    void info(const char *msg);
    //------------------
    //类成员函数可变参模板
    void myfunct2()
    {
        string temp;
        add(temp);
        this->temp+=temp;
        this->root.warn(this->temp);
        std::cout << "终止调用" << std::endl;
    }
    template <typename T, typename... U>
    void myfunct2(const T &firstarg, const U &...otherargs)
    {
        //判断是否是string类型,如果不是,就转换一下
        if (is_class<string>::value || is_class<const char *>::value)
        {
            //如果是string
            cout << "string 类型" << endl;
            this->temp.append(firstarg);
            this->temp.append("---");
        }
        std::cout << "收到的参数值是:" << firstarg << std::endl;
        cout << "temp " << this->temp << endl;
        myfunct2(otherargs...);

    }
    //-------------------

private:
    Mylogger();
    ~Mylogger()
    {
        if (m_ptr)
        {
            cout << "释放资源" << endl;
            root.shutdown();
        }
    }
    static Mylogger *m_ptr;

private:
    //......
    PatternLayout *ppll;
    PatternLayout *ppl2;
    OstreamAppender *pos; //目的地
    FileAppender *file;   //目的地文件类型
    Category &root;       //日志种类定义时,必须初始化
    string temp;
};
inline string IntString(int li)
{
    ostringstream oss;
    oss << li;
    return oss.str(); //返回转成字符串
}
void add(string &s)
{
    s.append("--").append(":").append(__FILE__).append("--:").append(__func__).append("---").append(IntString(__LINE__)).append("---");
    
    
}
void Mylogger::ClassSet(setLogGrade msg)
{
    //该函数设置优先级
    switch (msg)
    {
    case (ERROR):
        //this->root.setPriority(setLogGrade::ERROR);
        this->root.setPriority(Priority::ERROR);
        break;
    case (WARN):
        this->root.setPriority(Priority::WARN);
        break;
    case (INFO):
        this->root.setPriority(Priority::INFO);
        break;
    case (DEBUG):
        //this->root.setPriority(setLogGrade::DEBUG);
        this->root.setPriority(Priority::DEBUG);
        break;
    }
}
Mylogger::Mylogger()
    : ppll(nullptr), ppl2(nullptr), pos(nullptr), file(nullptr), root(Category::getRoot().getInstance("myCat"))
{
    this->ppll = new PatternLayout();
    this->ppll->setConversionPattern("%d %c [%p] %m %n");

    this->ppl2 = new PatternLayout();
    this->ppl2->setConversionPattern("%d %c [%p] %m %n");
    //初始化日志格式

    this->pos = new OstreamAppender("123", &cout);
    this->pos->setLayout(ppll);

    this->file = new FileAppender("file123", "my.log");
    this->file->setLayout(this->ppl2);

    root.addAppender(this->pos);
    root.addAppender(this->file);
    //root.setPriority(Priority::ERROR);//设置过滤等级
}
void Mylogger::warn(const char *msg)
{
    //myfunct2("1111","22222");
}
void Mylogger::info(const char *msg)
{
    string temp;
    add(temp);
    temp += msg;

    this->root.info(temp);
}
void Mylogger::error(const char *msg)
{
    //调用add函数,连接行号,函数名字符串,文件名
    string temp;
    add(temp);
    temp += msg;

    this->root.error(temp);
}
Mylogger *Mylogger::m_ptr = nullptr;
int main()
{
    Mylogger *log = Mylogger::getInstance();
    log->ClassSet(WARN);
    log->error("error");
    //log->warn("aaa");
    log->myfunct2("aaa","bbbb","cccx","dasfdasfdas","dasfadsfdafdaga");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值