From ee7166e1761c3b5ca885bab299a7d036e884c2e7 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 18 Feb 2022 11:14:44 -0800 Subject: [PATCH] Logging: remove magic constant from backtrace code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Tor Arne Vestbø --- src/corelib/global/qlogging.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index c369259788..11a43f73ef 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -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=, __in_chrg=) 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 buffer(8 + frameCount); + QVarLengthArray buffer(TypicalBacktraceFrameCount + frameCount); int n = backtrace(buffer.data(), buffer.size()); if (n > 0) { int numberPrinted = 0;