-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.cpp
More file actions
66 lines (53 loc) · 1.94 KB
/
log.cpp
File metadata and controls
66 lines (53 loc) · 1.94 KB
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
62
63
64
65
66
#include "log.h"
#include <QDebug>
QFile *gFileLog = NULL;
QMessageLogger *gMLog = NULL;
const char *msgHead[] = {
"Debug ",
"Warning ",
"Critical",
"Fatal ",
"Info "
};
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz ddd");
if(gFileLog){
QTextStream tWrite(gFileLog);
QString msgText="%1 | %6 | %2:%3, %4 | %5\n";
msgText = msgText.arg(msgHead[type]).arg(context.file).arg(context.line).arg(context.function).arg(localMsg.constData()).arg(current_date_time);
//gFileLog->write(msgText.toLocal8Bit(), msgText.length());
tWrite << msgText;
}else{
fprintf(stderr, "%s | %s | %s:%u, %s | %s\n", msgHead[type], current_date_time.toLocal8Bit().constData(), context.file, context.line, context.function, localMsg.constData());
}
}
void logSysInit(QString filePath)
{
QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd");
QString qDirPath = "/log";
QDir dir;
QString currentPath = dir.currentPath();
qDirPath = currentPath + qDirPath;
QDir logDir(qDirPath);
if (!logDir.exists())
{
bool is_ok = logDir.mkdir(qDirPath);
Q_ASSERT(is_ok);
}
filePath = qDirPath + "/" + filePath + "-" + current_date_time + ".txt";
gFileLog = new QFile(filePath);
if (!gFileLog->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
return;
}
//初始化自定义日志处理函数myMessageOutput
qInstallMessageHandler(myMessageOutput);
//gMLog 这个类不要也可以,执行的时候只能看一下效果。
gMLog = new QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO);
}
void logSysInit()
{
qInstallMessageHandler(myMessageOutput);
gMLog = new QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO);
}