From a07ecfb49b3fc711fc56016825bebbb01adbc28a Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 20 Nov 2014 18:30:24 +0100 Subject: [PATCH] refs #338 Using CLogPattern for subscribing to log messages in core --- src/swiftcore/main.cpp | 2 +- src/swiftcore/tool.cpp | 124 +++++++++++++++-------------------------- 2 files changed, 46 insertions(+), 80 deletions(-) diff --git a/src/swiftcore/main.cpp b/src/swiftcore/main.cpp index 00136ea55..c3c315b0b 100644 --- a/src/swiftcore/main.cpp +++ b/src/swiftcore/main.cpp @@ -33,7 +33,7 @@ int main(int argc, char *argv[]) // metadata are registered in runtime QApplication a(argc, argv); // not QCoreApplication because of icon, http://qt-project.org/forums/viewthread/15412 CLogHandler::instance()->install(); - CLogHandler::instance()->enableConsoleOutput(false); //! \todo How can I change the level only and display info and above? + CLogHandler::instance()->enableConsoleOutput(false); // default disable QIcon icon(BlackMisc::CIcons::swiftNova24()); QApplication::setWindowIcon(icon); QTextStream cin(stdin); diff --git a/src/swiftcore/tool.cpp b/src/swiftcore/tool.cpp index 315dd4f3e..b166f9691 100644 --- a/src/swiftcore/tool.cpp +++ b/src/swiftcore/tool.cpp @@ -43,6 +43,12 @@ namespace BlackMiscTest Q_ASSERT(runtime); QThread::sleep(3); // let the DBus server startup + // log + //! \todo make thread safe or ..? +// CLogSubscriber messageSubscriber; +// messageSubscriber.enableConsoleOutput(true); +// messageSubscriber.changeSubscription(CLogPattern().withSeverityAtOrAbove(CStatusMessage::SeverityInfo)); + QTextStream qtout(stdout); qtout << "Running on server here " << Tool::getPid() << " thread: " << QThread::currentThreadId() << endl; @@ -53,26 +59,13 @@ namespace BlackMiscTest QString line; while (line != "x" && runtime) { - BlackCore::IContextApplication *applicationContext = runtime->getIContextApplication(); - BlackCore::IContextNetwork *networkContext = runtime->getIContextNetwork(); - BlackCore::IContextAudio *audioContext = runtime->getIContextAudio(); - BlackCore::IContextSettings *settingsContext = runtime->getIContextSettings(); - BlackCore::IContextOwnAircraft *ownAircraftContext = runtime->getIContextOwnAircraft(); - BlackCore::IContextSimulator *simulatorContext = runtime->getIContextSimulator(); - qtout << "-------------" << endl; - qtout << "Connected with network: " << networkContext->isConnected() << endl; + qtout << "Connected with network: " << (runtime->getIContextNetwork()->isConnected() ? "yes" : "no") << endl; qtout << "Thread id: " << QThread::currentThreadId() << endl; qtout << "-------------" << endl; - qtout << "x .. to exit 0 .. settings" << endl; - qtout << "1 .. ATC booked 2 .. ATC online" << endl; - qtout << "3 .. Aircrafts in range 4 .. my aircraft 5 .. voice rooms" << endl; - qtout << "6 .. vatlib audio devices 7 .. Qt audio devices" << endl; - qtout << "-------------" << endl; - qtout << "logging:" << endl; - qtout << "log + context + [e]nabled/[d]isabled" << endl; - qtout << "contexts: app / aud / net / own (aircraft) / set / sim / all" << endl; - qtout << "examples: logappd, lognete, logsimd, logalle" << endl; + qtout << "x .. to exit" << endl; + qtout << "0 .. settings 1 .. vatlib audio devices 2 .. Qt audio devices" << endl; + qtout << "h .. further commands" << endl; qtout << "-------------" << endl; line = qtin.readLine().toLower().trimmed(); @@ -80,85 +73,40 @@ namespace BlackMiscTest { qtout << "-------------" << endl; QString ret1; - QMetaObject::invokeMethod(settingsContext, "getSettingsFileName", + QMetaObject::invokeMethod(runtime->getIContextSettings(), "getSettingsFileName", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QString, ret1)); qtout << "Settings: " << ret1 << endl; - QMetaObject::invokeMethod(settingsContext, "getSettingsAsJsonString", + QMetaObject::invokeMethod(runtime->getIContextSettings(), "getSettingsAsJsonString", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QString, ret1)); qtout << "JSON " << ret1 << endl; } else if (line.startsWith("1")) - { - // remarks: use fully qualified name in Q_RETURN_ARG - qtout << "-------------" << endl; - qtout << "ATC booked" << endl; - CAtcStationList stations; - QMetaObject::invokeMethod(networkContext, "getAtcStationsBooked", - Qt::BlockingQueuedConnection, - Q_RETURN_ARG(BlackMisc::Aviation::CAtcStationList, stations)); - qtout << stations << endl; - } - else if (line.startsWith("2")) - { - qtout << "-------------" << endl; - qtout << "ATC online" << endl; - CAtcStationList stations; - QMetaObject::invokeMethod(networkContext, "getAtcStationsOnline", - Qt::BlockingQueuedConnection, - Q_RETURN_ARG(BlackMisc::Aviation::CAtcStationList, stations)); - qtout << stations << endl; - } - else if (line.startsWith("3")) - { - qtout << "-------------" << endl; - qtout << "aircrafts in range" << endl; - CAircraftList aircrafts; - QMetaObject::invokeMethod(networkContext, "getAircraftsInRange", - Qt::BlockingQueuedConnection, - Q_RETURN_ARG(BlackMisc::Aviation::CAircraftList, aircrafts)); - qtout << aircrafts << endl; - } - else if (line.startsWith("4")) - { - qtout << "-------------" << endl; - qtout << "my aircraft" << endl; - CAircraft aircraft; - QMetaObject::invokeMethod(ownAircraftContext, "getOwnAircraft", - Qt::BlockingQueuedConnection, - Q_RETURN_ARG(BlackMisc::Aviation::CAircraft, aircraft)); - qtout << aircraft << endl; - - } - else if (line.startsWith("5")) - { - qtout << "-------------" << endl; - qtout << "voice rooms" << endl; - CVoiceRoomList voiceRooms; - QMetaObject::invokeMethod(audioContext, "getComVoiceRooms", - Qt::BlockingQueuedConnection, - Q_RETURN_ARG(BlackMisc::Audio::CVoiceRoomList, voiceRooms)); - qtout << voiceRooms << endl; - } - else if (line.startsWith("6")) { qtout << "-------------" << endl; qtout << "vatlib audio devices" << endl; CAudioDeviceList devices; - QMetaObject::invokeMethod(audioContext, "getAudioDevices", + QMetaObject::invokeMethod(runtime->getIContextAudio(), "getAudioDevices", Qt::BlockingQueuedConnection, Q_RETURN_ARG(BlackMisc::Audio::CAudioDeviceList, devices)); qtout << devices << endl; } - else if (line.startsWith("7")) + else if (line.startsWith("2")) { qtout << "-------------" << endl; qtout << "Qt audio devices" << endl; BlackSound::CSoundGenerator::printAllQtSoundDevices(); } + else if (line.startsWith("level")) + { + line = line.replace("level", "").trimmed(); + CStatusMessage::StatusSeverity severity = CStatusMessage::stringToSeverity(line); + // messageSubscriber.changeSubscription(CLogPattern().withSeverityAtOrAbove(severity)); + qtout << "Changed level to" << CStatusMessage::severityToString(severity); + } else if (line.startsWith("log")) { line.replace("log", ""); @@ -170,12 +118,12 @@ namespace BlackMiscTest else { CLogCategoryList categories; - if (line.startsWith("app")) categories = applicationContext; - else if (line.startsWith("aud")) categories = audioContext; - else if (line.startsWith("net")) categories = networkContext; - else if (line.startsWith("own")) categories = ownAircraftContext; - else if (line.startsWith("set")) categories = settingsContext; - else if (line.startsWith("sim")) categories = simulatorContext; + if (line.startsWith("app")) { categories = runtime->getIContextApplication(); } + else if (line.startsWith("aud")) { categories = runtime->getIContextAudio(); } + else if (line.startsWith("net")) { categories = runtime->getIContextNetwork(); } + else if (line.startsWith("own")) { categories = runtime->getIContextOwnAircraft(); } + else if (line.startsWith("set")) { categories = runtime->getIContextSettings(); } + else if (line.startsWith("sim")) { categories = runtime->getIContextSimulator(); } if (! categories.isEmpty()) { BlackMisc::singleShot(0, BlackMisc::CLogHandler::instance()->thread(), [ = ]() @@ -185,6 +133,24 @@ namespace BlackMiscTest } } } + else if (line.startsWith("h")) + { + qtout << "1) global logging:" << endl; + qtout << " levels: d, i, w, e example: level d for debug level" << endl; + qtout << "2) logging for contexts:" << endl; + qtout << " log + context + [e]nabled / [d]isabled" << endl; + qtout << " contexts: app / aud / net / own (aircraft) / set / sim / all" << endl; + qtout << " examples: logappd, lognete, logsimd, logalle" << endl; + qtout << "3) all . commands can be used, e.g. .com1 127.35" << endl; + qtout << endl; + } + else if (line.startsWith(".")) + { + bool c = runtime->parseCommandLine(line); + if (c) { qtout << "Handled command " << line; } + else { qtout << "Not handled " << line; } + qtout << endl; + } } QCoreApplication::quit(); }