Log to file in Qt.

本文介绍了一个使用Qt进行日志记录的方法。通过自定义消息处理器myMessageOutput,将不同类型的日志信息(如Debug、Warning、Critical和Fatal)写入到debug.log文件中。此方法有助于开发者更好地跟踪和调试应用程序。
#include <QtGui/QApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>


void myMessageOutput(QtMsgType type, const char *msg)
{

    QString txt;
    switch (type)
    {
    case QtDebugMsg:
        txt = QString("Debug: %1").arg(msg);
        break;
    case QtWarningMsg:
        txt = QString("Warning: %1").arg(msg);
        break;
    case QtCriticalMsg:
        txt = QString("Critical: %1").arg(msg);
        break;
    case QtFatalMsg:
        txt = QString("Fatal: %1").arg(msg);
        abort();
    default :
        txt = QString("App: %1").arg(msg);
        break;
    }

    QFile outFile("debug.log");
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);

    QTextStream ts(&outFile);
    ts << txt << endl;
    outFile.close();
}


int main(int argc, char *argv[])
{
    qInstallMsgHandler(myMessageOutput);
    QApplication a(argc, argv);


    qDebug() << "->main start.";


    return a.exec();
}
在 Windows 环境下使用 spdlog 日志库并将其集成到 Qt 项目中是一个常见需求,尤其在需要轻量级、高性能日志记录的 C++ 项目中。以下是详细的配置和使用方法。 ### 安装 spdlog spdlog 是一个基于 C++11 的头文件库,因此安装过程相对简单。可以使用 vcpkg 或直接通过 Git 获取源码并集成到项目中。 #### 方法 1:使用 vcpkg 安装(推荐) ```bash vcpkg install spdlog ``` 安装完成后,在 CMakeLists.txt 中添加: ```cmake find_package(spdlog REQUIRED) target_link_libraries(your_target PRIVATE spdlog::spdlog_header_only) ``` #### 方法 2:手动集成源码 从 GitHub 获取 spdlog 源码: ```bash git clone https://github.com/gabime/spdlog.git cd spdlog ``` 将 `include/spdlog` 文件夹复制到你的项目目录中,例如 `third_party/spdlog/include/spdlog`。 ### 配置 Qt 项目 假设你使用 CMake 管理 Qt 项目,以下是 CMakeLists.txt 的配置示例: ```cmake cmake_minimum_required(VERSION 3.14) project(MyQtApp) set(CMAKE_CXX_STANDARD 17) find_package(Qt5 COMPONENTS Widgets REQUIRED) # 设置 spdlog 头文件路径 include_directories(${PROJECT_SOURCE_DIR}/third_party/spdlog/include) add_executable(MyQtApp main.cpp mainwindow.cpp mainwindow.h) target_link_libraries(MyQtApp PRIVATE Qt5::Widgets) ``` ### 使用 spdlogQt 项目中 在 C++ 源文件中可以直接使用 spdlog 提供的日志接口。例如: ```cpp #include <spdlog/spdlog.h> #include <spdlog/sinks/basic_file_sink.h> void log_example() { // 控制台日志 spdlog::info("Welcome to spdlog!"); spdlog::error("Some error message"); // 文件日志 auto file_logger = spdlog::basic_logger_mt("file_logger", "logs/basic.txt"); file_logger->info("Logged to file"); } ``` ### 配置 UTF-8 支持(适用于 Visual Studio 2019) 为了确保日志输出的中文字符正常显示,需配置编译器使用 UTF-8 编码: ```cmake add_compile_options("$<$<C_COMPILER_ID:MSVC>:/source-charset:utf-8>") add_compile_options("$<$<C_COMPILER_ID:MSVC>:/execution-charset:utf-8>") ``` 此外,在 Visual Studio 中安装插件 **Force UTF-8(No BOM)**,确保源文件保存为 UTF-8 编码格式[^1]。 ### 示例:封装 spdlogQt 项目中的日志模块 可以将 spdlog 封装为一个独立的日志管理类,便于统一管理和使用: ```cpp // logger.h #pragma once #include <spdlog/spdlog.h> #include <memory> class Logger { public: static Logger& instance() { static Logger instance; return instance; } template<typename... Args> void info(const std::string& msg, Args... args) { m_logger->info(msg, args...); } template<typename... Args> void error(const std::string& msg, Args... args) { m_logger->error(msg, args...); } private: Logger() { m_logger = spdlog::basic_logger_mt("qt_app_logger", "logs/app.log"); } std::shared_ptr<spdlog::logger> m_logger; }; ``` 在项目中使用封装后的日志类: ```cpp #include "logger.h" void some_function() { Logger::instance().info("This is an info message"); Logger::instance().error("An error occurred"); } ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值