From 844831751d8b63157425471a90322026ed075cb8 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 9 Mar 2021 14:30:20 +0100 Subject: [PATCH] [libcalamaresui] Factor out the reading of the log file - this will be needed for other pastebins, too --- src/libcalamaresui/utils/Paste.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/libcalamaresui/utils/Paste.cpp b/src/libcalamaresui/utils/Paste.cpp index ce03a77e3..d2ef54a92 100644 --- a/src/libcalamaresui/utils/Paste.cpp +++ b/src/libcalamaresui/utils/Paste.cpp @@ -20,6 +20,23 @@ #include #include +/** @brief Reads the logfile, returns its contents. + * + * Returns an empty QByteArray() on any kind of error. + */ +static QByteArray +logFileContents() +{ + QFile pasteSourceFile( Logger::logFile() ); + if ( !pasteSourceFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) + { + cError() << "Could not open log file"; + return QByteArray(); + } + // TODO: read the **last** 16kiB? + return pasteSourceFile.read( 16384 /* bytes */ ); +} + namespace CalamaresUtils { @@ -32,25 +49,18 @@ QStringList UploadServersList = { QString ficheLogUpload( QObject* parent ) { - const QString& ficheHost = Calamares::Branding::instance()->uploadServer( Calamares::Branding::URL ); quint16 fichePort = Calamares::Branding::instance()->uploadServer( Calamares::Branding::Port ).toInt(); QString pasteUrlFmt = parent->tr( "Install log posted to\n\n%1\n\nLink copied to clipboard" ); - QFile pasteSourceFile( Logger::logFile() ); - if ( !pasteSourceFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) + QByteArray pasteData = logFileContents(); + if ( pasteData.isEmpty() ) { - cError() << "Could not open log file"; + // An error has already been logged return QString(); } - QByteArray pasteData; - while ( !pasteSourceFile.atEnd() ) - { - pasteData += pasteSourceFile.readLine(); - } - QTcpSocket* socket = new QTcpSocket( parent ); socket->connectToHost( ficheHost, fichePort );