summaryrefslogtreecommitdiffstats
path: root/qdb/server
diff options
context:
space:
mode:
Diffstat (limited to 'qdb/server')
-rw-r--r--qdb/server/hostserver.cpp26
-rw-r--r--qdb/server/hostserver.h3
-rw-r--r--qdb/server/logging.cpp102
-rw-r--r--qdb/server/logging.h26
-rw-r--r--qdb/server/usb-host/usbdeviceenumerator.cpp1
5 files changed, 156 insertions, 2 deletions
diff --git a/qdb/server/hostserver.cpp b/qdb/server/hostserver.cpp
index f70a3e3..36898f0 100644
--- a/qdb/server/hostserver.cpp
+++ b/qdb/server/hostserver.cpp
@@ -22,6 +22,7 @@
#include "libqdb/interruptsignalhandler.h"
#include "libqdb/qdbconstants.h"
+#include "logging.h"
#include <QtCore/qcoreapplication.h>
#include <QtCore/qcommandlineparser.h>
@@ -36,8 +37,10 @@
#include <QtNetwork/qlocalserver.h>
#include <QtNetwork/qlocalsocket.h>
-int hostServer(QCoreApplication &app, const QCommandLineParser &parser)
+int execHostServer(const QCoreApplication &app, const QCommandLineParser &parser)
{
+ setupLogging();
+
QString filterRules;
if (!parser.isSet("debug-transport"))
filterRules.append("transport=false\n");
@@ -86,6 +89,7 @@ void HostServer::listen()
void HostServer::close()
{
+ qDebug() << "Shutting QDB host server down";
m_localServer.close();
emit closed();
}
@@ -127,6 +131,8 @@ void HostServer::handleRequest()
if (requestObject["request"] == "devices") {
replyDeviceInformation();
+ } else if (requestObject["request"] == "stop-server") {
+ stopServer();
} else {
qWarning() << "Got invalid request from client:" << requestBytes;
m_client->disconnectFromServer();
@@ -158,3 +164,21 @@ void HostServer::replyDeviceInformation()
m_client->disconnectFromServer();
qDebug() << "Replied device information to the client";
}
+
+void HostServer::stopServer()
+{
+ QJsonObject obj;
+ obj["response"] = "stopping";
+
+ const QByteArray response = QJsonDocument{obj}.toJson(QJsonDocument::Compact);
+
+ if (!m_client || !m_client->isWritable()) {
+ qWarning() << "Could not reply to the client";
+ return;
+ }
+ m_client->write(response);
+ m_client->waitForBytesWritten();
+ m_client->disconnectFromServer();
+ qDebug() << "Acknowledged stopping";
+ close();
+}
diff --git a/qdb/server/hostserver.h b/qdb/server/hostserver.h
index 007c8ff..d6b161e 100644
--- a/qdb/server/hostserver.h
+++ b/qdb/server/hostserver.h
@@ -31,7 +31,7 @@ class QCoreApplication;
class QCommandLineParser;
QT_END_NAMESPACE
-int hostServer(QCoreApplication &app, const QCommandLineParser &parser);
+int execHostServer(const QCoreApplication &app, const QCommandLineParser &parser);
class HostServer : public QObject
{
@@ -55,6 +55,7 @@ private slots:
private:
void replyDeviceInformation();
+ void stopServer();
QLocalServer m_localServer;
QLocalSocket *m_client; // owned by this class, deleted in handleDisconnection()
diff --git a/qdb/server/logging.cpp b/qdb/server/logging.cpp
new file mode 100644
index 0000000..6566537
--- /dev/null
+++ b/qdb/server/logging.cpp
@@ -0,0 +1,102 @@
+/******************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the Qt Debug Bridge.
+**
+** $QT_BEGIN_LICENSE:COMM$
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** $QT_END_LICENSE$
+**
+******************************************************************************/
+#include "logging.h"
+
+#include <QtCore/qdatetime.h>
+#include <QtCore/qdebug.h>
+#include <QtCore/qdir.h>
+#include <QtCore/qfile.h>
+#include <QtCore/qstandardpaths.h>
+#include <QtGlobal>
+
+static QFile logFile;
+
+void hostServerMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
+{
+ Q_UNUSED(context);
+ if (!logFile.isWritable()) {
+ if (!logFile.open(QFile::WriteOnly | QIODevice::Unbuffered)) {
+ // Fall back to default handler
+ qInstallMessageHandler(nullptr);
+ qCritical() << "Could not open log file" << logFile.fileName();
+ return;
+ } else {
+ QString startMessage{"-- Starting QDB host server log on %1 --\n"};
+ startMessage = startMessage.arg(QDateTime::currentDateTime().toString(Qt::ISODate));
+ logFile.write(startMessage.toUtf8());
+ }
+ }
+
+ QString prefix;
+ switch (type) {
+ case QtDebugMsg:
+ prefix = "D:";
+ break;
+ case QtInfoMsg:
+ prefix = "I:";
+ break;
+ case QtWarningMsg:
+ prefix = "W:";
+ break;
+ case QtCriticalMsg:
+ prefix = "C:";
+ break;
+ case QtFatalMsg:
+ prefix = "F:";
+ break;
+ }
+
+ auto fullMsg = QString{"%1 %2\n"}.arg(prefix).arg(msg).toUtf8();
+ auto written = logFile.write(fullMsg);
+ if (written != fullMsg.size()) {
+ qInstallMessageHandler(nullptr);
+ qCritical() << "Could not write into log file" << logFile.fileName()
+ << ":" << logFile.errorString();
+ }
+
+ if (type == QtFatalMsg)
+ abort();
+}
+
+void setupLogging()
+{
+ if (qgetenv("QDB_LOGGING_TO_CONSOLE") != "1") {
+ auto dataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
+ if (!dataLocation.isEmpty()) {
+ QDir dataDir{dataLocation};
+ bool dirAvailable;
+ if (dataDir.exists())
+ dirAvailable = true;
+ else
+ dirAvailable = dataDir.mkpath(".");
+
+ if (dirAvailable) {
+ logFile.setFileName(dataLocation + "/qdb.log");
+ qInstallMessageHandler(hostServerMessageHandler);
+ } else {
+ qWarning() << "Application data location" << dataLocation
+ << "was not possible to log in, logging to console";
+ }
+ } else {
+ qWarning() << "Could not find writable application data location, logging to console";
+ }
+ }
+}
diff --git a/qdb/server/logging.h b/qdb/server/logging.h
new file mode 100644
index 0000000..dfcb23b
--- /dev/null
+++ b/qdb/server/logging.h
@@ -0,0 +1,26 @@
+/******************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the Qt Debug Bridge.
+**
+** $QT_BEGIN_LICENSE:COMM$
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** $QT_END_LICENSE$
+**
+******************************************************************************/
+#ifndef QDB_LOGGING_H
+#define QDB_LOGGING_H
+
+void setupLogging();
+
+#endif // QDB_LOGGING_H
diff --git a/qdb/server/usb-host/usbdeviceenumerator.cpp b/qdb/server/usb-host/usbdeviceenumerator.cpp
index 8a6d823..7095db8 100644
--- a/qdb/server/usb-host/usbdeviceenumerator.cpp
+++ b/qdb/server/usb-host/usbdeviceenumerator.cpp
@@ -184,6 +184,7 @@ void UsbDeviceEnumerator::startMonitoring()
{
QObject::connect(&m_pollTimer, &QTimer::timeout, this, &UsbDeviceEnumerator::pollQdbDevices);
m_pollTimer.start(1000);
+ pollQdbDevices();
}
void UsbDeviceEnumerator::stopMonitoring()