Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Example to keep track of CallStack and how to print
Forum Updated to NodeBB v4.3 + New Features

Example to keep track of CallStack and how to print

Scheduled Pinned Locked Moved Unsolved General and Desktop
backtracecallstack
1 Posts 1 Posters 195 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yalovali
    wrote on 28 Dec 2023, 06:26 last edited by
    #1

    With QT, this is a selective approach what to print in output.

    ccallstack.h

    `#define CALLSTACK CCallStack c(QDateTime::currentDateTime(),__FILE__,__LINE__,__FUNCTION__,"")
    #define CALLSTACK2(comment) CCallStack c(QDateTime::currentDateTime(),__FILE__,__LINE__,__FUNCTION__,comment)
    /**************************************************/
    class CCallStack
    {
        private:
            QString FunctionName;
            static QStringList Stack;
        public:
            CCallStack(QDateTime date,cstr file,int line,cstr functionName,cstr comment);
            CCallStack(cstr functionName);
            ~CCallStack();
            static void print();
    };
    

    `

    ccallstack.cpp

     #include "ccallstack.h"
    QStringList CCallStack::Stack;
    
    CCallStack::CCallStack(QDateTime date,cstr file,int line,cstr functionName,cstr comment)
    {
        QString s =
            QString("%1 file://%2:%3\033[35m %4\033[0m \033[34m:%5").arg(date.toString("HH:mm:ss:zzz")).arg(file).arg(
                    line).arg(
                    functionName).arg(comment);
        FunctionName = s;
        Stack.append(s);
    }
    
    CCallStack::CCallStack(cstr functionName):
        FunctionName(functionName)
    {
        Stack.append(functionName);
    }
    
    CCallStack::~CCallStack()
    {
        QString lastName = Stack.last();
        if (lastName!=FunctionName)
        {
            qWarning() << "There is a thread or Event Order conflict.";
            print();
        }
        Stack.removeLast();
    }
    void CCallStack::print()
    {
        int index = 0;
        qDebug().nospace().noquote() << "STACKTRACE OUTPUT Stack Count:" << Stack.count();
        foreach(QString name,Stack)
        {
            qDebug().nospace().noquote() << index << ":" << name;
            index++;
        }
        qDebug().nospace().noquote() << "END OF STACKTRACE OUTPUT";
    }
    

    usage:
    register a function using:

    foo (){
       CALLSTACK
       ...
    }
    

    or

    CALLSTACK2("this is a function to trace");
    

    Then just call like:

    CException::CException(cstr err):
    {
        CCallStack::print();
    }
    

    outputs:

    STACKTRACE OUTPUT Stack Count:6
    0:09:18:26:505 file://..\main_app\entities\cdesktopsession.cpp:1034 CDesktopSession::sl_updateTimer_triggerred :
    1:09:18:26:505 file://..\main_app\entities\cdesktopentity.cpp:161 CDesktopEntity::processTimeTick :Freeze_Frame_Index_Number
    2:09:18:26:505 file://..\main_app\entities\cdesktopfield.cpp:154 CDesktopField::updateEntity :Freeze_Frame_Index_Number
    3:09:18:26:505 file://..\main_app\entities\cdesktopa2lsinglevalue.cpp:299 CDesktopA2LSingleValue::getA2LValue :Freeze_Frame_Index_Number
    4:09:18:26:505 file://..\main_app\device\ccandevicexcp.cpp:302 CCanDeviceXCP::requestAddressValue_internal :
    5:09:18:26:505 file://..\main_app\device\ccandevice.cpp:72 CCanDevice::sendFrame_internal :
    END OF STACKTRACE OUTPUT
    

    Simple and usefull...

    1 Reply Last reply
    0

    1/1

    28 Dec 2023, 06:26

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved