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. how to use qInstallMessageHandler to ignore specific debug messages?
QtWS25 Last Chance

how to use qInstallMessageHandler to ignore specific debug messages?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 474 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
    Ylvy
    wrote on last edited by Ylvy
    #1

    How i could allow it to only print qDebug() messages?
    In my attempt below its not printing qDebug().

    void myMessageHandler(QtMsgType type, const QMessageLogContext& context, 
    	const QString& message) 
    {
    	switch (type)
    	{
    	case QtDebugMsg: 
    		qDebug() << message; // <- doesnt work
    	break;
    
    	// Stop these msg from being printed to debug.
    	case QtWarningMsg:
    	case QtCriticalMsg:
    	case QtFatalMsg:
    	case QtInfoMsg:
    	default:
    	break;
    	}
    }
    
    JonBJ Chris KawaC 2 Replies Last reply
    0
    • Y Ylvy

      How i could allow it to only print qDebug() messages?
      In my attempt below its not printing qDebug().

      void myMessageHandler(QtMsgType type, const QMessageLogContext& context, 
      	const QString& message) 
      {
      	switch (type)
      	{
      	case QtDebugMsg: 
      		qDebug() << message; // <- doesnt work
      	break;
      
      	// Stop these msg from being printed to debug.
      	case QtWarningMsg:
      	case QtCriticalMsg:
      	case QtFatalMsg:
      	case QtInfoMsg:
      	default:
      	break;
      	}
      }
      
      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Ylvy You can't use qDebug inside the handler. That's recursive.
      qInstallMessageHandler returns a previous handler (or the default one if you didn't set any yet). If you want to just filter out everything but debug you can store the old handler and call it from yours, e.g.

      // put it somewhere accessible to your handler
      QtMessageHandler defaultHandler {};
      
      // install new and get the old one
      defaultHandler = qInstallMessageHandler(myMessageHandler);
      

      and in your handler:

      void myMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) 
      {
         if (type == QtDebugMsg)
            defaultHandler(type, context, message);
      }
      
      1 Reply Last reply
      4
      • Y Ylvy

        How i could allow it to only print qDebug() messages?
        In my attempt below its not printing qDebug().

        void myMessageHandler(QtMsgType type, const QMessageLogContext& context, 
        	const QString& message) 
        {
        	switch (type)
        	{
        	case QtDebugMsg: 
        		qDebug() << message; // <- doesnt work
        	break;
        
        	// Stop these msg from being printed to debug.
        	case QtWarningMsg:
        	case QtCriticalMsg:
        	case QtFatalMsg:
        	case QtInfoMsg:
        	default:
        	break;
        	}
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @Ylvy
        If this is installed as the message handler, it gets called by qDebug(). But you issue a qDebug() in this case from inside it. Sounds dangerously like infinite recursion to me?!

        Try something like printf() or fprintf(stderr or cerr << for the qDebug() you have, does that get fired OK?

        What you probably ought do here is call the message handler you replaced by this in the case QtDebugMsg route.

        Y 1 Reply Last reply
        2
        • JonBJ JonB

          @Ylvy
          If this is installed as the message handler, it gets called by qDebug(). But you issue a qDebug() in this case from inside it. Sounds dangerously like infinite recursion to me?!

          Try something like printf() or fprintf(stderr or cerr << for the qDebug() you have, does that get fired OK?

          What you probably ought do here is call the message handler you replaced by this in the case QtDebugMsg route.

          Y Offline
          Y Offline
          Ylvy
          wrote on last edited by
          #3

          @JonB said in how to use qInstallMessageHandler to ignore specific debug messages?:

          @Ylvy
          What you probably ought do here is call the message handler you replaced by this in the case QtDebugMsg route.

          How?

          1 Reply Last reply
          0
          • Y Ylvy

            How i could allow it to only print qDebug() messages?
            In my attempt below its not printing qDebug().

            void myMessageHandler(QtMsgType type, const QMessageLogContext& context, 
            	const QString& message) 
            {
            	switch (type)
            	{
            	case QtDebugMsg: 
            		qDebug() << message; // <- doesnt work
            	break;
            
            	// Stop these msg from being printed to debug.
            	case QtWarningMsg:
            	case QtCriticalMsg:
            	case QtFatalMsg:
            	case QtInfoMsg:
            	default:
            	break;
            	}
            }
            
            Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Ylvy You can't use qDebug inside the handler. That's recursive.
            qInstallMessageHandler returns a previous handler (or the default one if you didn't set any yet). If you want to just filter out everything but debug you can store the old handler and call it from yours, e.g.

            // put it somewhere accessible to your handler
            QtMessageHandler defaultHandler {};
            
            // install new and get the old one
            defaultHandler = qInstallMessageHandler(myMessageHandler);
            

            and in your handler:

            void myMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) 
            {
               if (type == QtDebugMsg)
                  defaultHandler(type, context, message);
            }
            
            1 Reply Last reply
            4
            • Y Ylvy has marked this topic as solved on

            • Login

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