今天给大家介绍下QT里面的日志系统,直接上代码

 #include <QApplication>
#include <iostream>
#include "ThorHardwareUtil/PLC/plc.h"
#include "ThorHardwareUtil/crc.h"
#include "ThorHardwareUtil/Warehouse/baseshelf.h"
#include <QDebug>
#include "opendoorthread.h"
#include "TestProject/testform.h"
#include <QtMessageHandler>
#include <QFile>
#include <QDir>
#include <ThorUtil/confighelper.h>
#include <thread>
using namespace std;
//写入信息到文件
void WriteLog(QString str, QString LogType)
{ QString fileFolder= qApp->applicationDirPath()+"/log/"+QDateTime::currentDateTime().toString("yyyy-MM-dd");
QDir dir(fileFolder);
if(!dir.exists())
{
dir.mkpath(fileFolder);
}
QString filePath=QString("%1/%2.log").arg(fileFolder).arg(LogType);
QString strToWrite="日志产生时间:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
strToWrite.append(QString("\r\n日志消息:%1").arg(str));
strToWrite.append("\r\n---------------------------------------------------------------------");
QFile file(filePath);
file.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream text_stream(&file);
text_stream <<strToWrite<<"\r\n";
file.flush();
file.close();
//打印到控制台
std::cout << strToWrite.toLocal8Bit().constData() << std::endl;
}
//注册函数
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QString txtMessage = "";
QString messageType = "";
switch (type) {
case QtDebugMsg: //调试信息提示
messageType = "Debug";
txtMessage = QString("Debug: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
break;
case QtInfoMsg:
messageType = "Info";
txtMessage = QString("Warning: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
break;
case QtWarningMsg: //一般的warning提示
messageType = "Waring";
txtMessage = QString("Warning: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
break;
case QtCriticalMsg: //严重错误提示
messageType = "Critical";
txtMessage = QString("Critical: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
PostErrorMessage(txtMessage,messageType);
break;
case QtFatalMsg: //致命错误提示
messageType = "Fatal";
txtMessage = QString("Fatal: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(QString::number(context.line)).arg(context.function);
abort();
}
WriteLog(txtMessage, messageType);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//注册监听级别函数
qInstallMessageHandler(myMessageOutput); qDebug() << "debug"; return a.exec();
}

这样大家使用qDebug,qInfo,qCritical等打印信息就会分类输出到日志文件里面,方便查看

注意:release模式下无法打印QMessageLogContext的具体内容,因为qt默认把release给取消了,需要在pro文件里面加上DEFINES += QT_MESSAGELOGCONTEXT

QT日志系统的更多相关文章

  1. C++ 高性能无锁日志系统

    服务器编程中,日志系统需要满足几个条件 .高效,日志系统不应占用太多资源 .简洁,为了一个简单的日志功能引入大量第三方代码未必值得 .线程安全,服务器中各个线程都能同时写出日志 .轮替,服务器不出故障 ...

  2. Atitit.日志系统slf4j的使用

    Atitit.日志系统slf4j的使用 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar ...

  3. Android源码——Logger日志系统

    Android的Logger日志系统是基于内核中的Logger日志驱动程序实现的. 日志保存在内核空间中 缓冲区保存日志   分类方法:日志的类型  +   日志的输出量   日志类型:   main ...

  4. 使用Slf4j集成Log4j2构建项目日志系统的完美解决方案

    一.背景 最近因为公司项目性能需要,我们考虑把以前基于的log4j的日志系统重构成基于Slf4j和log4j2的日志系统,因为,使用slf4j可以很好的保证我们的日志系统具有良好的兼容性,兼容当前常见 ...

  5. 日志系统实战(三)-分布式跟踪的Net实现

    介绍 在大型系统开发调试中,跨系统之间联调开始变得不好使了.莫名其妙一个错误爆出来了,日志虽然有记录,但到底是哪里出问题了呢? 是Ios端参数传的不对?还是A系统或B系统提供的接口导致?相信有不少人遇 ...

  6. 日志系统实战(一)—AOP静态注入

    背景 近期在写日志系统,需要在运行时在函数内注入日志记录,并附带函数信息,这时就想到用Aop注入的方式. AOP分动态注入和静态注入两种注入的方式. 动态注入方式 利用Remoting的Context ...

  7. Android性能优化之UncaughtExceptionHandler定制自己的错误日志系统

    前言: 每当我们app测试的时候,测试人员总是对我们说这里崩溃了,那里挂掉了!我们只能默默接受,然后尝试着重现bug,更可悲的是有时候bug很难复现,为了解决这种现状所以我们要尝试这建立一个自己的bu ...

  8. [Asp.net 5] Logging-其他日志系统的实现

    Microsoft.Framework.Logging.NLog 使用Nlog扩展日志系统:按照我们上节说的,对于扩展的日志系统都要实现俩个接口ILogger.ILoggerProvider.所以在当 ...

  9. 【转载】scribe、chukwa、kafka、flume日志系统对比

    原文地址:http://www.ttlsa.com/log-system/scribe-chukwa-kafka-flume-log-system-contrast/ 1. 背景介绍许多公司的平台每天 ...

随机推荐

  1. [Swift]LeetCode468. 验证IP地址 | Validate IP Address

    Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither ...

  2. [Swift]LeetCode709. 转换成小写字母 | To Lower Case

    Implement function ToLowerCase() that has a string parameter str, and returns the same string in low ...

  3. [Swift]LeetCode961. 重复 N 次的元素 | N-Repeated Element in Size 2N Array

    In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeate ...

  4. CentOS7Linux中自动化运维工具Ansible的安装,以及通过模块批量管理多台主机

    使用自动化运维工具Ansible集中化管理服务器 Ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet ...

  5. Django中使用geetest实现滑动验证

    下载第三方模块 导入模块social-auth-app-django 和geetest 提前去官网下载gt.js或者引入http://static.geetest.com/static/tools/g ...

  6. Java面向对象--类的对象之间的几种关系详解

    转载: https://www.cnblogs.com/supren/p/7853377.html

  7. AbstractQueuedSynchronizer源码分析(ReentrantLock锁的实现)

    1.  前言 Java中好多地方用到AbstractQueuedSynchronizer(PS:简称AQS),比如ReentrantLock.线程池,这部分在面试的时候也经常被问到,今天以Reentr ...

  8. Filebeat 模块与配置

    续 • <开始使用Filebeat> 1.  关于Filebeat 当你要面对成百上千.甚至成千上万的服务器.虚拟机和容器生成的日志时,请告别 SSH 吧!Filebeat 将为你提供一种 ...

  9. 并发编程(六)——AbstractQueuedSynchronizer 之 Condition 源码分析

    我们接着上一篇文章继续,本文讲讲解ReentrantLock 公平锁和非公平锁的区别,深入分析 AbstractQueuedSynchronizer 中的 ConditionObject 公平锁和非公 ...

  10. rabbitmq.config配置参数详解

    rabbitmq.config详细配置参数 详细使用方法请点击:http://www.cnblogs.com/wyt007/p/9073316.html Key Documentation tcp_l ...