diff --git a/src/connection.cpp b/src/connection.cpp index ae7151f..6d33e9a 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -34,7 +34,7 @@ ConnectionLoader::ConnectionLoader(MainWindow* main, Controller* rpc) connD->setupUi(d); auto theme = Settings::getInstance()->get_theme_name(); - qDebug() << theme << "theme " << theme << " has loaded"; + DEBUG("theme " << theme << " has loaded"); auto size = QSize(512,512); if (theme == "Dark" || theme == "Midnight") { @@ -64,6 +64,7 @@ ConnectionLoader::~ConnectionLoader() void ConnectionLoader::loadConnection() { + DEBUG("calling doAutoConnect"); QTimer::singleShot(1, [=]() { this->doAutoConnect(); }); if (!Settings::getInstance()->isHeadless()) d->exec(); @@ -73,6 +74,7 @@ void ConnectionLoader::loadProgress() { bool failed = false; QTimer::singleShot(1, [=]() mutable { + DEBUG("failed=" << failed); // continually retry ShowProgress() until it succeeds // by running without an exception do { @@ -103,24 +105,27 @@ void ConnectionLoader::ShowProgress() isSyncing = new QAtomicInteger(); isSyncing->store(true); - main->logger->write("isSyncing"); - + DEBUG("isSyncing"); + // Do a sync after import syncTimer = new QTimer(main); - main->logger->write("Beginning sync after import wif"); - connection->doRPCWithDefaultErrorHandling("sync", "", [=](auto) { + DEBUG("Beginning sync after import wif"); + connection->doRPC("sync", "", [=](auto) { + DEBUG("finished syncing"); isSyncing->store(false); // Cancel the timer syncTimer->deleteLater(); // When sync is done, set the connection this->doRPCSetConnectionShield(connection); + }, [=](auto) { + DEBUG("sync rpc error! server=" << config->server); }); // While it is syncing, we'll show the status updates while it is alive. QObject::connect(syncTimer, &QTimer::timeout, [=]() { - // Check the sync status + DEBUG("Check the sync status"); if (isSyncing != nullptr && isSyncing->load()) { - // Get the sync status + DEBUG("Get the sync status"); try { connection->doRPC("syncstatus", "", [=](json reply) { if (isSyncing != nullptr && reply.find("synced_blocks") != reply.end()) { @@ -148,10 +153,10 @@ void ConnectionLoader::ShowProgress() } }); - syncTimer->setInterval(1*1000); + int interval = 1*1000; + syncTimer->setInterval(interval); syncTimer->start(); - main->logger->write("Start sync timer"); - + DEBUG("Start sync timer with interval=" << interval); } void ConnectionLoader::doAutoConnect() @@ -159,14 +164,14 @@ void ConnectionLoader::doAutoConnect() auto config = std::shared_ptr(new ConnectionConfig()); config->dangerous = false; config->server = Settings::getInstance()->getSettings().server; - qDebug() << __func__ << " server=" << config->server; + DEBUG(" server=" << config->server); // Initialize the library - main->logger->write(QObject::tr("Attempting to initialize library with ") + config->server); + DEBUG("Attempting to initialize library with "<< config->server); // Check to see if there's an existing wallet if (litelib_wallet_exists(Settings::getDefaultChainName().toStdString().c_str())) { - qDebug() << __func__ << ": using existing wallet"; + DEBUG("using existing wallet"); main->logger->write(QObject::tr("Using existing wallet.")); QString response = ""; @@ -177,7 +182,7 @@ void ConnectionLoader::doAutoConnect() ); response = litelib_process_response(resp); } catch (const std::exception& e) { - qDebug() << __func__ << ": caught an exception, ignoring: " << e.what(); + DEBUG("caught an exception, ignoring: " << e.what()); } if (response.toUpper().trimmed() != "OK") { @@ -190,7 +195,7 @@ void ConnectionLoader::doAutoConnect() ); response = litelib_process_response(resp); } catch (const std::exception& e) { - qDebug() << __func__ << ": caught an exception, ignoring: " << e.what(); + DEBUG("caught an exception, ignoring: " << e.what()); } if (response.toUpper().trimmed() != "OK") { @@ -198,14 +203,14 @@ void ConnectionLoader::doAutoConnect() showError(resp); return; } else { - qDebug() << __func__ << ": Successfully connected to random server: " << config->server << " !!!"; + DEBUG("Successfully connected to random server: " << config->server << " !!!"); } } else { - qDebug() << __func__ << ": Successfully connected to " << config->server << " !!!"; + DEBUG("Successfully connected to " << config->server << " !!!"); } } else { - qDebug() << __func__ << ": no existing wallet"; + DEBUG("no existing wallet"); main->logger->write(QObject::tr("Create/restore wallet.")); createOrRestore(config->dangerous, config->server); d->show(); @@ -216,61 +221,59 @@ void ConnectionLoader::doAutoConnect() qDebug() << __func__ << ": server=" << config->server << " connection=" << connection << " me=" << me << endl; - // After the lib is initialized, try to do get info connection->doRPC("info", "", [=](auto reply) { // If success, set the connection - main->logger->write("Connection is online."); + DEBUG("Connection is online."); connection->setInfo(reply); - main->logger->write("getting Connection reply"); + DEBUG("getting Connection reply"); isSyncing = new QAtomicInteger(); isSyncing->store(true); - main->logger->write("isSyncing"); + DEBUG("isSyncing"); // Do a sync at startup syncTimer = new QTimer(main); - main->logger->write("Beginning sync"); - connection->doRPCWithDefaultErrorHandling("sync", "", [=](auto) { + DEBUG("Beginning sync"); + connection->doRPC("sync", "", [=](auto) { + DEBUG("finished syncing"); isSyncing->store(false); // Cancel the timer syncTimer->deleteLater(); // When sync is done, set the connection this->doRPCSetConnection(connection); + }, [=](auto) { + DEBUG("sync rpc error! server=" << config->server); }); // While it is syncing, we'll show the status updates while it is alive. QObject::connect(syncTimer, &QTimer::timeout, [=]() { - // Check the sync status + DEBUG("Check the sync status"); if (isSyncing != nullptr && isSyncing->load()) { - // Get the sync status - + DEBUG("Getting the sync status"); try { connection->doRPC("syncstatus", "", [=](json reply) { - if (isSyncing != nullptr && reply.find("synced_blocks") != reply.end()) - - { + if (isSyncing != nullptr && reply.find("synced_blocks") != reply.end()) { qint64 synced = reply["synced_blocks"].get(); qint64 total = reply["total_blocks"].get(); me->showInformation( "Syncing... " + QString::number(synced) + " / " + QString::number(total) ); } - }, - [=](QString err) { - qDebug() << "Sync error" << err; - }); - }catch (...) - { - main->logger->write("catch sync progress reply"); - - } + }, + [=](QString err) { + DEBUG("Sync error" << err); + }); + } catch (const std::exception& e) { + DEBUG("caught exception from syncstatus: " << e.what()); + } } }); - syncTimer->setInterval(1* 1000); + int interval = 1*1000; + syncTimer->setInterval(interval); syncTimer->start(); - main->logger->write("Start sync timer"); + DEBUG("Start sync timer with interval=" << interval); }, [=](QString err) { showError(err); @@ -284,13 +287,13 @@ void ConnectionLoader::createOrRestore(bool dangerous, QString server) d->hide(); // Create a wizard FirstTimeWizard wizard(dangerous,server); - main->logger->write("Start new Wallet with FirstimeWizard"); + DEBUG("Start new Wallet with FirstimeWizard"); wizard.exec(); } void ConnectionLoader::doRPCSetConnection(Connection* conn) { - qDebug() << "Connectionloader finished, setting connection"; + DEBUG("Connectionloader finished, setting connection"); main->logger->write("Connectionloader finished, setting connection"); rpc->setConnection(conn); d->accept(); @@ -301,17 +304,16 @@ void ConnectionLoader::doRPCSetConnection(Connection* conn) main->logger->write("Path to Wallet.dat : " ); qDebug() << __func__ << ": wallet path =" << plaintextWallet; plaintextWallet.remove(); - - } catch (...) { - qDebug() << "No plaintext wallet found! file=" << plaintextWallet; + } catch (const std::exception& e) { + DEBUG("Caught exception" << e.what() ); + DEBUG("No plaintext wallet found! file=" << plaintextWallet); main->logger->write("no Plaintext wallet.dat"); } - } void ConnectionLoader::doRPCSetConnectionShield(Connection* conn) { - qDebug() << "Importing finished, setting connection"; + DEBUG("Importing finished, setting connection"); rpc->setConnection(conn); d->accept(); main->getRPC()->shield([=] (auto) {}); @@ -322,9 +324,10 @@ void ConnectionLoader::doRPCSetConnectionShield(Connection* conn) main->logger->write("Path to Wallet.dat : " ); qDebug() << __func__ << ": wallet path =" << plaintextWallet; plaintextWallet.remove(); - } catch (...) { + } catch (const std::exception& e) { + DEBUG("Caught exception" << e.what() ); main->logger->write("no Plaintext wallet.dat"); - qDebug() << "No plaintext wallet found! file=" << plaintextWallet; + DEBUG("No plaintext wallet found! file=" << plaintextWallet); } } @@ -446,11 +449,12 @@ Connection::Connection(MainWindow* m, std::shared_ptr conf) void Connection::doRPC(const QString cmd, const QString args, const std::function& cb, const std::function& errCb) { - if (shutdownInProgress) - // Ignoring RPC because shutdown in progress + if (shutdownInProgress) { + DEBUG("Ignoring RPC because shutdown in progress"); return; + } - qDebug() << __func__ << ": " << cmd; + DEBUG("cmd=" << cmd << " args=" << args); // Create a runner. auto runner = new Executor(cmd, args); @@ -465,7 +469,7 @@ void Connection::doRPC(const QString cmd, const QString args, const std::functio void Connection::doRPCWithDefaultErrorHandling(const QString cmd, const QString args, const std::function& cb) { - qDebug() << __func__ << ": " << cmd; + DEBUG("cmd=" << cmd << " args=" << args); doRPC(cmd, args, cb, [=] (QString err) { this->showTxError(err); }); @@ -473,7 +477,7 @@ void Connection::doRPCWithDefaultErrorHandling(const QString cmd, const QString void Connection::doRPCIgnoreError(const QString cmd, const QString args, const std::function& cb) { - qDebug() << __func__ << ": " << cmd; + DEBUG("cmd=" << cmd << " args=" << args); doRPC(cmd, args, cb, [=] (auto) { // Ignored error handling }); @@ -505,5 +509,6 @@ void Connection::showTxError(const QString& error) */ void Connection::shutdown() { + DEBUG("shutting down"); shutdownInProgress = true; }