2018-07-23 15:21:31 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2016, Canonical Ltd.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2018-07-24 18:41:07 +00:00
|
|
|
// #define BUILD_FOR_BUNDLE
|
2018-07-23 15:21:31 +00:00
|
|
|
|
|
|
|
#include <QCommandLineParser>
|
2018-08-21 13:18:16 +00:00
|
|
|
#include <QDirIterator>
|
2018-07-23 15:21:31 +00:00
|
|
|
#include <QFile>
|
2018-08-29 22:45:19 +00:00
|
|
|
#include <QFontDatabase>
|
2018-11-28 19:36:39 +00:00
|
|
|
#include <QApplication>
|
2018-11-01 08:24:31 +00:00
|
|
|
#include <QMutexLocker>
|
2018-07-23 15:21:31 +00:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QQuickView>
|
2018-07-24 18:41:07 +00:00
|
|
|
#include <QStandardPaths>
|
2018-07-23 15:21:31 +00:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include "attachedproperties.h"
|
|
|
|
#include "reactitem.h"
|
|
|
|
#include "rootview.h"
|
|
|
|
#include "utilities.h"
|
|
|
|
|
2018-08-29 22:45:19 +00:00
|
|
|
#include "exceptionglobalhandler.h"
|
|
|
|
|
2018-12-13 15:42:28 +00:00
|
|
|
#include "appconfig.h"
|
2018-11-06 16:01:43 +00:00
|
|
|
Q_DECLARE_LOGGING_CATEGORY(JSSERVER)
|
|
|
|
Q_DECLARE_LOGGING_CATEGORY(STATUS)
|
|
|
|
Q_LOGGING_CATEGORY(JSSERVER, "jsserver")
|
|
|
|
Q_LOGGING_CATEGORY(STATUS, "status")
|
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
static QStringList consoleOutputStrings;
|
|
|
|
static QMutex consoleOutputMutex;
|
2018-08-15 21:27:13 +00:00
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
#ifdef BUILD_FOR_BUNDLE
|
2018-11-16 16:34:39 +00:00
|
|
|
bool nodeJsServerStarted = false;
|
|
|
|
QProcess *g_nodeJsServerProcess = nullptr;
|
|
|
|
#define NODEJS_SERVER_NAME "ubuntu-server"
|
2018-07-23 15:21:31 +00:00
|
|
|
#endif
|
|
|
|
|
2018-07-24 18:41:07 +00:00
|
|
|
const int MAIN_WINDOW_WIDTH = 1024;
|
|
|
|
const int MAIN_WINDOW_HEIGHT = 768;
|
2018-08-29 22:45:19 +00:00
|
|
|
const QString CRASH_REPORT_EXECUTABLE = QStringLiteral("reportApp");
|
2018-07-24 18:41:07 +00:00
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
const char *ENABLE_LOG_FILE_ENV_VAR_NAME = "STATUS_LOG_FILE_ENABLED";
|
|
|
|
const char *LOG_FILE_PATH_ENV_VAR_NAME = "STATUS_LOG_PATH";
|
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
// TODO: some way to change while running
|
|
|
|
class ReactNativeProperties : public QObject {
|
|
|
|
Q_OBJECT
|
2018-11-16 16:34:39 +00:00
|
|
|
Q_PROPERTY(
|
|
|
|
bool liveReload READ liveReload WRITE setLiveReload NOTIFY liveReloadChanged)
|
|
|
|
Q_PROPERTY(
|
|
|
|
QUrl codeLocation READ codeLocation WRITE setCodeLocation NOTIFY codeLocationChanged)
|
|
|
|
Q_PROPERTY(
|
|
|
|
QString pluginsPath READ pluginsPath WRITE setPluginsPath NOTIFY pluginsPathChanged)
|
2018-07-23 15:21:31 +00:00
|
|
|
Q_PROPERTY(
|
|
|
|
QString executor READ executor WRITE setExecutor NOTIFY executorChanged)
|
2018-10-27 19:24:47 +00:00
|
|
|
Q_PROPERTY(
|
|
|
|
QVariantMap initialProps READ initialProps WRITE setInitialProps NOTIFY initialPropsChanged)
|
2018-07-23 15:21:31 +00:00
|
|
|
public:
|
2018-11-01 08:24:31 +00:00
|
|
|
ReactNativeProperties(QObject *parent = nullptr) : QObject(parent) {
|
2018-07-23 15:21:31 +00:00
|
|
|
m_codeLocation = m_packagerTemplate.arg(m_packagerHost).arg(m_packagerPort);
|
|
|
|
}
|
|
|
|
bool liveReload() const { return m_liveReload; }
|
|
|
|
void setLiveReload(bool liveReload) {
|
|
|
|
if (m_liveReload == liveReload)
|
|
|
|
return;
|
|
|
|
m_liveReload = liveReload;
|
|
|
|
Q_EMIT liveReloadChanged();
|
|
|
|
}
|
|
|
|
QUrl codeLocation() const { return m_codeLocation; }
|
|
|
|
void setCodeLocation(const QUrl &codeLocation) {
|
|
|
|
if (m_codeLocation == codeLocation)
|
|
|
|
return;
|
|
|
|
m_codeLocation = codeLocation;
|
|
|
|
Q_EMIT codeLocationChanged();
|
|
|
|
}
|
|
|
|
QString pluginsPath() const { return m_pluginsPath; }
|
|
|
|
void setPluginsPath(const QString &pluginsPath) {
|
|
|
|
if (m_pluginsPath == pluginsPath)
|
|
|
|
return;
|
|
|
|
m_pluginsPath = pluginsPath;
|
|
|
|
Q_EMIT pluginsPathChanged();
|
|
|
|
}
|
|
|
|
QString executor() const { return m_executor; }
|
|
|
|
void setExecutor(const QString &executor) {
|
|
|
|
if (m_executor == executor)
|
|
|
|
return;
|
|
|
|
m_executor = executor;
|
|
|
|
Q_EMIT executorChanged();
|
|
|
|
}
|
2018-10-27 19:24:47 +00:00
|
|
|
QVariantMap initialProps() const { return m_initialProps; }
|
|
|
|
void setInitialProps(const QVariantMap &initialProps) {
|
|
|
|
if (m_initialProps == initialProps)
|
|
|
|
return;
|
|
|
|
m_initialProps = initialProps;
|
|
|
|
Q_EMIT initialPropsChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
QString packagerHost() const { return m_packagerHost; }
|
|
|
|
void setPackagerHost(const QString &packagerHost) {
|
|
|
|
if (m_packagerHost == packagerHost)
|
|
|
|
return;
|
|
|
|
m_packagerHost = packagerHost;
|
|
|
|
setCodeLocation(m_packagerTemplate.arg(m_packagerHost).arg(m_packagerPort));
|
|
|
|
}
|
|
|
|
QString packagerPort() const { return m_packagerPort; }
|
|
|
|
void setPackagerPort(const QString &packagerPort) {
|
|
|
|
if (m_packagerPort == packagerPort)
|
|
|
|
return;
|
|
|
|
m_packagerPort = packagerPort;
|
|
|
|
setCodeLocation(m_packagerTemplate.arg(m_packagerHost).arg(m_packagerPort));
|
|
|
|
}
|
|
|
|
void setLocalSource(const QString &source) {
|
|
|
|
if (m_localSource == source)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// overrides packager*
|
|
|
|
if (source.startsWith("file:")) {
|
|
|
|
setCodeLocation(source);
|
|
|
|
} else {
|
|
|
|
QFileInfo fi(source);
|
|
|
|
if (!fi.exists()) {
|
2018-11-06 16:01:43 +00:00
|
|
|
qCWarning(STATUS) << "Attempt to set non-existent local source file";
|
2018-07-23 15:21:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
setCodeLocation(QUrl::fromLocalFile(fi.absoluteFilePath()));
|
|
|
|
setLiveReload(false);
|
|
|
|
}
|
|
|
|
}
|
2018-10-27 19:24:47 +00:00
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
Q_SIGNALS:
|
|
|
|
void liveReloadChanged();
|
|
|
|
void codeLocationChanged();
|
|
|
|
void pluginsPathChanged();
|
|
|
|
void executorChanged();
|
2018-10-27 19:24:47 +00:00
|
|
|
void initialPropsChanged();
|
2018-07-23 15:21:31 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_liveReload = false;
|
|
|
|
QString m_packagerHost = "localhost";
|
|
|
|
QString m_packagerPort = "8081";
|
|
|
|
QString m_localSource;
|
|
|
|
QString m_packagerTemplate =
|
|
|
|
"http://%1:%2/index.desktop.bundle?platform=desktop&dev=true";
|
|
|
|
QUrl m_codeLocation;
|
|
|
|
QString m_pluginsPath;
|
|
|
|
#ifdef BUILD_FOR_BUNDLE
|
|
|
|
QString m_executor = "RemoteServerConnection";
|
|
|
|
#else
|
|
|
|
QString m_executor = "LocalServerConnection";
|
|
|
|
#endif
|
2018-10-27 19:24:47 +00:00
|
|
|
QVariantMap m_initialProps;
|
2018-07-23 15:21:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void saveMessage(QtMsgType type, const QMessageLogContext &context,
|
|
|
|
const QString &msg);
|
|
|
|
void writeLogsToFile();
|
2018-11-06 16:01:43 +00:00
|
|
|
void writeLogFromJSServer(const QString &msg);
|
|
|
|
void writeSingleLineLogFromJSServer(const QString &msg);
|
2018-11-01 08:24:31 +00:00
|
|
|
|
|
|
|
#ifdef BUILD_FOR_BUNDLE
|
|
|
|
|
2018-11-16 16:34:39 +00:00
|
|
|
void killZombieJsServer();
|
|
|
|
bool runNodeJsServer();
|
2018-11-01 08:24:31 +00:00
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
#endif
|
|
|
|
|
2018-08-21 13:18:16 +00:00
|
|
|
void loadFontsFromResources() {
|
2018-08-29 22:45:19 +00:00
|
|
|
QDirIterator it(":", QDirIterator::Subdirectories);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
QString resourceFile = it.next();
|
|
|
|
if (resourceFile.endsWith(".otf", Qt::CaseInsensitive) ||
|
|
|
|
resourceFile.endsWith(".ttf", Qt::CaseInsensitive)) {
|
2018-11-16 16:34:39 +00:00
|
|
|
qint32 fontId = QFontDatabase::addApplicationFont(resourceFile);
|
|
|
|
if (Q_UNLIKELY(fontId == -1)) {
|
|
|
|
qCDebug(STATUS) << "Unable to install font" << resourceFile;
|
|
|
|
}
|
2018-08-21 13:18:16 +00:00
|
|
|
}
|
2018-08-29 22:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void exceptionPostHandledCallback() {
|
|
|
|
#ifdef BUILD_FOR_BUNDLE
|
2018-11-16 16:34:39 +00:00
|
|
|
if (g_nodeJsServerProcess) {
|
|
|
|
g_nodeJsServerProcess->kill();
|
2018-08-29 22:45:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-08-21 13:18:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
bool redirectLogIntoFile() {
|
2018-09-06 16:51:49 +00:00
|
|
|
#ifdef BUILD_FOR_BUNDLE
|
2018-11-01 08:24:31 +00:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return qEnvironmentVariable(ENABLE_LOG_FILE_ENV_VAR_NAME, "") ==
|
|
|
|
QStringLiteral("1");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QString getDataStoragePath() {
|
2018-10-27 19:24:47 +00:00
|
|
|
QString statusDataDir = qgetenv("STATUS_DATA_DIR");
|
|
|
|
QString dataStoragePath;
|
|
|
|
if (!statusDataDir.isEmpty()) {
|
|
|
|
dataStoragePath = statusDataDir;
|
2018-12-21 14:17:49 +00:00
|
|
|
} else {
|
2018-10-27 19:24:47 +00:00
|
|
|
dataStoragePath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
|
|
|
|
}
|
2018-09-06 16:51:49 +00:00
|
|
|
QDir dir(dataStoragePath);
|
|
|
|
if (!dir.exists()) {
|
|
|
|
dir.mkpath(".");
|
|
|
|
}
|
|
|
|
return dataStoragePath;
|
|
|
|
}
|
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
2018-11-28 19:36:39 +00:00
|
|
|
QApplication app(argc, argv);
|
2018-07-23 15:21:31 +00:00
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
QCoreApplication::setApplicationName("Status");
|
2018-08-29 22:45:19 +00:00
|
|
|
|
2018-12-18 17:30:26 +00:00
|
|
|
// Init AppConfig
|
|
|
|
AppConfig::inst().getValue(AppConfig::LOGGING_ENABLED);
|
|
|
|
|
2018-08-29 22:45:19 +00:00
|
|
|
QString appPath = QCoreApplication::applicationDirPath();
|
2018-11-01 08:24:31 +00:00
|
|
|
QString dataStoragePath = getDataStoragePath();
|
2018-11-16 16:34:39 +00:00
|
|
|
#ifdef BUILD_FOR_BUNDLE
|
2018-10-27 19:24:47 +00:00
|
|
|
if (qgetenv("STATUS_DATA_DIR").isEmpty()) {
|
|
|
|
killZombieJsServer();
|
|
|
|
}
|
2018-11-16 16:34:39 +00:00
|
|
|
#else
|
2018-11-01 08:24:31 +00:00
|
|
|
dataStoragePath = "";
|
2018-08-29 22:45:19 +00:00
|
|
|
#endif
|
|
|
|
|
2018-09-06 16:51:49 +00:00
|
|
|
ExceptionGlobalHandler exceptionHandler(
|
|
|
|
appPath + QDir::separator() + CRASH_REPORT_EXECUTABLE,
|
2018-11-01 08:24:31 +00:00
|
|
|
exceptionPostHandledCallback, dataStoragePath);
|
2018-08-29 22:45:19 +00:00
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
Q_INIT_RESOURCE(react_resources);
|
|
|
|
|
2018-08-21 13:18:16 +00:00
|
|
|
loadFontsFromResources();
|
2018-11-01 08:24:31 +00:00
|
|
|
if (redirectLogIntoFile()) {
|
|
|
|
qInstallMessageHandler(saveMessage);
|
|
|
|
}
|
|
|
|
|
2018-12-13 15:42:28 +00:00
|
|
|
//qCDebug(STATUS) << "###STATUS_NO_LOGGING";
|
|
|
|
|
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
#ifdef BUILD_FOR_BUNDLE
|
2018-11-16 16:34:39 +00:00
|
|
|
if (!runNodeJsServer()) {
|
|
|
|
if (g_nodeJsServerProcess->state() == QProcess::NotRunning) {
|
|
|
|
// If we failed to start the Node.js server (happens on Windows if the Node.js server process was previously running), let's do a final attempt
|
|
|
|
delete g_nodeJsServerProcess;
|
|
|
|
if (!runNodeJsServer()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 16:01:52 +00:00
|
|
|
|
|
|
|
app.setWindowIcon(QIcon(":/icon.png"));
|
2018-07-23 15:21:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
QQuickView view;
|
|
|
|
ReactNativeProperties *rnp = new ReactNativeProperties(&view);
|
2018-10-27 19:24:47 +00:00
|
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
|
|
QVariantMap initialProps;
|
|
|
|
QString statusNodePort = env.value("STATUS_NODE_PORT");
|
|
|
|
QString statusDataDir = env.value("STATUS_DATA_DIR");
|
|
|
|
if (!statusNodePort.isEmpty()) {
|
|
|
|
initialProps["STATUS_NODE_PORT"] = statusNodePort;
|
|
|
|
}
|
|
|
|
if (!statusDataDir.isEmpty()) {
|
|
|
|
initialProps["STATUS_DATA_DIR"] = statusDataDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
rnp->setInitialProps(initialProps);
|
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
#ifdef BUILD_FOR_BUNDLE
|
|
|
|
rnp->setCodeLocation("file:" + QGuiApplication::applicationDirPath() +
|
|
|
|
"/assets");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
utilities::registerReactTypes();
|
|
|
|
|
|
|
|
QCommandLineParser p;
|
|
|
|
p.setApplicationDescription("React Native host application");
|
|
|
|
p.addHelpOption();
|
|
|
|
p.addOptions({
|
|
|
|
{{"R", "live-reload"}, "Enable live reload."},
|
|
|
|
{{"H", "host"}, "Set packager host address.", rnp->packagerHost()},
|
|
|
|
{{"P", "port"}, "Set packager port number.", rnp->packagerPort()},
|
|
|
|
{{"L", "local"}, "Set path to the local packaged source", "not set"},
|
|
|
|
{{"M", "plugins-path"}, "Set path to node modules", "./plugins"},
|
|
|
|
{{"E", "executor"}, "Set Javascript executor", rnp->executor()},
|
|
|
|
});
|
|
|
|
p.process(app);
|
|
|
|
rnp->setLiveReload(p.isSet("live-reload"));
|
|
|
|
if (p.isSet("host"))
|
|
|
|
rnp->setPackagerHost(p.value("host"));
|
|
|
|
if (p.isSet("port"))
|
|
|
|
rnp->setPackagerPort(p.value("port"));
|
|
|
|
if (p.isSet("local"))
|
|
|
|
rnp->setLocalSource(p.value("local"));
|
|
|
|
if (p.isSet("plugins-path"))
|
|
|
|
rnp->setPluginsPath(p.value("plugins-path"));
|
|
|
|
if (p.isSet("executor"))
|
|
|
|
rnp->setExecutor(p.value("executor"));
|
|
|
|
|
|
|
|
view.rootContext()->setContextProperty("ReactNativeProperties", rnp);
|
|
|
|
view.setSource(QUrl("qrc:///main.qml"));
|
|
|
|
view.setResizeMode(QQuickView::SizeRootObjectToView);
|
2018-07-24 18:41:07 +00:00
|
|
|
view.resize(MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT);
|
2018-07-23 15:21:31 +00:00
|
|
|
view.show();
|
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
QTimer flushLogsToFileTimer;
|
|
|
|
if (redirectLogIntoFile()) {
|
|
|
|
flushLogsToFileTimer.setInterval(500);
|
|
|
|
QObject::connect(&flushLogsToFileTimer, &QTimer::timeout,
|
|
|
|
[=]() { writeLogsToFile(); });
|
|
|
|
flushLogsToFileTimer.start();
|
|
|
|
}
|
2018-07-23 15:21:31 +00:00
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
QString getLogFilePath() {
|
|
|
|
QString logFilePath;
|
2018-07-23 15:21:31 +00:00
|
|
|
#ifdef BUILD_FOR_BUNDLE
|
2018-11-16 16:34:39 +00:00
|
|
|
logFilePath = getDataStoragePath() + QDir::separator() + "Status.log";
|
2018-11-01 08:24:31 +00:00
|
|
|
#else
|
|
|
|
logFilePath = qEnvironmentVariable(LOG_FILE_PATH_ENV_VAR_NAME, "");
|
|
|
|
if (logFilePath.isEmpty()) {
|
2018-11-16 16:34:39 +00:00
|
|
|
logFilePath = getDataStoragePath() + QDir::separator() + "StatusDev.log";
|
2018-11-01 08:24:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return logFilePath;
|
|
|
|
}
|
2018-07-23 15:21:31 +00:00
|
|
|
|
|
|
|
void writeLogsToFile() {
|
2018-08-15 21:27:13 +00:00
|
|
|
QMutexLocker locker(&consoleOutputMutex);
|
2018-12-13 09:16:54 +00:00
|
|
|
if(consoleOutputStrings.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
QFile logFile(getLogFilePath());
|
2018-07-23 15:21:31 +00:00
|
|
|
if (logFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
|
|
|
|
for (QString message : consoleOutputStrings) {
|
|
|
|
logFile.write(message.toStdString().c_str());
|
|
|
|
}
|
|
|
|
consoleOutputStrings.clear();
|
|
|
|
|
|
|
|
logFile.flush();
|
|
|
|
logFile.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
#ifdef BUILD_FOR_BUNDLE
|
2018-11-16 16:34:39 +00:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <tlhelp32.h>
|
|
|
|
|
|
|
|
bool IsProcessRunning(const wchar_t *processName) {
|
|
|
|
bool exists = false;
|
|
|
|
PROCESSENTRY32 entry = { sizeof(PROCESSENTRY32) };
|
|
|
|
|
|
|
|
HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
if (snapshot != NULL) {
|
|
|
|
if (::Process32First(snapshot, &entry)) {
|
|
|
|
do {
|
|
|
|
if (!wcsicmp(entry.szExeFile, processName)) {
|
|
|
|
exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (::Process32Next(snapshot, &entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
::CloseHandle(snapshot);
|
|
|
|
}
|
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void killZombieJsServer() {
|
|
|
|
// Ensure that a zombie Node.js server process is not still running in the background before we spawn a new one
|
|
|
|
QString cmd;
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
cmd = QString("pkill -f %1").arg(NODEJS_SERVER_NAME);
|
|
|
|
#elif defined(Q_OS_MAC)
|
|
|
|
cmd = QString("killall -9 %1").arg(NODEJS_SERVER_NAME);
|
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
#define _CAT(A, B) A##B
|
|
|
|
#define _W(A) _CAT(L, #A)
|
|
|
|
WCHAR exeName[_MAX_PATH];
|
|
|
|
wsprintf(exeName, L"%s.exe", _W(NODEJS_SERVER_NAME));
|
|
|
|
if (IsProcessRunning(exeName)) {
|
|
|
|
qCDebug(STATUS) << NODEJS_SERVER_NAME << "is running, killing it";
|
|
|
|
::ShellExecuteW(NULL, NULL, L"tskill", _W(NODEJS_SERVER_NAME), NULL, SW_HIDE);
|
|
|
|
} else {
|
|
|
|
qCDebug(STATUS) << NODEJS_SERVER_NAME << "is not running";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!cmd.isEmpty()) {
|
|
|
|
qCDebug(STATUS) << "Running " << cmd;
|
|
|
|
QByteArray cmdArray = cmd.toLocal8Bit();
|
|
|
|
system(cmdArray.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runNodeJsServer() {
|
|
|
|
g_nodeJsServerProcess = new QProcess();
|
|
|
|
g_nodeJsServerProcess->setWorkingDirectory(getDataStoragePath());
|
|
|
|
g_nodeJsServerProcess->setProgram(QGuiApplication::applicationDirPath() + QDir::separator() + NODEJS_SERVER_NAME);
|
2018-10-27 19:24:47 +00:00
|
|
|
QString port = qgetenv("REACT_SERVER_PORT");
|
|
|
|
if (!port.isEmpty()) {
|
|
|
|
QStringList arguments = (QStringList() << "--port" << port);
|
|
|
|
g_nodeJsServerProcess->setArguments(arguments);
|
|
|
|
}
|
2018-11-16 16:34:39 +00:00
|
|
|
QObject::connect(g_nodeJsServerProcess, &QProcess::errorOccurred,
|
2018-07-23 15:21:31 +00:00
|
|
|
[=](QProcess::ProcessError) {
|
2018-11-06 16:01:43 +00:00
|
|
|
qCWarning(JSSERVER) << "process name: "
|
2018-11-16 16:34:39 +00:00
|
|
|
<< qUtf8Printable(g_nodeJsServerProcess->program());
|
2018-11-06 16:01:43 +00:00
|
|
|
qCWarning(JSSERVER) << "process error: "
|
2018-11-16 16:34:39 +00:00
|
|
|
<< qUtf8Printable(g_nodeJsServerProcess->errorString());
|
2018-07-23 15:21:31 +00:00
|
|
|
});
|
|
|
|
|
2018-08-29 22:45:19 +00:00
|
|
|
QObject::connect(
|
2018-11-16 16:34:39 +00:00
|
|
|
g_nodeJsServerProcess, &QProcess::readyReadStandardOutput, [=] {
|
|
|
|
writeLogFromJSServer(g_nodeJsServerProcess->readAllStandardOutput().trimmed());
|
2018-08-29 22:45:19 +00:00
|
|
|
});
|
|
|
|
QObject::connect(
|
2018-11-16 16:34:39 +00:00
|
|
|
g_nodeJsServerProcess, &QProcess::readyReadStandardError, [=] {
|
2018-08-29 22:45:19 +00:00
|
|
|
QString output =
|
2018-11-16 16:34:39 +00:00
|
|
|
g_nodeJsServerProcess->readAllStandardError().trimmed();
|
2018-11-06 16:01:43 +00:00
|
|
|
writeLogFromJSServer(output);
|
2018-08-29 22:45:19 +00:00
|
|
|
if (output.contains("Server starting")) {
|
2018-11-16 16:34:39 +00:00
|
|
|
nodeJsServerStarted = true;
|
2018-08-29 22:45:19 +00:00
|
|
|
}
|
|
|
|
});
|
2018-07-23 15:21:31 +00:00
|
|
|
|
|
|
|
QObject::connect(QGuiApplication::instance(), &QCoreApplication::aboutToQuit,
|
|
|
|
[=]() {
|
2018-11-16 16:34:39 +00:00
|
|
|
qCDebug(STATUS) << "Kill node.js server process";
|
|
|
|
g_nodeJsServerProcess->kill();
|
2018-07-23 15:21:31 +00:00
|
|
|
});
|
|
|
|
|
2018-11-16 16:34:39 +00:00
|
|
|
qCDebug(STATUS) << "starting node.js server process...";
|
|
|
|
g_nodeJsServerProcess->start();
|
2018-11-06 16:01:43 +00:00
|
|
|
qCDebug(STATUS) << "wait for started...";
|
2018-07-23 15:21:31 +00:00
|
|
|
|
2018-11-16 16:34:39 +00:00
|
|
|
if (g_nodeJsServerProcess->waitForReadyRead(10000)) {
|
|
|
|
// We know that the process started, now wait until it communicates that it has started
|
|
|
|
while (!nodeJsServerStarted) {
|
|
|
|
QGuiApplication::processEvents();
|
|
|
|
}
|
|
|
|
qCDebug(STATUS) << "waiting finished";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
qCDebug(STATUS) << "failed to start process";
|
2018-07-23 15:21:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 16:34:39 +00:00
|
|
|
return false;
|
2018-07-23 15:21:31 +00:00
|
|
|
}
|
2018-11-16 16:34:39 +00:00
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
#endif
|
2018-07-23 15:21:31 +00:00
|
|
|
|
2018-11-06 16:01:43 +00:00
|
|
|
void writeLogFromJSServer(const QString &msg) {
|
|
|
|
if (msg.contains("\\n")) {
|
|
|
|
QStringList lines = msg.split("\\n");
|
|
|
|
foreach (const QString &line, lines) {
|
|
|
|
writeSingleLineLogFromJSServer(line);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeSingleLineLogFromJSServer(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString extractJSServerMessage(const QString& msg, int prefixLength) {
|
|
|
|
return msg.mid(prefixLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeSingleLineLogFromJSServer(const QString &msg) {
|
|
|
|
if (msg.startsWith("TRACE "))
|
|
|
|
qCDebug(JSSERVER) << qUtf8Printable(extractJSServerMessage(msg, 6));
|
|
|
|
else if (msg.startsWith("DEBUG "))
|
|
|
|
qCDebug(JSSERVER) << qUtf8Printable(extractJSServerMessage(msg, 6));
|
|
|
|
else if (msg.startsWith("INFO "))
|
|
|
|
qCInfo(JSSERVER) << qUtf8Printable(extractJSServerMessage(msg, 5));
|
|
|
|
else if (msg.startsWith("WARN "))
|
|
|
|
qCWarning(JSSERVER) << qUtf8Printable(extractJSServerMessage(msg, 5));
|
|
|
|
else if (msg.startsWith("ERROR "))
|
|
|
|
qCWarning(JSSERVER) << qUtf8Printable(extractJSServerMessage(msg, 6));
|
|
|
|
else if (msg.startsWith("FATAL "))
|
|
|
|
qCCritical(JSSERVER) << qUtf8Printable(extractJSServerMessage(msg, 6));
|
|
|
|
else
|
|
|
|
qCDebug(JSSERVER) << qUtf8Printable(msg);
|
|
|
|
}
|
|
|
|
|
2018-08-15 21:27:13 +00:00
|
|
|
void appendConsoleString(const QString &msg) {
|
|
|
|
QMutexLocker locker(&consoleOutputMutex);
|
|
|
|
consoleOutputStrings << msg;
|
|
|
|
}
|
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
void saveMessage(QtMsgType type, const QMessageLogContext &context,
|
|
|
|
const QString &msg) {
|
2018-12-13 09:16:54 +00:00
|
|
|
|
2018-11-01 08:24:31 +00:00
|
|
|
Q_UNUSED(context);
|
2018-07-23 15:21:31 +00:00
|
|
|
QByteArray localMsg = msg.toLocal8Bit();
|
|
|
|
QString message = localMsg + "\n";
|
2018-09-14 15:30:31 +00:00
|
|
|
QString timestamp = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
|
|
|
|
QString typeStr;
|
2018-07-23 15:21:31 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case QtDebugMsg:
|
2018-09-14 15:30:31 +00:00
|
|
|
typeStr = "D";
|
2018-07-23 15:21:31 +00:00
|
|
|
break;
|
|
|
|
case QtInfoMsg:
|
2018-09-14 15:30:31 +00:00
|
|
|
typeStr = "I";
|
2018-07-23 15:21:31 +00:00
|
|
|
break;
|
|
|
|
case QtWarningMsg:
|
2018-09-14 15:30:31 +00:00
|
|
|
typeStr = "W";
|
2018-07-23 15:21:31 +00:00
|
|
|
break;
|
|
|
|
case QtCriticalMsg:
|
2018-09-14 15:30:31 +00:00
|
|
|
typeStr = "C";
|
2018-07-23 15:21:31 +00:00
|
|
|
break;
|
|
|
|
case QtFatalMsg:
|
2018-09-14 15:30:31 +00:00
|
|
|
typeStr = "F";
|
|
|
|
}
|
2018-11-06 16:01:43 +00:00
|
|
|
appendConsoleString(QString("%1 - %2 - [%3] - %4").arg(timestamp, typeStr, context.category, message));
|
2018-09-14 15:30:31 +00:00
|
|
|
if (type == QtFatalMsg) {
|
2018-07-23 15:21:31 +00:00
|
|
|
writeLogsToFile();
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "main.moc"
|
2018-12-18 17:30:26 +00:00
|
|
|
|