From bc96616492ce642831590dad97acde9942fe7e23 Mon Sep 17 00:00:00 2001 From: Arttu Tarkiainen Date: Tue, 9 Jun 2020 15:43:10 +0300 Subject: Do not print progress indicator when forwarding output to a file Task-number: QTIFW-1770 Change-Id: I2f3a8b440b89328fa3d183b1102d6a6db213fd3b Reviewed-by: Katja Marttila --- src/libs/installer/globals.cpp | 4 ++++ src/libs/installer/globals.h | 1 + src/libs/installer/init.cpp | 24 ++++++++++++++++++++++-- src/libs/installer/progresscoordinator.cpp | 2 +- src/sdk/sdkapp.h | 1 + 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/libs/installer/globals.cpp b/src/libs/installer/globals.cpp index a6453c1d9..a3134dfae 100644 --- a/src/libs/installer/globals.cpp +++ b/src/libs/installer/globals.cpp @@ -63,6 +63,9 @@ const char IFW_PACKAGE_COMPRESSEDSIZE[] = "ifw.package.compressedsize"; const char IFW_PACKAGE_UNCOMPRESSEDSIZE[] = "ifw.package.uncompressedsize"; const char IFW_INSTALLER_INSTALLLOG[] = "ifw.installer.installlog"; +// Internal-only, hidden in --help text +const char IFW_PROGRESS_INDICATOR[] = "ifw.progress.indicator"; + namespace QInstaller { @@ -98,6 +101,7 @@ Q_LOGGING_CATEGORY(lcPackageLicenses, IFW_PACKAGE_LICENSES) Q_LOGGING_CATEGORY(lcPackageUncompressedSize, IFW_PACKAGE_UNCOMPRESSEDSIZE) Q_LOGGING_CATEGORY(lcPackageCompressedSize, IFW_PACKAGE_COMPRESSEDSIZE) Q_LOGGING_CATEGORY(lcInstallerInstallLog, IFW_INSTALLER_INSTALLLOG) +Q_LOGGING_CATEGORY(lcProgressIndicator, IFW_PROGRESS_INDICATOR) QStringList loggingCategories() diff --git a/src/libs/installer/globals.h b/src/libs/installer/globals.h index 37e2fe20b..cdfad1a53 100644 --- a/src/libs/installer/globals.h +++ b/src/libs/installer/globals.h @@ -67,6 +67,7 @@ INSTALLER_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcPackageUncompressedSize) INSTALLER_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcPackageCompressedSize) INSTALLER_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcInstallerInstallLog) INSTALLER_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcInstallerUninstallLog) +INSTALLER_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcProgressIndicator) QStringList INSTALLER_EXPORT loggingCategories(); diff --git a/src/libs/installer/init.cpp b/src/libs/installer/init.cpp index 8a13119b0..3c295d353 100644 --- a/src/libs/installer/init.cpp +++ b/src/libs/installer/init.cpp @@ -1,6 +1,6 @@ /************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Installer Framework. @@ -47,6 +47,7 @@ #include "licenseoperation.h" #include "settingsoperation.h" #include "consumeoutputoperation.h" +#include "globals.h" #include "lib7z_facade.h" #include "utils.h" @@ -59,6 +60,12 @@ #include +#if defined(Q_OS_UNIX) +#include +#elif defined(Q_OS_WIN) +#include +#endif + using namespace KDUpdater; using namespace QInstaller; @@ -69,6 +76,8 @@ static void initResources() } #endif +static bool s_outputRedirected = false; + static QString trimAndPrepend(QtMsgType type, const QString &msg) { QString ba(msg); @@ -112,6 +121,12 @@ void messageHandler(QtMsgType type, const QMessageLogContext &context, const QSt if (msg.contains(QLatin1String("This plugin does not support propagateSizeHints"))) return; + if (context.category == lcProgressIndicator().categoryName()) { + if (!s_outputRedirected) + std::cout << msg.toStdString() << "\r" << std::flush; + return; + } + static Uptime uptime; QString ba = QLatin1Char('[') + QString::number(uptime.elapsed()) + QStringLiteral("] ") @@ -168,5 +183,10 @@ void QInstaller::init() FileDownloaderFactory::setFollowRedirects(true); - qInstallMessageHandler(messageHandler); +#if defined(Q_OS_UNIX) + s_outputRedirected = !isatty(fileno(stdout)); +#elif defined(Q_OS_WIN) + s_outputRedirected = (GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)) == FILE_TYPE_DISK); +#endif + qInstallMessageHandler(messageHandler); } diff --git a/src/libs/installer/progresscoordinator.cpp b/src/libs/installer/progresscoordinator.cpp index a61c13a0e..a00a5bf23 100644 --- a/src/libs/installer/progresscoordinator.cpp +++ b/src/libs/installer/progresscoordinator.cpp @@ -320,7 +320,7 @@ void ProgressCoordinator::printProgressPercentage(int progress) QString formatted = QString::fromLatin1("[%1 %2%] ").arg( m_progressSpinner->spinnerChars.at(m_progressSpinner->currentIndex), QString::number(progress)); - std::cout << formatted.toStdString() << "\r" << std::flush; + qCDebug(QInstaller::lcProgressIndicator).noquote() << formatted; m_progressSpinner->currentIndex == (m_progressSpinner->spinnerChars.size() - 1) ? m_progressSpinner->currentIndex = 0 diff --git a/src/sdk/sdkapp.h b/src/sdk/sdkapp.h index af7468731..2ed99046c 100644 --- a/src/sdk/sdkapp.h +++ b/src/sdk/sdkapp.h @@ -153,6 +153,7 @@ public: loggingRules = QLatin1String("ifw.* = false\n" "ifw.installer.* = true\n" "ifw.server = true\n" + "ifw.progress.indicator = true\n" "ifw.package.name = true\n" "ifw.package.version = true\n" "ifw.package.displayname = true\n"); -- cgit v1.2.3