This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathliblog.cpp
61 lines (49 loc) · 1.82 KB
/
liblog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "liblog.h"
#include <QDebug>
void LibLog::LogRec(QString message, QString exceptionName)
{
QTime systemTime = QTime::currentTime();
QDate systemDate = QDate::currentDate();
//文件名 日期+.log
QString fileName = QString("%1%2.log").arg(systemDate.toString("yyyy_MM_dd")).arg((exceptionName.isNull()? "":"_" + exceptionName));
QFile file;
//文件路径
file.setFileName("Log/" + fileName);
//判断文件夹是否存在
if(!QDir("Log").exists())
{
QDir().mkdir("Log");
}
//以可读可写的形式打开文件,并将信息保存于该文件
file.open(QFile::Append);
QFile::setPermissions(file.fileName(), QFileDevice::ReadOwner|QFileDevice::WriteOwner);
if(file.isOpen())
{
QString saveMessage = systemTime.toString("hh:mm:ss.zzz") + "\t" + message+ "\n";
file.write(saveMessage.toUtf8());
// debugShow(QSysInfo::productType() != "raspbian",systemTime.toString("hh:mm:ss.zzz"),message,exceptionName);
}
file.close();
}
void LibLog::debugShow(bool flag, QString times, QString message, QString exceptionName)
{
if(flag)
{
//清除头尾的非必要字符并以\t划分
QStringList messageModule = message.trimmed().split('\t');
QStringList messageModules;
foreach(QString inf , messageModule)
{
//排除空格
if(inf.trimmed().count() == 0)
continue;
//剩下以\n划分
messageModules.append(inf.trimmed().split('\n'));
}
//组合
message = messageModules.join(" > ");
message = (exceptionName.isNull() ? QString("%1 >>> %2").arg(times).arg(message) : QString("%1 >>> %2 *** %3").arg(times).arg(exceptionName).arg(message));
//输出信息
qDebug() << message;
}
}