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?
Forum Updated to NodeBB v4.3 + New Features

how to use qInstallMessageHandler to ignore specific debug messages?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 574 Views 1 Watching
  • 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 6 May 2023, 16:05 last edited by Ylvy 5 Jun 2023, 16:17
    #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;
    	}
    }
    
    J C 2 Replies Last reply 6 May 2023, 16:27
    0
    • Y Ylvy
      6 May 2023, 16:05

      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;
      	}
      }
      
      C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 6 May 2023, 16:34 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
        6 May 2023, 16:05

        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;
        	}
        }
        
        J Offline
        J Offline
        JonB
        wrote on 6 May 2023, 16:27 last edited by JonB 5 Jun 2023, 16:31
        #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 6 May 2023, 16:34
        2
        • J JonB
          6 May 2023, 16:27

          @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 6 May 2023, 16:34 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
            6 May 2023, 16:05

            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;
            	}
            }
            
            C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 6 May 2023, 16:34 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 6 May 2023, 16:40

            1/4

            6 May 2023, 16:05

            • Login

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