Results 1 to 1 of 1

Thread: qDebug() messages aren't flushing

  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qDebug() messages aren't flushing

    I have a simple program that starts a single shot timer. When the timer expires, a Qprocess is started that attempts to establish a bluetooth connection.

    If the connection fails, the timer is restarted. This continues until the connection is successful.

    The program that does the bluetooth connection (bluetoothctl) consumes the entire processor (rpi zero W) until it finishes. I see this when I have a second ssh session open while my program is running. Bluetoothctl runs several seconds while attempting to make a connection. During this time, the second ssh session is non-responsive.

    This is ok--I don't have a GUI that needs refreshing. But, I do want to make sure there's nothing wrong in my code (like unintentional recursion) that causing the cpu resource to be consumed. So, I have some qDebug() statements to help with that.

    Initially, the qDebug statements print when expected. Then, as the program continues to try to make a connection, there is an ever increasing delay before the qDebug output is seen. Eventually, I see no qDebug output at all, but when I turn on the client bluetooth so that a successful connection can be made, the connection is successful and all the delayed qDebug statements appear.

    I have added fflush(stderr), fflush(stdout), and QCoreApplication.processEvents() after the qDebug statements, but they are still delayed.

    So, while the application is "working", I worry that eventually, whatever queue is saving the messages will get full, or worse, there's really some mistake in my code that's causing this strange bevhaviour.

    Why are the qDebug statements delayed? Can anything be done to get them to print immediately after the QProcess finishes?

    Here's the code that runs when the timer expires:

    Qt Code:
    1. void Bluetooth::connectDev(void)
    2. {
    3. if (!isConnected())
    4. {
    5. emit disconnected();
    6. qDebug() << "attempting connection";
    7.  
    8. // If we've connected previously, we can use the info in settings.
    9. if (controllerMac != "none" && deviceMac != "none")
    10. {
    11.  
    12. QProcess echoP;
    13. QProcess btP;
    14. QString btCmd = "select " + controllerMac + "\\nconnect " + deviceMac + "\\nquit\\n";
    15. echoP.setStandardOutputProcess(&btP);
    16.  
    17. echoP.start("echo -e """"" + btCmd + """");
    18. qDebug() << "starting bluetoothctl";
    19. btP.start("bluetoothctl");
    20. btP.setProcessChannelMode(QProcess::ForwardedChannels);
    21. if (!echoP.waitForStarted())
    22. {
    23. qDebug("echoP never started");
    24. return;
    25. }
    26. else
    27. qDebug("echoP started");
    28.  
    29. if (!btP.waitForStarted())
    30. {
    31. qDebug("btP not started");
    32. autoConnectTimer->start();
    33. return;
    34. }
    35. else
    36. qDebug("btP started");
    37.  
    38. echoP.waitForFinished();
    39. btP.waitForFinished();
    40. qDebug()<< "bluetoothctl finished, connect attempts = " << connectionAttempts++;
    41. QByteArray output = btP.readAllStandardOutput();
    42. qDebug()<< output;
    43. fflush(stdout);
    44. fflush(stderr);
    45. qDebug() << "restarting autoconnect timer";
    46. fflush(stderr);
    47. }
    48. }
    49. else
    50. {
    51. connectionAttempts = 0;
    52. setDiscoverable(false);
    53. emit connected();
    54. qDebug() << "already connected";
    55. }
    56. QCoreApplication::processEvents();
    57. autoConnectTimer->start();
    58. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help!


    Added after 1 29 minutes:


    I've been playing around with timer interval. Slowing it down until the qDebug messages don't get delay.

    It's strange--it takes at most 2-3 seconds for the QProcess to finish, based on qDebug output after waitForFinished(). Yet, if I poll more often than about eight seconds, the debug output gets more and more delayed until there's none.

    It's as if there's significant cpu resource being consumed AFTER the waitForFinished() unblocks. As much (maybe more) after the waitForFinished than during the actual process execution.
    Last edited by davethomaspilot; 23rd May 2018 at 00:25.

Similar Threads

  1. QDebug messages show in application?
    By Leutzig in forum Newbie
    Replies: 1
    Last Post: 16th December 2015, 11:43
  2. QSslSocket/QTcpSocket not flushing data by itself
    By raheelgup in forum Qt Programming
    Replies: 2
    Last Post: 10th June 2014, 09:48
  3. Managing Widgets that aren't on the top level
    By prusikknot in forum Qt Programming
    Replies: 0
    Last Post: 19th April 2010, 17:51
  4. Replies: 16
    Last Post: 11th October 2009, 18:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.