#include "MyDebug.h"
#include <QMutex>
#include <iostream>
#include <QDateTime>
#include <QCoreApplication>
#include <QFile>
#include <QDir>
#include <QTextStream>
//接收调试信息的函数
void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QMutex mutex;
QMutexLocker lock(&mutex);
QString text;
switch(type)
{
//如果是debug信息,那么直接打印至应用程序输出,然后退出本函数
case QtDebugMsg:
std::cout << msg.toStdString() << std::endl;
return ;
//如果是警告,则继续执行后面的数据处理
case QtWarningMsg:
text = QString("Warning...............................");
break;
case QtCriticalMsg:
text = QString("Critical..............................");
break;
case QtFatalMsg:
text = QString("Fatal.................................");
break;
default:
text = QString("Default...............................");
break;
}
//获取单例
MyDebug *instance = MyDebug::Instance();
//消息输出位置
QString context_info = QString("File: %1\r\nFunc: %2\r\nLine: %3")
.arg(QString(context.file))
.arg(QString(context.function))
.arg(context.line);
//消息打印时间
QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
QString current_date = QString("Time: %1")
.arg(current_date_time);
//调试信息
QString message = QString("%1\r\n%2\r\n%3\r\n%4")
.arg(text)
.arg(current_date)
.arg(context_info)
.arg("MSG : "+msg);
//log 位置
QString logPahtHtml = "<a href = \"%1\">Click here to open the log directory.</a>\r\n";
logPahtHtml = logPahtHtml.arg(QCoreApplication::applicationDirPath()+"/"+instance->logPath());
//将调试信息格式化成 html 格式,
QString msgHtml = msg;
msgHtml.replace(" ", " ");
msgHtml.remove("\r");
msgHtml.replace("\n", "<br>");
msgHtml = QString("<font color=red>" + msgHtml + "</font>");
QString contextInfoHtml = context_info;
contextInfoHtml.remove("\r");
contextInfoHtml.replace("\n", "<br>");
//格式化后的调试信息
QString messageHtml = QString("%1<br>%2<br>%3<br>%4<br>%5")
.arg(text)
.arg(logPahtHtml)
.arg(current_date)
.arg(contextInfoHtml)
.arg("MSG : "+msgHtml);
//将调试信息写入文件
QFile file(instance->logPath() + instance->logName());
file.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream text_stream(&file);
text_stream << message << "\r\n\r\n\r\n";
file.flush();
file.close();
//将处理好的调试信息发送出去
instance->sigDebugStrData(message);
//将处理成 html 的调试信息发送出去
instance->sigDebugHtmlData(messageHtml);
//检查文件是否达到了指定大小
if(file.size() < 1024*1024) {
return ;
}
//log达到了限制值则将名字更改,防止文件越来越大
for(int loop = 1; loop < 100; ++loop) {
QString fileName = QString("%1/log_%2.txt").arg(instance->logPath()).arg(loop);
QFile file_1(fileName);
if(file_1.size() < 4) {
file.rename(fileName);
return ;
}
}
}
//MyDebug单例
MyDebug* MyDebug::self = nullptr;
MyDebug* MyDebug::Instance()
{
if(!self) {
QMutex muter;
QMutexLocker clocker(&muter);
if(!self) {
self = new MyDebug();
}
}
return self;
}
//安装消息器
void MyDebug::installMessageHandler()
{
qInstallMessageHandler(outputMessage);
}
//卸载消息器
void MyDebug::uninstallMessageHandler()
{
qInstallMessageHandler(0);
}
//建立文件路径
QString MyDebug::logPath()
{
QString current_date_file_name = QDateTime::currentDateTime().toString("yyyy-MM-dd");
QDir dir(QString("log/%1").arg(current_date_file_name));
if(!dir.exists()) {
dir.mkpath("./");
}
return dir.path() + "/" ;
}
QString MyDebug::logName()
{
return "log.txt";
}
MyDebug::MyDebug(QObject *parent) : QObject(parent)
{
static MyDebug::GC gc;
}
MyDebug::~MyDebug()
{
std::cout << "~MyDebug" << std::endl;
}
//垃圾自动回收
MyDebug::GC::~GC()
{
if (self != nullptr) {
delete self;
self = nullptr;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
QDebug 数据重定向
共7个文件
cpp:3个
h:2个
ui:1个
5星 · 超过95%的资源 需积分: 45 99 下载量 16 浏览量
2018-09-08
16:51:51
上传
评论 2
收藏 3KB 7Z 举报
温馨提示
QDebug 数据重定向至文件、ui、或者其他地方,可以根据需要,自己选择
资源推荐
资源详情
资源评论
收起资源包目录
MyDebugDemo.7z (7个子文件)
MyDebugDemo
mainwindow.ui 1KB
mainwindow.cpp 538B
MyDebugDemo.pro 1KB
MyDebug.cpp 5KB
MyDebug.h 759B
main.cpp 283B
mainwindow.h 408B
共 7 条
- 1
资源评论
- 利弗莫尔2021-02-04确实挺好用的。
- Quard_D2020-05-14这个挺好的
逸冰小子
- 粉丝: 35
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功