forked from Skarsnik/QUsb2snes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
104 lines (93 loc) · 3.05 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
#include "appui.h"
//#include "sd2snesdevice.h"
#include "wsserver.h"
#ifdef Q_OS_MACOS
#include "osx/appnap.h"
#endif
#include <QSerialPortInfo>
#include <QDebug>
#include <QApplication>
#include <QTimer>
#include <QThread>
#include <QSystemTrayIcon>
#include <QFile>
#include <QMessageBox>
#include <QMenu>
#include <QObject>
#include <QHostInfo>
WSServer wsServer;
QSettings* globalSettings;
static QTextStream logfile;
//static QTextStream lowlogfile;
static QTextStream cout(stdout);
//bool dontLogNext = false;
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QTextStream* log = &logfile;
/*if (QString(context.category) == "LowLevelTelnet")
return ;*/
//cout << msg;
QString logString = QString("%6 %5 - %7: %1 \t(%2:%3, %4)").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function).arg(context.category, 20).arg(QDateTime::currentDateTime().toString(Qt::ISODate));
switch (type)
{
case QtDebugMsg:
*log << logString.arg("Debug");
break;
case QtCriticalMsg:
*log << logString.arg("Critical");
break;
case QtWarningMsg:
*log << logString.arg("Warning");
break;
case QtFatalMsg:
*log << logString.arg("Fatal");
*log<< "\n"; log->flush();
QMessageBox::critical(nullptr, QObject::tr("Critical error"), msg);
qApp->exit(1);
break;
case QtInfoMsg:
*log << logString.arg("Info");
break;
}
*log << "\n";
log->flush();
cout << QString("%1 : %2").arg(context.category, 20).arg(msg) << "\n";
cout.flush();
}
void startServer()
{
//myTrayIcon->showMessage(QString("Webserver started"), QString("Webserver started"));
quint16 port = 8080;
QHostAddress addr = QHostInfo::fromName("localhost").addresses().first();
if (globalSettings->contains("listen"))
addr = QHostInfo::fromName(globalSettings->value("listen").toString()).addresses().first();
if (globalSettings->contains("port"))
port = globalSettings->value("port").toUInt();
if (!wsServer.start(addr, port))
qApp->exit(1);
}
int main(int ac, char *ag[])
{
QApplication app(ac, ag);
QFile mlog(qApp->applicationDirPath() + "/log.txt");
logfile.setDevice(&mlog);
globalSettings = new QSettings("config.ini", QSettings::IniFormat);
if (mlog.open(QIODevice::WriteOnly | QIODevice::Text))
qInstallMessageHandler(myMessageOutput);
QApplication::setApplicationName("QUsb2Snes");
QApplication::setApplicationVersion("0.8");
#ifdef Q_OS_MACOS
auto appNap = new AppNapSuspender();
appNap->suspend();
#endif
if (app.arguments().size() == 2 && app.arguments().at(1) == "-nogui")
{
QTimer::singleShot(100, &startServer);
return app.exec();
}
AppUi* appUi = new AppUi();
appUi->sysTray->show();
QTimer::singleShot(200, &startServer);
return app.exec();
}