From 6726a926a439fc1ef37e9e3c2809613fd6a5e3fb Mon Sep 17 00:00:00 2001 From: Anubhav Choudhary Date: Mon, 29 Mar 2021 12:44:34 -0600 Subject: [PATCH] [logUpload] Configurable upload size limit A key 'sizeLimit' added to uploadServer field in branding.desc to limit the size of logFile to upload. --- src/branding/default/branding.desc | 18 +++++++++++------- src/libcalamaresui/Branding.cpp | 6 ++++-- src/libcalamaresui/Branding.h | 2 +- src/libcalamaresui/ViewManager.cpp | 2 +- src/libcalamaresui/utils/Paste.cpp | 18 +++++++++++------- src/libcalamaresui/utils/TestPaste.cpp | 6 +++--- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/branding/default/branding.desc b/src/branding/default/branding.desc index 90f92b5f1..98e887e76 100644 --- a/src/branding/default/branding.desc +++ b/src/branding/default/branding.desc @@ -220,13 +220,17 @@ slideshowAPI: 2 # These options are to customize online uploading of logs to pastebins: -# - type : Defines the kind of pastebin service to be used. Currently -# it accepts two values: -# - none : disables the pastebin functionality -# - fiche : use fiche pastebin server -# - url : Defines the address of pastebin service to be used. -# Takes string as input. Important bits are the host and port, -# the scheme is not used. +# - type : Defines the kind of pastebin service to be used. Currently +# it accepts two values: +# - none : disables the pastebin functionality +# - fiche : use fiche pastebin server +# - url : Defines the address of pastebin service to be used. +# Takes string as input. Important bits are the host and port, +# the scheme is not used. +# - sizeLimit : Defines maximum size limit (in KiB) of log file to be pasted. +# Takes integer as input. If <=0, no limit will be forced, +# else only last 'n' KiB of log file will be pasted. uploadServer : type : "fiche" url : "http://termbin.com:9999" + sizeLimit : 20 diff --git a/src/libcalamaresui/Branding.cpp b/src/libcalamaresui/Branding.cpp index 3668c0b4b..c074b7403 100644 --- a/src/libcalamaresui/Branding.cpp +++ b/src/libcalamaresui/Branding.cpp @@ -153,15 +153,17 @@ uploadServerFromMap( const QVariantMap& map ) QString typestring = map[ "type" ].toString(); QString urlstring = map[ "url" ].toString(); + qint64 sizeLimit = map[ "sizeLimit" ].toLongLong(); if ( typestring.isEmpty() || urlstring.isEmpty() ) { - return Branding::UploadServerInfo( Branding::UploadServerType::None, QUrl() ); + return Branding::UploadServerInfo( Branding::UploadServerType::None, QUrl(), -1 ); } bool bogus = false; // we don't care about type-name lookup success here return Branding::UploadServerInfo( names.find( typestring, bogus ), - QUrl( urlstring, QUrl::ParsingMode::StrictMode ) ); + QUrl( urlstring, QUrl::ParsingMode::StrictMode ), + sizeLimit ); } /** @brief Load the @p map with strings from @p config diff --git a/src/libcalamaresui/Branding.h b/src/libcalamaresui/Branding.h index 831b2adec..f36d3c58c 100644 --- a/src/libcalamaresui/Branding.h +++ b/src/libcalamaresui/Branding.h @@ -226,7 +226,7 @@ public: * This is both the type (which may be none, in which case the URL * is irrelevant and usually empty) and the URL for the upload. */ - using UploadServerInfo = QPair< UploadServerType, QUrl >; + using UploadServerInfo = std::tuple< UploadServerType, QUrl, qint64 >; UploadServerInfo uploadServer() const { return m_uploadServer; } /** diff --git a/src/libcalamaresui/ViewManager.cpp b/src/libcalamaresui/ViewManager.cpp index 704655c8b..6e0240157 100644 --- a/src/libcalamaresui/ViewManager.cpp +++ b/src/libcalamaresui/ViewManager.cpp @@ -144,7 +144,7 @@ void ViewManager::onInstallationFailed( const QString& message, const QString& details ) { bool shouldOfferWebPaste - = Calamares::Branding::instance()->uploadServer().first != Calamares::Branding::UploadServerType::None; + = std::get<2>(Calamares::Branding::instance()->uploadServer()) != Calamares::Branding::UploadServerType::None; cError() << "Installation failed:" << message; cDebug() << Logger::SubEntry << "- message:" << message; diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index 40e314108..779a8aca0 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -30,7 +30,7 @@ using namespace CalamaresUtils::Units; * Returns an empty QByteArray() on any kind of error. */ STATICTEST QByteArray -logFileContents() +logFileContents( qint64 sizeLimit ) { const QString name = Logger::logFile(); QFile pasteSourceFile( name ); @@ -40,11 +40,15 @@ logFileContents() return QByteArray(); } QFileInfo fi( pasteSourceFile ); - if ( fi.size() > 16_KiB ) + sizeLimit *= 1024; //For KiB to bytes + cDebug() << "Log upload size limit was set to " << sizeLimit << " bytes"; + if ( fi.size() > sizeLimit and sizeLimit > 0 ) { - pasteSourceFile.seek( fi.size() - 16_KiB ); + // Fixme : this following line is not getting pasted + cDebug() << "Only last " << sizeLimit << " bytes of log file (" << fi.size() << ") uploaded" ; + pasteSourceFile.seek( fi.size() - sizeLimit ); } - return pasteSourceFile.read( 16_KiB ); + return pasteSourceFile.read( sizeLimit ); } @@ -101,7 +105,7 @@ ficheLogUpload( const QByteArray& pasteData, const QUrl& serverUrl, QObject* par QString CalamaresUtils::Paste::doLogUpload( QObject* parent ) { - auto [ type, serverUrl ] = Calamares::Branding::instance()->uploadServer(); + auto [ type, serverUrl, sizeLimit ] = Calamares::Branding::instance()->uploadServer(); if ( !serverUrl.isValid() ) { cWarning() << "Upload configure with invalid URL"; @@ -113,7 +117,7 @@ CalamaresUtils::Paste::doLogUpload( QObject* parent ) return QString(); } - QByteArray pasteData = logFileContents(); + QByteArray pasteData = logFileContents( sizeLimit ); if ( pasteData.isEmpty() ) { // An error has already been logged @@ -165,6 +169,6 @@ CalamaresUtils::Paste::doLogUploadUI( QWidget* parent ) bool CalamaresUtils::Paste::isEnabled() { - auto [ type, serverUrl ] = Calamares::Branding::instance()->uploadServer(); + auto [ type, serverUrl, sizeLimit ] = Calamares::Branding::instance()->uploadServer(); return type != Calamares::Branding::UploadServerType::None; } diff --git a/src/libcalamaresui/utils/TestPaste.cpp b/src/libcalamaresui/utils/TestPaste.cpp index d21d6b81e..da55395e0 100644 --- a/src/libcalamaresui/utils/TestPaste.cpp +++ b/src/libcalamaresui/utils/TestPaste.cpp @@ -16,7 +16,7 @@ #include #include -extern QByteArray logFileContents(); +extern QByteArray logFileContents( qint64 sizeLimit ); extern QString ficheLogUpload( const QByteArray& pasteData, const QUrl& serverUrl, QObject* parent ); class TestPaste : public QObject @@ -37,13 +37,13 @@ TestPaste::testGetLogFile() { QFile::remove( Logger::logFile() ); // This test assumes nothing **else** has set up logging yet - QByteArray contentsOfLogfileBefore = logFileContents(); + QByteArray contentsOfLogfileBefore = logFileContents( 16 ); QVERIFY( contentsOfLogfileBefore.isEmpty() ); Logger::setupLogLevel( Logger::LOGDEBUG ); Logger::setupLogfile(); - QByteArray contentsOfLogfileAfterSetup = logFileContents(); + QByteArray contentsOfLogfileAfterSetup = logFileContents( 16 ); QVERIFY( !contentsOfLogfileAfterSetup.isEmpty() ); }