Qt⾃定义⽇志输出函数(按照年⽉⽇路径)1. 前⾔
Qt 实现的⽇志函数, 可以按照⽇期年⽉⽇路径⽣成⽂件并记录⽇志
2. 效果
3. 源码
main.cpp
#include <QCoreApplication>
#include <QMutex>
#include <QString>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
QMutex LogLock;
int WriteLog(QString type, QString content)
{
LogLock.lock();
QDateTime currentTime = QDateTime::currentDateTime();
QString time = String("yyyy-MM-dd hh:mm:ss");    QString date= String("yyyyMMdd");
QString month = date.mid(4, 2);
QString year=date.mid(0, 4);
QString day = date.mid(6, 2);
QString path="Logs/"+ year + "/" + month+"/" + day + "/";
QString fileName = path + type + "_"+date + ".log";
QString data = "[" + time + "]" + " " + content;
QDir dir;
if (! (ists(path)))
dir.mkpath(path);
QFile f(fileName);
f.open(QIODevice::WriteOnly|QIODevice::Append) ;
QTextStream t(&f);
t << data<< endl;
mid函数提取年月日f.close();
LogLock.unlock();
return 0;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
WriteLog("Log", "content");
();
}
[点击并拖拽以移动]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。