From 4ace963c80a246ad10c4e4da226d9f71da4cddcb Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 4 Sep 2016 05:51:54 +1200 Subject: [PATCH] Show important console messages on metrics screen --- src/init.cpp | 1 + src/metrics.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ src/metrics.h | 1 + 3 files changed, 87 insertions(+) diff --git a/src/init.cpp b/src/init.cpp index a24a1782..6a9eb6f1 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -978,6 +978,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) if (GetBoolArg("-showmetrics", true) && !fPrintToConsole && !GetBoolArg("-daemon", false)) { // Start the persistent metrics interface + ConnectMetricsScreen(); threadGroup.create_thread(&ThreadShowMetricsScreen); } diff --git a/src/metrics.cpp b/src/metrics.cpp index ad58ab41..b835ddfa 100644 --- a/src/metrics.cpp +++ b/src/metrics.cpp @@ -5,15 +5,95 @@ #include "metrics.h" #include "chainparams.h" +#include "ui_interface.h" #include "util.h" #include "utiltime.h" #include +#include +#include AtomicCounter transactionsValidated; AtomicCounter ehSolverRuns; AtomicCounter minedBlocks; +boost::synchronized_value> messageBox; +boost::synchronized_value initMessage; +bool loaded = false; + +static bool metrics_ThreadSafeMessageBox(const std::string& message, + const std::string& caption, + unsigned int style) +{ + std::string strCaption; + // Check for usage of predefined caption + switch (style) { + case CClientUIInterface::MSG_ERROR: + strCaption += _("Error"); + break; + case CClientUIInterface::MSG_WARNING: + strCaption += _("Warning"); + break; + case CClientUIInterface::MSG_INFORMATION: + strCaption += _("Information"); + break; + default: + strCaption += caption; // Use supplied caption (can be empty) + } + + boost::strict_lock_ptr> u = messageBox.synchronize(); + u->push_back(strCaption + ": " + message); + if (u->size() > 5) { + u->pop_back(); + } +} + +static void metrics_InitMessage(const std::string& message) +{ + *initMessage = message; +} + +void ConnectMetricsScreen() +{ + uiInterface.ThreadSafeMessageBox.disconnect_all_slots(); + uiInterface.ThreadSafeMessageBox.connect(metrics_ThreadSafeMessageBox); + uiInterface.InitMessage.disconnect_all_slots(); + uiInterface.InitMessage.connect(metrics_InitMessage); +} + +int printMessageBox() +{ + boost::strict_lock_ptr> u = messageBox.synchronize(); + + if (u->size() == 0) { + return 0; + } + + std::cout << std::endl; + std::cout << "Messages:" << std::endl; + for (auto it = u->cbegin(); it != u->cend(); ++it) { + std::cout << *it << std::endl; + } + return 2 + u->size(); +} + +int printInitMessage() +{ + if (loaded) { + return 0; + } + + std::string msg = *initMessage; + std::cout << std::endl; + std::cout << "Init message: " << msg << std::endl; + + if (msg == "Done loading") { + loaded = true; + } + + return 2; +} + void ThreadShowMetricsScreen() { // Make this thread recognisable as the metrics screen thread @@ -53,6 +133,7 @@ void ThreadShowMetricsScreen() int64_t nStart = GetTime(); while (true) { + // Number of lines that are always displayed int lines = 4; // Erase below current position @@ -91,6 +172,10 @@ void ThreadShowMetricsScreen() } } + // Messages + lines += printMessageBox(); + lines += printInitMessage(); + // Explain how to exit std::cout << std::endl; std::cout << "[Hit Ctrl+C to exit] [Set 'showmetrics=0' to hide]" << std::endl;; diff --git a/src/metrics.h b/src/metrics.h index f1fa13d5..a9713f0e 100644 --- a/src/metrics.h +++ b/src/metrics.h @@ -27,6 +27,7 @@ extern AtomicCounter transactionsValidated; extern AtomicCounter ehSolverRuns; extern AtomicCounter minedBlocks; +void ConnectMetricsScreen(); void ThreadShowMetricsScreen(); /**