-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogHandler.cpp
More file actions
149 lines (126 loc) · 3.87 KB
/
LogHandler.cpp
File metadata and controls
149 lines (126 loc) · 3.87 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "LogHandler.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <QMutexLocker>
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QDate>
#include <QTimer>
#include <QDebug>
#include <QApplication>
QMutex LogHandler::s_logMutex;
QFile* LogHandler::s_file = Q_NULLPTR;
QTextStream* LogHandler::s_stream = Q_NULLPTR;
LogHandler::LogHandler()
{
QString path = qApp->applicationDirPath() + QLatin1String("/log");
QDir dir(path);
if(!dir.exists())
{
dir.mkpath(path);
}
// 以日期命名,若不存在,则创建.
QDate currentDate = QDate::currentDate();
QString date = currentDate.toString("yyyy-MM-dd");
path += QDir::separator() + date + ".log";
s_file = new QFile(path);
qInfo() << "Log Handler path : " << path;
if(s_file->exists())
{
s_stream = (s_file->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) ? new QTextStream(s_file) : Q_NULLPTR;
}
else
{
s_stream = (s_file->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) ? new QTextStream(s_file) : Q_NULLPTR;
}
if (Q_NULLPTR != s_stream)
{
s_stream->setCodec("UTF-8");
}
*s_stream << "Begin application at:" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") << endl;
// 定时刷新日志输出到文件,保证日志的实时更新.
m_flushTimer = new QTimer;
m_flushTimer->start(1000);
QObject::connect(m_flushTimer, &QTimer::timeout, [this] {
//qDebug() << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); // 测试不断写入.
QMutexLocker locker(&s_logMutex);
if (Q_NULLPTR != s_stream) {
s_stream->flush();
}
});
}
LogHandler::~LogHandler()
{
QMutexLocker locker(&s_logMutex);
*s_stream << "End application at:" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") << endl << endl <<endl;
qInstallMessageHandler(0);
if(m_flushTimer)
{
delete m_flushTimer;
m_flushTimer = Q_NULLPTR;
}
if(s_file)
{
s_stream->flush();
s_file->close();
delete s_file;
s_file = Q_NULLPTR;
}
if(s_stream)
{
delete s_stream;
s_stream = Q_NULLPTR;
}
}
void LogHandler::messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QMutexLocker locker(&s_logMutex);
QString level;
switch (type)
{
case QtDebugMsg:
level = "Debug";
break;
case QtInfoMsg:
level = "Info";
break;
case QtWarningMsg:
level = "Warn";
break;
case QtCriticalMsg:
level = "Error";
break;
case QtFatalMsg:
level = "Fatal";
break;
default:;
}
// 这里在VS中无法输出,原因未知,抄袭qlogging.cpp 中qDefaultMessageHandler函数中代码,临时解决此问题.
// 输出到标准输出.
//QByteArray localMsg = msg.toLocal8Bit();
//fprintf(stderr, "%s\n", localMsg.constData());
//fflush(stderr);
#if defined(Q_OS_WIN)
QString stdOut = msg;
stdOut.append(QLatin1Char('\n'));
OutputDebugString(reinterpret_cast<const wchar_t *>(stdOut.utf16()));
#endif
if (Q_NULLPTR == s_stream)
{
return;
}
// 输出到日志文件, 格式: 时间 - [Level] (文件名:行数, 函数): 消息.
QString fileName = context.file;
int index = fileName.lastIndexOf(QDir::separator());
fileName = fileName.mid(index + 1);
*s_stream << QString("%1 - [%2] (%3:%4, %5): %6\n")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(level)
.arg(fileName).arg(context.line).arg(context.function).arg(msg);
}
void LogHandler::installMessageHandler()
{
QMutexLocker locker(&s_logMutex);
qInstallMessageHandler(&messageHandler);
}