-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
109 lines (93 loc) · 2.68 KB
/
main.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
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
#include "mainwindow.h"
#include <singleapplication.h>
#include <QTranslator>
#include <QDebug>
#include <QFile>
#include <QDateTime>
#include <QDir>
static QString timePoint;
//日志生成
void LogMsgOutput(QtMsgType type,
const QMessageLogContext&,
const QString& msg)
{
static QMutex mutex; //日志代码互斥锁
// 持有锁
mutex.lock();
// Critical Resource of Code
QByteArray localMsg = msg.toLocal8Bit();
QString log;
switch (type) {
case QtDebugMsg:
log.append(QString("Debug %1")
.arg(localMsg.constData()));
break;
case QtInfoMsg:
log.append(QString("Info: %1")
.arg(localMsg.constData()));
break;
case QtWarningMsg:
log.append(QString("Warning: %1")
.arg(localMsg.constData()));
break;
case QtCriticalMsg:
log.append(QString("Critical: %1")
.arg(localMsg.constData()));
break;
case QtFatalMsg:
log.append(QString("Fatal: %1")
.arg(localMsg.constData()));
break;
}
QDir dir(QApplication::applicationDirPath());
dir.mkdir("logs");
QFile file(dir.path() + QString("/logs/log%1.lgt").arg(timePoint));
file.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream out(&file);
out << log << endl;
file.close();
// 释放锁
mutex.unlock();
}
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(DrCOM_JLU_Qt);
SingleApplication a(argc, argv);
//release模式输出日志到文件
// 因为调用了QApplication::applicationDirPath()
// 要在QApplication实例化之后调用
#ifndef QT_DEBUG
timePoint = QDateTime::currentDateTime().toString("yyyyMMddHHmmss");
qInstallMessageHandler(LogMsgOutput);
#endif
SingleApplication::setQuitOnLastWindowClosed(false);
SingleApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
qDebug() << "...main...";
QFont font = a.font();
font.setPointSize(10);
font.setFamily("Microsoft YaHei");
a.setFont(font);
QTranslator translator;
translator.load(":/ts/DrCOM_zh_CN.qm");
a.installTranslator(&translator);
MainWindow w(&a);
QObject::connect(&a, &SingleApplication::instanceStarted, [&w]() {
qDebug() << "One instance had started. Its window will be shown by the next line of the source code.";
w.ShowLoginWindow();
});
QSettings s(SETTINGS_FILE_NAME);
bool bHideWindow=s.value(ID_HIDE_WINDOW, false).toBool();
// 如果是软件自行重启的就不显示窗口
int restartTimes = s.value(ID_RESTART_TIMES, 0).toInt();
qDebug() << "main: restartTimes=" << restartTimes;
if(bHideWindow){
qDebug()<<"not show window caused by user settings";
} else if (restartTimes > 0) {
// 是自行重启不显示窗口
qDebug()<<"not show window caused by self restart";
} else {
qDebug() << "show caused by normal start";
w.show();
}
return a.exec();
}