Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5983359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:13:09+00:00 2026-05-22T22:13:09+00:00

The following page provides a nice simple solution for file based logging in Qt

  • 0

The following page provides a nice simple solution for file based logging in Qt for debugging without using a larger logging framework like the many that are suggested in other SO questions.

I’m writing a library and would like to instantiate a logger that the classes in the library can use (mostly for debugging purposes). There is no int main() function since it’s a library. So would the best approach be to add the instantiation into a file like logger.h and have any classes include logger.h if it would like to do qDebug() << PREFIX << "Bla" as the link above suggests?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:13:09+00:00Added an answer on May 22, 2026 at 10:13 pm

    I pretty much agree with OrcunC but I’d recommend making that ofstream a little more accessible and capable of handling the Qt value types.

    Here’s my recommended process:

    1. Create a global QIODevice that to which everything will be written. This will probably be a QFile.
    2. Create a QTextStream wrapper around that QIODevice that you’ll then use for all the logging.
    3. If you want something slightly more complicated, create methods that do the filtering based on log level info.

    For example:

    // setup the global logger somewhere appropriate
    QFile *file = new QFile("your.log");
    file->open(QIODevice::ReadOnly);
    QTextStream *qlogger = new QTextStream(file);
    

    And once the global logger is initialized, you could reference it as a global:

    #include "qlogger.h"
    //... and within some method
    *qlogger << "your log" << aQtValueType;
    

    But you might want some filtering:

    #include "qlogger.h"
    // lower number = higher priority
    void setCurrentLogLevel(int level) {
       globalLogLevel = level;
    }
    QTextStream* qLog(int level) {
       if (level <= globalLogLevel) {
           return qlogger;
       }
       return getNullLogger(); // implementation left to reader
    }
    

    And then you’d likely create an enum that represented the LogLevel and do something like this:

    #include "qlogger.h"
    //...
    setCurrentLogLevel(LogLevel::Warning);
    *qLog(LogLevel::Debug) << "this will be filtered" << yourQMap;
    *qLog(LogLevel::Critical) << "not filtered" << yourQString;
    

    As you’d be dealing with globals, carefully consider memory management issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following html page: <html> <head> <title>AJAX Example</title> <meta http-equiv=Content-Type content=text/html;
I'm using the following in the web page but can't get a response from
I am using resources :users in routes.rb . This provides the following paths as
I have the following in a page e.g. /mypage?myvar=oldvalue $_SESSION['myvar'] = $_GET['myvar']; $myvar =
The following code will generate a link to the page I want to get
Suppose I have the following CSS rule in my page: body { font-family: Calibri,
I've got the following JavaScript on my web page... 64 var description = new
I am getting the following error when I post back a page from the
Inside my page, I have the following: <aspe:UpdatePanel runat=server ID=updatePanel> <ContentTemplate> <local:KeywordSelector runat=server ID=ksKeywords
I have created a custom content type and using views2 for creating various page-views.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.