Qt中第三方日志库QsLog的基本配置和使用详解

上一篇文章Qt第三方日志库QsLog基本语法介绍介绍了一下日志库QsLog的基本语法,本文将介绍一下QsLog的基本配置和使用。配合上文一起食用效果更佳哟~

一、QsLog基本介绍

qslog的下载地址:https://github.com/victronenergy/QsLog

QsLog是一个基于Qt的QDebug类的易于使用的记录器。QsLog是在麻省理工学院许可下以开源形式发布的。

QsLog的特征:

  • 六个日志级别(从跟踪到致命);
  • 运行时可配置的日志级别阈值;
  • 关闭日志记录时的最小开销;
  • 支持多个目标,附带文件和调试目标;
  • 线程安全
  • 支持现成的常见Qt类型的日志记录;
  • 小依赖:直接把它放到你的项目中。

二、QsLog的使用方法

2.1 方法一——在Mingw编译器中的编译和使用

打开QsLog.pri项目,进行编译:
在这里插入图片描述
编译完成后,在工程源码目录中,会出现build-QsLogShared文件夹。
在这里插入图片描述
进入该文件夹,将libQsLog2.a和QsLog2.dll复制出来,移动到新建项目的文件夹中。
在这里插入图片描述
同时将QsLog-master中的头文件也移出来,放入到新建项目相应的文件夹中。
在这里插入图片描述
内容如下:
在这里插入图片描述
其中bin目录下存放libQsLog2.a和QsLog2.dll。include目录下存放相应的.h头文件。

然后将dll文件放在exe所在目录下。
在这里插入图片描述

2.2 方法二——在Visual Studio编译器中的使用

编译步骤相同,只是利用Visual Studio进行编译,生成的是QsLog2.dll和QsLog2.lib(而mingw编译器生成的是Qslog2.dll和QsLog2.a)。

三、项目基本配置

新建一个Qt案例,项目名称为“qslogTest”,基类选择“QWidget”,取消选中创建UI界面复选框,完成项目创建。

在pro文件中添加相应的头文件和lib库文件:

INCLUDEPATH += include/QsLog.h \
               include/QsLogDest.h \
               include/QsLogDestConsole.h \
               include/QsLogDestFile.h \
               include/QsLogDestFunctor.h \
               include/QsLogDisableForThisFile.h \
               include/QsLogLevel.h


LIBS += $$PWD/QsLog/bin/libQsLog2.a

四、UI界面设计

ui界面如下:
在这里插入图片描述
界面内为一个QTextBrowser。

五、主程序实现

5.1 widget.h

头文件中声明两个函数和一个槽函数:

public:
    void initLogger();
    void destroyLogger();

public slots:
    void logSlot(const QString &message, int level);

5.2 widget.cpp

首先定义相应的头文件和使用命名空间:

#include"QsLog/include/QsLog.h"
#include"QsLog/include/QsLogDest.h"
using namespace QsLogging;

在构造函数中进行初始化,析构函数中进行销毁:

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    initLogger();   //初始化日志

}

Widget::~Widget()
{
    delete ui;
    destroyLogger();//销毁日志
}

初始化函数:

void Widget::initLogger()
{
    // 1. 启动日志记录机制
    Logger& logger = Logger::instance();
    logger.setLoggingLevel(QsLogging::TraceLevel);
    //设置log位置为exe所在目录
    const QString sLogPath(QDir(QCoreApplication::applicationDirPath()).filePath("log.txt"));

    // 2. 添加两个destination
    DestinationPtr fileDestination(DestinationFactory::MakeFileDestination(
      sLogPath, EnableLogRotation, MaxSizeBytes(512), MaxOldLogCount(2)));
    DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination());
    //DestinationPtr functorDestination(DestinationFactory::MakeFunctorDestination(&logFunction));

    //这样和槽函数连接
    DestinationPtr sigsSlotDestination(DestinationFactory::MakeFunctorDestination(this, SLOT(logSlot(QString,int))));

    logger.addDestination(debugDestination);
    logger.addDestination(fileDestination);
    //logger.addDestination(functorDestination);
    logger.addDestination(sigsSlotDestination);

    // 3. 开始日志记录
    QLOG_INFO() << "Program started";
    QLOG_INFO() << "Built with Qt" << QT_VERSION_STR << "running on" << qVersion();

    QLOG_TRACE() << "Here's a" << QString::fromUtf8("trace") << "message";
    QLOG_DEBUG() << "Here's a" << static_cast<int>(QsLogging::DebugLevel) << "message";
    QLOG_WARN()  << "Uh-oh!";
    qDebug() << "This message won't be picked up by the logger";
    QLOG_ERROR() << "An error has occurred";
    qWarning() << "Neither will this one";
    QLOG_FATAL() << "Fatal error!";
}

析构函数:

//析构
void Widget::destroyLogger()
{
    QsLogging::Logger::destroyInstance();
}

槽函数:

//槽函数
void Widget::logSlot(const QString &message, int level)
{
    ui->textBrowser->append(qPrintable(message));
}

【注】:如果发现日志显示中文时会显示乱码,则只需将qPrintable修改为qUtf8Printable即可。

六、效果演示

完整效果如下:
在这里插入图片描述

七、拓展

如果想设置颜色,比如说将FATAL设置为红色,可以修改代码如下:

void Widget::logSlot(const QString &message, int level)
{
	if(message.contains("FATAL")){
		const QString ss="<font color=\"#FF0000\">";
		ui->textBrowser->append(ss + qPrintable(message) + "</font> ");//显示红色的字体
    }else{
        ui->textBrowser->append(qPrintable(message));
    }
}

运行效果如下:
在这里插入图片描述

如果没有看懂的话,完整代码可以参考:
https://download.csdn.net/download/didi_ya/85019199


ok,以上便是本文的全部内容了,如果对你有所帮助,记得点个赞哟~

参考文章:

  1. https://blog.csdn.net/hp_cpp/article/details/83580525
Qt使用第三方静态的步骤如下: 1. 打开Qt Creator,新建一个项目,并选择类型的C++。 2. 在项目路径下复制之前的文件(.a或.lib)和头文件(.h)。 3. 在Qt Creator,右键点击项目,选择"Add Existing Directory",将文件和头文件都添加到项目。 4. 双击打开项目的.pro文件,在其添加以下代码: ``` QMAKE_CXXFLAGS += -msse2 -mssse3 -msse4.1 QMAKE_CXXFLAGS += -mavx2 -m32 -mpclmul -msha -maes ``` 这些代码是为了设置编译器的标志,以支持特定的指令集。 5. 在需要使用的地方,包含的头文件,并使用QLibrary类加载文件。然后,使用resolve()函数获取的函数指针,并进行调用。 以上是在Qt使用第三方静态基本步骤。请根据你的具体情况进行相应的调整和配置。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [Qt编译第三方源代码为静态使用](https://blog.csdn.net/qq_38769149/article/details/102600406)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Qt 调用第三方](https://blog.csdn.net/glblove1986/article/details/101269998)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wendy_ya

您的鼓励将是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值