Merge branch 'issue-1613' into calamares

This commit is contained in:
Adriaan de Groot 2021-01-28 01:07:03 +01:00
commit db5f3bc309
9 changed files with 199 additions and 17 deletions

View File

@ -261,12 +261,12 @@ PythonJob::exec()
{
m_description.truncate( i_newline );
}
cDebug() << "Job description from __doc__" << prettyName() << '=' << m_description;
cDebug() << Logger::SubEntry << "Job description from __doc__" << prettyName() << '=' << m_description;
}
}
else
{
cDebug() << "Job description from pretty_name" << prettyName() << '=' << m_description;
cDebug() << Logger::SubEntry << "Job description from pretty_name" << prettyName() << '=' << m_description;
}
emit progress( 0 );

View File

@ -188,7 +188,7 @@ System::runCommand( System::RunLocation location,
: -1 ) )
{
cWarning() << "Process" << args.first() << "timed out after" << timeoutSec.count() << "s. Output so far:\n"
<< Logger::NoQuote {} << process.readAllStandardOutput();
<< Logger::NoQuote << process.readAllStandardOutput();
return ProcessResult::Code::TimedOut;
}
@ -196,7 +196,7 @@ System::runCommand( System::RunLocation location,
if ( process.exitStatus() == QProcess::CrashExit )
{
cWarning() << "Process" << args.first() << "crashed. Output so far:\n" << Logger::NoQuote {} << output;
cWarning() << "Process" << args.first() << "crashed. Output so far:\n" << Logger::NoQuote << output;
return ProcessResult::Code::Crashed;
}
@ -206,7 +206,7 @@ System::runCommand( System::RunLocation location,
{
if ( showDebug && !output.isEmpty() )
{
cDebug() << Logger::SubEntry << "Finished. Exit code:" << r << "output:\n" << Logger::NoQuote {} << output;
cDebug() << Logger::SubEntry << "Finished. Exit code:" << r << "output:\n" << Logger::NoQuote << output;
}
else
{
@ -218,7 +218,7 @@ System::runCommand( System::RunLocation location,
if ( !output.isEmpty() )
{
cDebug() << Logger::SubEntry << "Target cmd:" << RedactedList( args ) << "Exit code:" << r << "output:\n"
<< Logger::NoQuote {} << output;
<< Logger::NoQuote << output;
}
else
{

View File

@ -207,6 +207,8 @@ constexpr FuncSuppressor::FuncSuppressor( const char s[] )
const constexpr FuncSuppressor Continuation( s_Continuation );
const constexpr FuncSuppressor SubEntry( s_SubEntry );
const constexpr NoQuote_t NoQuote {};
const constexpr Quote_t Quote {};
QString
toString( const QVariant& v )

View File

@ -25,15 +25,17 @@ struct FuncSuppressor
const char* m_s;
};
struct NoQuote
struct NoQuote_t
{
};
struct Quote
struct Quote_t
{
};
DLLEXPORT extern const FuncSuppressor Continuation;
DLLEXPORT extern const FuncSuppressor SubEntry;
DLLEXPORT extern const NoQuote_t NoQuote;
DLLEXPORT extern const Quote_t Quote;
enum
{
@ -74,13 +76,13 @@ operator<<( QDebug& s, const FuncSuppressor& f )
}
inline QDebug&
operator<<( QDebug& s, const NoQuote& )
operator<<( QDebug& s, const NoQuote_t& )
{
return s.noquote().nospace();
}
inline QDebug&
operator<<( QDebug& s, const Quote& )
operator<<( QDebug& s, const Quote_t& )
{
return s.quote().space();
}
@ -254,7 +256,7 @@ operator<<( QDebug& s, const DebugMap& t )
inline QDebug&
operator<<( QDebug& s, const Pointer& p )
{
s << NoQuote {} << '@' << p.ptr << Quote {};
s << NoQuote << '@' << p.ptr << Quote;
return s;
}
} // namespace Logger

View File

@ -15,6 +15,7 @@
*/
#include "String.h"
#include "Logger.h"
#include <QStringList>
@ -121,4 +122,60 @@ obscure( const QString& string )
return result;
}
QString
truncateMultiLine( const QString& string, CalamaresUtils::LinesStartEnd lines, CalamaresUtils::CharCount chars )
{
const int maxLines = lines.atStart + lines.atEnd;
if ( maxLines < 1 )
{
QString shorter( string );
shorter.truncate( chars.total );
return shorter;
}
if ( ( string.length() <= chars.total ) && ( string.count( '\n' ) <= maxLines ) )
{
return string;
}
QString shorter = string;
QString front, back;
if ( shorter.count( '\n' ) >= maxLines )
{
int from = -1;
for ( int i = 0; i < lines.atStart; ++i )
{
from = shorter.indexOf( '\n', from + 1 );
if ( from < 0 )
{
// That's strange, we counted at least maxLines newlines before
break;
}
}
if ( from > 0 )
{
front = shorter.left( from + 1 );
}
int lastNewLine = -1;
int lastCount = shorter.endsWith( '\n' ) ? -1 : 0;
for ( auto i = shorter.rbegin(); i != shorter.rend() && lastCount < lines.atEnd; ++i )
{
if ( *i == '\n' )
{
++lastCount;
lastNewLine = int( i - shorter.rbegin() );
}
}
if ( ( lastNewLine >= 0 ) && ( lastCount >= lines.atEnd ) )
{
back = shorter.right( lastNewLine );
}
}
return front + back;
}
} // namespace CalamaresUtils

View File

@ -61,6 +61,43 @@ DLLEXPORT QString removeDiacritics( const QString& string );
* @return the obfuscated string.
*/
DLLEXPORT QString obscure( const QString& string );
/** @brief Parameter for counting lines at beginning and end of string
*
* This is used by truncateMultiLine() to indicate how many lines from
* the beginning and how many from the end should be kept.
*/
struct LinesStartEnd
{
int atStart = 0;
int atEnd = 0;
};
/** @brief Parameter for counting characters in truncateMultiLine()
*/
struct CharCount
{
int total = 0;
};
/** @brief Truncate a string to some reasonable length for display
*
* Keep the first few, or last few (or both) lines of a possibly lengthy
* message @p string and reduce it to a displayable size (e.g. for
* pop-up windows that display the message). If the message is longer
* than @p chars, then characters are removed from the front (if
* @p lines.atStart is zero) or end (if @p lines.atEnd is zero) or in the middle
* (if both are nonzero).
*
* @param string the input string.
* @param lines number of lines to preserve.
* @param chars maximum number of characters in the returned string.
* @return a string built from parts of the input string.
*/
DLLEXPORT QString truncateMultiLine( const QString& string,
LinesStartEnd lines = LinesStartEnd { 3, 5 },
CharCount chars = CharCount { 812 } );
} // namespace CalamaresUtils
#endif

View File

@ -13,6 +13,7 @@
#include "Entropy.h"
#include "Logger.h"
#include "RAII.h"
#include "String.h"
#include "Traits.h"
#include "UMask.h"
#include "Variant.h"
@ -63,6 +64,8 @@ private Q_SLOTS:
void testVariantStringListYAMLDashed();
void testVariantStringListYAMLBracketed();
/** @brief Test smart string truncation. */
void testStringTruncation();
private:
void recursiveCompareMap( const QVariantMap& a, const QVariantMap& b, int depth );
@ -495,6 +498,87 @@ strings: [ aap, noot, mies ]
QVERIFY( !getStringList( m, key ).contains( "lam" ) );
}
void
LibCalamaresTests::testStringTruncation()
{
Logger::setupLogLevel( Logger::LOGDEBUG );
using namespace CalamaresUtils;
const QString longString( R"(---
--- src/libcalamares/utils/String.h
+++ src/libcalamares/utils/String.h
@@ -62,15 +62,22 @@ DLLEXPORT QString removeDiacritics( const QString& string );
*/
DLLEXPORT QString obscure( const QString& string );
+/** @brief Parameter for counting lines at beginning and end of string
+ *
+ * This is used by truncateMultiLine() to indicate how many lines from
+ * the beginning and how many from the end should be kept.
+ */
struct LinesStartEnd
{
- int atStart;
- int atEnd;
+ int atStart = 0;
+ int atEnd = 0;
)" );
const int sufficientLength = 812;
// There's 18 lines in all
QCOMPARE( longString.count( '\n' ), 18 );
QVERIFY( longString.length() < sufficientLength );
// If we ask for more, we get everything back
QCOMPARE( longString, truncateMultiLine( longString, LinesStartEnd { 20, 0 }, CharCount { sufficientLength } ) );
QCOMPARE( longString, truncateMultiLine( longString, LinesStartEnd { 0, 20 }, CharCount { sufficientLength } ) );
// If we ask for no lines, only characters, we get that
{
auto s = truncateMultiLine( longString, LinesStartEnd { 0, 0 }, CharCount { 4 } );
QCOMPARE( s.length(), 4 );
QCOMPARE( s, QString( "---\n" ) );
}
{
auto s = truncateMultiLine( longString, LinesStartEnd { 0, 0 }, CharCount { sufficientLength } );
QCOMPARE( s, longString );
}
// Lines at the start
{
auto s = truncateMultiLine( longString, LinesStartEnd { 4, 0 }, CharCount { sufficientLength } );
QVERIFY( s.length() > 1 );
QVERIFY( longString.startsWith( s ) );
cDebug() << "Result-line" << Logger::Quote << s;
QCOMPARE( s.count( '\n' ), 4 );
}
// Lines at the end
{
auto s = truncateMultiLine( longString, LinesStartEnd { 0, 4 }, CharCount { sufficientLength } );
QVERIFY( s.length() > 1 );
QVERIFY( longString.endsWith( s ) );
cDebug() << "Result-line" << Logger::Quote << s;
QCOMPARE( s.count( '\n' ), 4 );
}
// Lines at both ends
{
auto s = truncateMultiLine( longString, LinesStartEnd { 2, 2 }, CharCount { sufficientLength } );
QVERIFY( s.length() > 1 );
cDebug() << "Result-line" << Logger::Quote << s;
QCOMPARE( s.count( '\n' ), 4 );
auto firsttwo = truncateMultiLine( s, LinesStartEnd { 2, 0 }, CharCount { sufficientLength } );
auto lasttwo = truncateMultiLine( s, LinesStartEnd { 0, 2 }, CharCount { sufficientLength } );
QCOMPARE( firsttwo + lasttwo, s );
QVERIFY( longString.startsWith( firsttwo ) );
QVERIFY( longString.endsWith( lasttwo ) );
}
}
QTEST_GUILESS_MAIN( LibCalamaresTests )
#include "utils/moc-warnings.h"

View File

@ -35,7 +35,7 @@
[[noreturn]] static void
bail( const QString& descriptorPath, const QString& message )
{
cError() << "FATAL in" << descriptorPath << Logger::Continuation << Logger::NoQuote {} << message;
cError() << "FATAL in" << descriptorPath << Logger::Continuation << Logger::NoQuote << message;
::exit( EXIT_FAILURE );
}

View File

@ -19,6 +19,7 @@
#include "utils/Logger.h"
#include "utils/Paste.h"
#include "utils/Retranslator.h"
#include "utils/String.h"
#include "viewpages/BlankViewStep.h"
#include "viewpages/ExecutionViewStep.h"
#include "viewpages/ViewStep.h"
@ -136,15 +137,14 @@ ViewManager::insertViewStep( int before, ViewStep* step )
emit endInsertRows();
}
void
ViewManager::onInstallationFailed( const QString& message, const QString& details )
{
bool shouldOfferWebPaste = false; // TODO: config var
cError() << "Installation failed:";
cDebug() << "- message:" << message;
cDebug() << "- details:" << details;
cError() << "Installation failed:" << message;
cDebug() << Logger::SubEntry << "- message:" << message;
cDebug() << Logger::SubEntry << "- details:" << Logger::NoQuote << details;
QString heading
= Calamares::Settings::instance()->isSetupMode() ? tr( "Setup Failed" ) : tr( "Installation Failed" );
@ -152,7 +152,7 @@ ViewManager::onInstallationFailed( const QString& message, const QString& detail
QString text = "<p>" + message + "</p>";
if ( !details.isEmpty() )
{
text += "<p>" + details + "</p>";
text += "<p>" + CalamaresUtils::truncateMultiLine( details, CalamaresUtils::LinesStartEnd{8, 0}) + "</p>";
}
if ( shouldOfferWebPaste )
{