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 8222191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:11:37+00:00 2026-06-07T14:11:37+00:00

in the Debug.h file, I have the following: #ifdef DEBUG_FLAG #define DEBUG(msg) std::cerr <<

  • 0

in the Debug.h file, I have the following:

#ifdef DEBUG_FLAG
    #define DEBUG(msg) std::cerr << #msg << std::endl
#else
    #define DEBUG(msg) for(;true==false;)
#endif

In other places, I may write something like

void process (Data data)
{
    DEBUG("Function 'process' starts");
    // Blah blah
    // More blah blah...
    DEBUG("Function 'process' returns");
}

Will the compiler optimize away the for(;true==false;); ?

Also, is this kind of practice okay? If not, what would be a better way?

Thanks!

  • 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-06-07T14:11:38+00:00Added an answer on June 7, 2026 at 2:11 pm

    Here’s an alternative, that uses the compiler’s dead code removal:

    #define DEBUG(msg) if (!DEBUG_ENABLED) {} \
                       else dbglog() << __FILE__ << ":" << __LINE__ << " " << msg
    #ifdef DEBUG_FLAG
    #define DEBUG_ENABLED 1
    #else
    #define DEBUG_ENABLED 0
    #endif
    

    The dbglog instance is a ostream wrapper that detects if the log line ended with a newline or not. If not, it adds one.

    struct dbglog {
        std::ostream &os_;
        mutable bool has_endl_;
        dbglog (std::ostream &os = std::cerr) : os_(os), has_endl_(false) {}
        ~dbglog () { if (!has_endl_) os_ << std::endl; }
        template <typename T> static bool has_endl (const T &) { return false; }
        static bool has_endl (char c) { return (c == '\n'); }
        static bool has_endl (std::string s) { return has_endl(*s.rbegin()); }
        static bool has_endl (const char *s) { return has_endl(std::string(s)); }
        template <typename T>
        static bool same_manip (T & (*m)(T &), T & (*e)(T &)) { return (m == e); }
        const dbglog & operator << (std::ostream & (*m)(std::ostream &)) const {
            has_endl_ = same_manip(m, std::endl);
            os_ << m;
            return *this;
        }
        template <typename T>
        const dbglog & operator << (const T &v) const {
            has_endl_ = has_endl(v);
            os_ << v;
            return *this;
        }
    };
    

    Now, you can add a simple message like this (note, the newline is optional):

    DEBUG("A simple message");
    DEBUG("A simple message with newline\n");
    DEBUG("A simple message with endl") << std::endl;
    

    Or, if you want to add more debugging information:

    DEBUG("Entering: ") << __func__ << ", argc=" << argc << ", argv=" << argv;
    //...
    DEBUG("Leaving: ") << __func__ << std::endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following macro in my PCH file. #ifdef DEBUG #define MYLOG(...) NSLog(__VA_ARGS__) #else
I have the following logback.xml file: <configuration debug=true> <appender name=STDOUT class=ch.qos.logback.core.ConsoleAppender> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread]
I have a header file like this: #ifndef __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #define __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #ifdef _DEBUG //
I have the following in my log4j file #set httpclient debug levels log4j.logger.org.apache.component=ERROR,stdout log4j.logger.httpclient.wire=ERROR,stdout
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
I have the following code. (You can copy it into a file to debug
We have the following in our codebase, in a component file: {$IFDEF ADO} FDatabase
I have the following in config/environments/test.rb: config.assets.debug = true config.assets.enabled = true I get
I have the following PS script: function Install-BizTalkApplication { param( [Parameter(Position=0,Mandatory=$true,HelpMessage=Msi file should be
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =

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.