Results 1 to 4 of 4

Thread: How does qDebug() print a line break?

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How does qDebug() print a line break?

    You now how when you
    Qt Code:
    1. qDebug() << 1 << 2 << 3;
    To copy to clipboard, switch view to plain text mode 
    it neatly prints a line break after the 3? How does the << operator know which one is the last item in the chain?
    I would like to borrow (steal, really) this feature for a custom file logger I wrote where I would like to chain the
    << operator and it should put a line break after the last item.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How does qDebug() print a line break?

    This sounds like a "How long is a piece of string?" question

    qDebug() returns an instance of the QDebug class. Each of the operator<< calls return a reference to the QDebug instance, which allows you to chain together << calls. When the code reaches the end of the statement (";"), the QDebug instance goes out of scope. The destructor is called, it pushes the CRLF onto the stream, flushes the stream, and closes it. That's how it "knows".
    Last edited by d_stranz; 10th August 2021 at 00:08.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Cruz (11th August 2021)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How does qDebug() print a line break?

    Okay, I understand the mechanics. I am still not sure how I can apply this to my logger class, because I open a file and a text stream inside.
    How can I return an instance that can go out of scope while still maintaining access to that file?

    Qt Code:
    1. class Logger
    2. {
    3. QFile file;
    4. QTextStream stream;
    5. QString fileName;
    6. QMutex mutex;
    7.  
    8. public:
    9. Logger();
    10. Logger(QString rl);
    11. ~Logger();
    12.  
    13. void open(QString rl);
    14. void flush();
    15.  
    16. Logger& operator<<(const QString s);
    17. };
    18.  
    19. void Logger::open(QString rl)
    20. {
    21. this->fileName = rl;
    22. if (file.isOpen())
    23. {
    24. flush();
    25. file.close();
    26. }
    27. file.setFileName(fileName);
    28. file.open(QFile::WriteOnly | QFile::Text);
    29. stream.setDevice(&file);
    30. }
    31.  
    32. Logger& Logger::operator<<(const QString s)
    33. {
    34. QMutexLocker locker(&mutex);
    35. stream << " " << s;
    36. return *this;
    37. }
    38.  
    39. Logger& Logger::operator++()
    40. {
    41. QMutexLocker locker(&mutex);
    42. stream << "\n";
    43. stream.flush();
    44. return *this;
    45. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Logger logger("logs/birch.log");
    2. logger << "one" << "two" << "three";
    3. logger++; // <- explicit new line operator to get rid of.
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How does qDebug() print a line break?

    How can I return an instance that can go out of scope while still maintaining access to that file?
    Make a singleton class (LogFile) to hold the (static) file pointer / instance. The Logger class asks for the pointer in its constructor through a LogFile:: getInstance() call. In that call, if the file is not open, then open it and pass back the pointer. You may want to give LogFile a close() method, which will close the file and set the static pointer back to null.

    The LogFile class controls the lifetime of the file instance, not Logger.

    This presumes you only want one log file in your program. If the program is multi-threaded, then you would probably want to add mutexes or some other mechanism to prevent a log file full of garbage.
    Last edited by d_stranz; 12th August 2021 at 17:00.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    Cruz (12th August 2021)

Similar Threads

  1. QXmlQuery::evaluateTo(QString) adds a line break
    By mentalmushroom in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2018, 00:45
  2. QPrinter print QWebView content without any any page break.
    By Cupidvogel in forum Qt Programming
    Replies: 6
    Last Post: 21st May 2018, 01:51
  3. Replies: 1
    Last Post: 24th November 2017, 19:36
  4. how to print filename and linenumber using qDebug
    By dpatel in forum Qt Programming
    Replies: 3
    Last Post: 24th May 2010, 13:36
  5. How to print variables with qDebug?
    By ricardo in forum Qt Programming
    Replies: 7
    Last Post: 13th July 2009, 00:27

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.