C++ day07 log4cpp的实际使用习题(有答案)

分为三部分
1)头文件
2)函数具体实现文件
3)测试文件

1)头文件
MyLogger.h

#ifndef __MY_LPGGER_H__
#define __MY_LPGGER_H__
#include <log4cpp/Category.hh>

using namespace log4cpp;

class Mylogger
{
public:
	//单例类创建函数
    static Mylogger *getInstance();
    //释放单例类
    static void destroy();
	
	//警告
    void warn(const char * msg);
    //错误
    void error(const char * msg);
    //检测
    void debug(const char * msg);
    //通知
    void info(const char * msg);

private:
	//构造函数
    Mylogger();
    //析构函数
    ~Mylogger();
private:
    static Mylogger *_pInstance;
    //category,定义错误等级
    Category &_mycat;

};

//宏定义,用于绑定输出错误日志的函数及错误行号
#define prefix(msg) (string(__FILE__) + string(" ") + string(__FUNCTION__) \
        + string(" ") + string(std::to_string(__LINE__)) + string(" ") \
        + msg).c_str()

//定义具体错误类型
#define LogError(msg)  Mylogger::getInstance()->error(prefix(msg))
#define LogInfo(msg)  Mylogger::getInstance()->info(prefix(msg))
#define LogWarn(msg)  Mylogger::getInstance()->warn(prefix(msg))
#define LogDebug(msg)  Mylogger::getInstance()->debug(prefix(msg))

#endif

2)函数具体实现文件
MyLogger.cc

#include "MyLogger.h"
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/Priority.hh>
#include <iostream>

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

Mylogger *Mylogger::_pInstance = nullptr;

Mylogger *Mylogger::getInstance()
{
    if(nullptr == _pInstance)
    {
        _pInstance = new Mylogger();
    }

    return _pInstance;
}

void Mylogger::destroy()
{
    if(_pInstance)
    {
        delete _pInstance;
        _pInstance = nullptr;
    }
}

void Mylogger::warn(const char * msg)
{
    _mycat.warn(msg);
}

void Mylogger::error(const char * msg)
{
    _mycat.error(msg);
}

void Mylogger::debug(const char * msg)
{
    _mycat.debug(msg);
}

void Mylogger::info(const char * msg)
{
    _mycat.info(msg);
}

Mylogger::Mylogger()
: _mycat(Category::getRoot().getInstance("Mycat"))
{
    //日志的格式
    PatternLayout *ppl1 = new PatternLayout();
    ppl1->setConversionPattern("%d %c [%p] %m %n");

    PatternLayout *ppl2 = new PatternLayout();
    ppl2->setConversionPattern("%d %c [%p] %m %n");

    //日志的目的地
    OstreamAppender *poa = new OstreamAppender("OstreamAppender123", &cout); //输出到终端
    poa->setLayout(ppl1);
	//输出到日志文件
    FileAppender *pfa = new FileAppender("FileAppender123", "wd.log");
    pfa->setLayout(ppl2);

    _mycat.addAppender(poa);
    _mycat.addAppender(pfa);
    //DEBUG及以上危险等级的日志都要输出
    _mycat.setPriority(Priority::DEBUG);
}

//析构函数就是执行shutdown()文件,整体退出
Mylogger::~Mylogger()
{
    Category::shutdown();
}

3)测试文件
testMyLogger.cc

#include "MyLogger.h"
#include <iostream>
#include <string>

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

//测试用,在实际中不用
string func(const char *msg)
{
    string s1 = string(__FILE__) + string(" ") + string(__FUNCTION__) 
        + string(" ") + string(std::to_string(__LINE__)) + string(" ") 
        +msg;
    return s1;
}

//实际使用日志返利
void test()
{
    /* Mylogger *ps1 = Mylogger::getInstance(); */
    /* ps1->error("hshsh"); */
    /* Mylogger::getInstance()->error("hahha"); */
    /* Mylogger::getInstance()->error(func("hahha").c_str()); */
    /* Mylogger::getInstance()->error(prefix("helloworld")); */
    /* Mylogger::getInstance()->warn(prefix("helloworld")); */
    /* Mylogger::getInstance()->info(prefix("helloworld")); */
    /* Mylogger::getInstance()->debug(prefix("helloworld")); */
    LogError("wuhan");
    const char *pstr = "hello";
    int number = 1;
    int value = 2;
    //可变参数输出
    LogError("hello, %d, %s %d\n", number, pstr, value);

}

//test2在实际中不用
void test2()
{
    cout << __FILE__  << "   " << __FUNCTION__ << "  " << __LINE__ << endl;
}
int main(int argc, char **argv)
{
    test();
    return 0;
}

输出效果如下
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值