Logging: remove magic constant from backtrace code

Explain what that number 8 is by way of a comment and a constant.

Pick-to: 6.2 6.3
Change-Id: Ic15405335d804bdea761fffd16d4f7567089c575
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Thiago Macieira 2022-02-18 11:14:44 -08:00
parent 6358bc9331
commit ee7166e176

View File

@ -1263,6 +1263,18 @@ void QMessagePattern::setPattern(const QString &pattern)
#if defined(QLOGGING_HAVE_BACKTRACE) && !defined(QT_BOOTSTRAPPED)
// make sure the function has "Message" in the name so the function is removed
/*
A typical backtrace in debug mode looks like:
#0 backtraceFramesForLogMessage (frameCount=5) at qlogging.cpp:1296
#1 formatBacktraceForLogMessage (backtraceParams=..., function=0x4040b8 "virtual void MyClass::myFunction(int)") at qlogging.cpp:1344
#2 qFormatLogMessage (type=QtDebugMsg, context=..., str=...) at qlogging.cpp:1452
#3 stderr_message_handler (type=QtDebugMsg, context=..., message=...) at qlogging.cpp:1744
#4 qDefaultMessageHandler (type=QtDebugMsg, context=..., message=...) at qlogging.cpp:1795
#5 qt_message_print (msgType=QtDebugMsg, context=..., message=...) at qlogging.cpp:1840
#6 qt_message_output (msgType=QtDebugMsg, context=..., message=...) at qlogging.cpp:1891
#7 QDebug::~QDebug (this=<optimized out>, __in_chrg=<optimized out>) at qdebug.h:111
*/
static constexpr int TypicalBacktraceFrameCount = 8;
#if (defined(Q_CC_GNU) || __has_attribute(optimize)) \
&& !defined(Q_CC_INTEL) && !defined(Q_CC_CLANG)
@ -1282,7 +1294,7 @@ static QStringList backtraceFramesForLogMessage(int frameCount)
// This code is protected by QMessagePattern::mutex so it is thread safe on all compilers
static const QRegularExpression rx(QStringLiteral("^(?:[^(]*/)?([^(/]+)\\(([^+]*)(?:[\\+[a-f0-9x]*)?\\) \\[[a-f0-9x]*\\]$"));
QVarLengthArray<void *, 32> buffer(8 + frameCount);
QVarLengthArray<void *, 32> buffer(TypicalBacktraceFrameCount + frameCount);
int n = backtrace(buffer.data(), buffer.size());
if (n > 0) {
int numberPrinted = 0;