From 659d72bced5f252c6a9969125dab743350c1282b Mon Sep 17 00:00:00 2001 From: codestation Date: Sun, 15 Mar 2015 01:53:46 -0430 Subject: [PATCH] Catch SIGINT signal. Show pin, connection, disconnection and database messages if verbose logging is disabled. --- man/qcma_cli.1 | 2 +- src/cli/headlessmanager.cpp | 8 ++++++++ src/cli/headlessmanager.h | 1 + src/cli/main_cli.cpp | 12 +++++++++++- src/cmaclient.cpp | 12 ++++++++++-- src/database.cpp | 3 ++- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/man/qcma_cli.1 b/man/qcma_cli.1 index 140d8e1..d112a11 100644 --- a/man/qcma_cli.1 +++ b/man/qcma_cli.1 @@ -26,7 +26,7 @@ You can control \fBqcma_cli\fR using signals, i.e. using the \fBkill\fR command Refreshes the database. .TP -.B SIGTERM +.B SIGTERM or SIGINT Shuts down the process but waits until the current event is finished. .SH "SEE ALSO" diff --git a/src/cli/headlessmanager.cpp b/src/cli/headlessmanager.cpp index 6b73f41..b4875fe 100644 --- a/src/cli/headlessmanager.cpp +++ b/src/cli/headlessmanager.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -59,6 +60,8 @@ void HeadlessManager::refreshDatabase() return; } + QTextStream(stdout) << "Database scan has started" << endl; + if(!m_db->rescan()) { qDebug("No PS Vita system has been registered"); } @@ -128,6 +131,11 @@ void HeadlessManager::start() } } +void HeadlessManager::receiveMessage(QString message) +{ + QTextStream(stdout) << message << endl; +} + void HeadlessManager::stop() { if(CmaClient::stop() < 0) { diff --git a/src/cli/headlessmanager.h b/src/cli/headlessmanager.h index 39be819..9c21c20 100644 --- a/src/cli/headlessmanager.h +++ b/src/cli/headlessmanager.h @@ -70,6 +70,7 @@ public slots: private slots: void threadStopped(); + void receiveMessage(QString message); }; #endif // HEADLESSMANAGER_H diff --git a/src/cli/main_cli.cpp b/src/cli/main_cli.cpp index 75a21b5..07f37a9 100644 --- a/src/cli/main_cli.cpp +++ b/src/cli/main_cli.cpp @@ -70,12 +70,18 @@ static bool setup_handlers() return false; } + if (sigaction(SIGINT, &term, NULL) != 0) { + qCritical("SIGINT signal handle failed"); + return false; + } + return true; } int main(int argc, char *argv[]) { - if(SingleCoreApplication::sendMessage(QObject::tr("An instance of Qcma is already running"))) { + if(SingleCoreApplication::sendMessage("Another instance of Qcma tried to start")) { + QTextStream(stdout) << "An instance of Qcma is already running" << endl; return 0; } @@ -137,6 +143,10 @@ int main(int argc, char *argv[]) app.setApplicationName("qcma"); HeadlessManager manager; + + // receive the message from another process + QObject::connect(&app, SIGNAL(messageAvailable(QString)), &manager, SLOT(receiveMessage(QString))); + manager.start(); return app.exec(); diff --git a/src/cmaclient.cpp b/src/cmaclient.cpp index 1cde7e6..650b420 100644 --- a/src/cmaclient.cpp +++ b/src/cmaclient.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -132,7 +133,7 @@ void CmaClient::processNewConnection(vita_device_t *device) QMutexLocker locker(&mutex); in_progress = true; - qDebug("Vita connected: id %s", VitaMTP_Get_Identification(device)); + QTextStream(stdout) << "Vita connected, id: " << VitaMTP_Get_Identification(device) << endl; DeviceCapability vita_info; if(!vita_info.exchangeInfo(device)) { @@ -187,7 +188,12 @@ int CmaClient::generatePin(wireless_vita_info_t *info, int *p_err) tempOnlineId = QString(info->name); qDebug("Registration request from %s (MAC: %s)", info->name, info->mac_addr); int pin = rand() % 10000 * 10000 | rand() % 10000; - qDebug("Your registration PIN for %s is: %08d", info->name, pin); + QTextStream out(stdout); + out << "Your registration PIN for " << info->name << " is: "; + out.setFieldWidth(8); + out.setPadChar('0'); + out << pin << endl; + *p_err = 0; emit this_object->receivedPin(info->name, pin); return pin; @@ -240,6 +246,8 @@ void CmaClient::enterEventLoop(vita_device_t *device) int CmaClient::stop() { + QTextStream(stdout) << "Stopping Qcma" << endl; + if(!isActive()) { return -1; } diff --git a/src/database.cpp b/src/database.cpp index d3e0e9e..896ee88 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include const file_type audio_list[] = { @@ -58,7 +59,7 @@ void Database::process() cancel_operation = false; int count = create(); cancel_operation = false; - qDebug("Total entries added to the database: %i", count); + QTextStream(stdout) << "Total entries added to the database: " << count << endl; if(count < 0) { clear(); }