当前位置: 首页 > 工具软件 > Plog > 使用案例 >

开源PLog库的使用

袁和通
2023-12-01

plog下载地址https://github.com/SergiusTheBest/plog
假设有下面三个模块,欲使他们3个日志都写到同一个log.txt文件。A依赖B、C 模块

ModleA.exe
ModleB.dll
ModleC.dll

在ModleA中

#include “MoudlB.h”
#include “MoudlC.h”
#include "Log.h"
string logFile = "d:/log.txt";
//初始化log对象,设置当前日志级别为plog::debug,最大日志文件的大小为5M, 日志文件最多有3个进行循环 (分别为log.txt log.1.txt log.2.txt)
static plog::RollingFileAppender<plog::TxtFormatter> testFileAppender(logFile.c_str(), 1024 * 1024 * 5, 3);
plog::init(plog::debug, &testFileAppender)/*.addAppender(&alogAppender)*/; //addAppender 是为当前APender 附加一个新日志路径
ModleA::initPlog(plog::debug, &testFileAppender);
ModleB::initPlog(plog::debug, &testFileAppender);
ModleC::initPlog(plog::debug, &testFileAppender);

在B的 dll中,在ModleB.cpp中这样实现initPlog函数

#include "Log.h"
void ModleB::InitPlog(int severity, plog::IAppender* appender) {
    if (severity >= 0 && severity <= 6) {
        plog::init((plog::Severity)severity, appender);
    }
    else {
        plog::init(plog::Severity::verbose, appender);
    }
}

在C的 dll中,在ModleC.cpp中也可以这样实现initPlog

#include "Log.h"
void ModleC::InitPlog(int severity, plog::IAppender* appender) {
    if (severity >= 0 && severity <= 6) {
        plog::init((plog::Severity)severity, appender);
    }
    else {
        plog::init(plog::Severity::verbose, appender);
    }
}

然后不管在A,B,C中,只要调用LOGD<<”this is a log” ,都会打印日志到d:/log.txt中。

 类似资料: