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

  • SEARCH
  • Home
  • 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 6042939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:50:44+00:00 2026-05-23T06:50:44+00:00

I would like to set a debug mode so that it prints the log

  • 0

I would like to set a debug mode so that it prints the log statements only if the debug mode is on. For example if I have code like this

printf("something \n");
.
.
.
perror("something \n");

It only works if the debug flag is on.. I don’t want to use “if” statements.
I think there is a clever way to do this using #define or something..

Thank is advance..

  • 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-23T06:50:44+00:00Added an answer on May 23, 2026 at 6:50 am

    Without going in to specific libraries or solutions, generally people make a logger class or function, and a single debug flag. The debug function checks this flag before calling printf or cout. Then in the rest of your code you simply call your debug function / method.

    Here’s an example:

    class MyDebugger
    {
        private:
            bool m_debug;
    
        public:
            MyDebugger();
            void setDebug(bool debug);
            void debug(const char* message);
    };
    
    MyDebugger::MyDebugger()
    {
        m_debug = false;
    }
    
    void MyDebugger::setDebug(bool debug)
    {
        m_debug = debug;
    }
    
    void MyDebugger::debug(const char* message)
    {
        if(m_debug)
        {
            cout << message << endl;
        }
    }
    
    int main(int argc, char** argv)
    {
        MyDebugger debugger;
        debugger.debug("This won't be shown");
        debugger.setDebug(true);
        debugger.debug("But this will");
    
        return 0;
    }
    

    of course this is an incredibly naive implementation. In real logger classes there are many levels for finer-grained control of how much detail gets printed (levels like error, warning, info, and debug to differentiate the importance of the message). They might also let you log to files as well as stdout. Still this should give you a general idea.

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

Sidebar

Related Questions

I would like to add some C# debug only code that only runs if
This sounds like a issue a beginner like me would only have...this is my
I would like to set the log file name for a log4j and log4net
I would like to set up an XSD such that it enforces something like
I would like to set values on an url like this: <a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'&note='$id_note'&user='$username'> Then
I'm writing R code where I would like to have it run either in
I would like to set level logging to DEBUG in tomcat but in console
I have some code like this: #define FORCE_DEBUG_MODE [[[NSUserDefaults standardUserDefaults] valueForKey:@forceDebugMode] isEqualToString:@1] #if defined
I would like to set up a global UITouch handler to debug all UITouch
I have some debugging functions that I would like to refactor, but seeing as

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.