Qt实现的Log日志系统
Posted QtCoderStudio
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt实现的Log日志系统相关的知识,希望对你有一定的参考价值。
对于编程开发来说,Log日志系统从来都是很有必要的,一个完善的日志系统,可以帮助程序员分析程序的各种异常情况,同时也是系统维护的一个必要手段,那么既然Log系统这么重要,我今天就手把手带着你们写一个自己的Log系统。
我就直接上代码了,这样来的比较直接一些,有看不懂的,私信给我即可。
一、头文件代码实现
#ifndef SIMPLELOG_H
#define SIMPLELOG_H
#include <QFile>
#include <QMutex>
#include <QDateTime>
#include <QTextStream>
#include <stdio.h>
#define LOG_IDENTIFIER "_Log.html"
#define LOG_INFO 0
#define LOG_WARNING 1
#define LOG_ERROR 2
#define DETAIL_INFO QString::fromLocal8Bit("<.%1.>@%2=>%3: ").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__)
#define LOGERROR(_log) SimpleLog::getInstance()->writeLog(_log, DETAIL_INFO, LOG_ERROR)
#define LOGINFO(_log) SimpleLog::getInstance()->writeLog(_log, DETAIL_INFO, LOG_INFO)
#define LOGWRANING(_log) SimpleLog::getInstance()->writeLog(_log, DETAIL_INFO, LOG_WARNING)
class SimpleLog
{
private:
SimpleLog();
static SimpleLog* m_instance;
public:
~SimpleLog();
static SimpleLog* getInstance();
void writeLog(QString _log, QString _details = "", int _flag = LOG_INFO);
private:
void openNewLog();
void endLog();
private:
QFile m_log;
QMutex m_mutex;
QTextStream m_stream;
int m_index;
};
#endif // SIMPLELOG_H
二、CPP文件代码实现
#include "simplelog.h"
#include <QDir>
#include <QSettings>
#include <QApplication>
#include <QDebug>
SimpleLog* SimpleLog::m_instance = new SimpleLog;
SimpleLog* SimpleLog::getInstance()
{
return m_instance;
}
SimpleLog::SimpleLog()
{
QDir _applicationDir(QCoreApplication::applicationDirPath());
_applicationDir.cdUp();
if (!_applicationDir.exists(_applicationDir.path() + "/log"))
{
_applicationDir.mkpath(_applicationDir.path() + "/log");
}
_applicationDir.cd("log");
QString date = QDate::currentDate().toString("yyyy-MM-dd");
QString time = QTime::currentTime().toString("hh-mm-ss");
QString fileNamelog;
fileNamelog+=date;
fileNamelog+=" ";
fileNamelog+=time;
fileNamelog+=LOG_IDENTIFIER;
QString _path = _applicationDir.path() + "/" + date;
if (!_applicationDir.exists(_path))
{
_applicationDir.mkpath(_path);
}
_applicationDir.cd(date);
m_index = 0;
m_log.setFileName(_applicationDir.path() + "/" + fileNamelog);
if (m_log.exists())m_log.remove();
if (m_log.open(QIODevice::ReadWrite | QIODevice::Text | QFile::Append))
{
m_stream.setDevice(&m_log);
QString _logTitle = QString::fromLocal8Bit("<html>\n<head>\n<title>日志记录</title>\n</head>\n<body>\n");
m_stream << _logTitle;
}
else
{
QString _err = m_log.errorString();
}
}
SimpleLog::~SimpleLog()
{
endLog();
}
void SimpleLog::writeLog(QString _log, QString _details, int _flag)
{
QString time = QTime::currentTime().toString("hh:mm:ss");
m_mutex.lock();
if (_flag == LOG_INFO)
{
m_stream << "<br><font size=\"3\" color=\"#459EF6\" family= \"微软雅黑\">" << "Index: "<<m_index << " Time-->" << time << " " << _details<< _log << "</font>";
}
else if (_flag == LOG_WARNING)
{
m_stream << "<br><font size=\"3\" color=\"#0000FF\" family= \"微软雅黑\">" << "Index: "<<m_index << " Time-->" << time << " " << _details << _log << "</font>";
}
else if (_flag == LOG_ERROR)
{
m_stream << "<br><font size=\"3\" color=\"#FF0000\" family= \"微软雅黑\">" << "Index: "<<m_index << " Time-->" << time << " " << _details << _log << "</font>";
}
m_stream.flush();
m_log.flush();
m_index += 1;
m_mutex.unlock();
}
void SimpleLog::endLog()
{
if (m_log.isOpen())
{
m_stream << "</body>";
m_stream.flush();
m_log.flush();
m_log.close();
}
}
希望可以帮助那些还有疑惑或者对这块不了解的同学们,如果不懂的也可以在私信给我,我将会给你提供对应的帮助。
邮箱:ycloco001@126.com
WeChat:yanche521
如果觉得这篇文章对你有用,请记得关注或打赏哦,谢谢。
以上是关于Qt实现的Log日志系统的主要内容,如果未能解决你的问题,请参考以下文章
Qt+QtWebApp开发笔记:http服务器日志系统介绍添加日志系统至Demo测试
Qt重定向qDebug,实现日志系统(QtDebugMsgQtInfoMsgQtWarningMsgQtCriticalMsgQtFatalMsg)