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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:08:18+00:00 2026-05-29T22:08:18+00:00

To print debug messages in my program, I have a that can be used

  • 0

To print debug messages in my program, I have a that can be used like this:

DBG(5) << "Foobar" << std::endl;

5 means the level of the message, if the debug level is smaller than 5, it won’t print the message. Currently it is implemented like:

#define DBG(level) !::Logger::IsToDebug((level)) ? : ::Logger::Debug

Basically IsToDebug checks if the message should be printed, and returns true when it should. Logger::Debug is an std::ostream. This works with gcc and clang too, however clang generates expression result unused warnings. According to this email this doesn’t like to change either.

Prefixing it with (void) doesn’t work, it will only cast the thing before the ?, resulting in a compilation error (void can’t be converted to bool, obviously). The other problem with this syntax that it uses a gcc extension.

Doing things like #define DBG(x) if (::Logger::IsToDebug((x))) ::Logger::Debug solves the problem, but it’s a sure way break your program (if (foo) DBG(1) << "foo"; else ...) (and I can’t put the whole thing into a do { ... } while(0) due to how the macro is called.)

The only more or less viable solution I came up with is this (assuming IsToDebug returns either 0 or 1):

#define DBG(level) for(int dbgtmpvar = ::Logger::IsToDebug((level)); \
                       dbgtmpvar > 0; --dbgtmpvar) ::Logger::Debug

Which seems like an overkill (not counting it’s runtime overhead)

  • 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-29T22:08:20+00:00Added an answer on May 29, 2026 at 10:08 pm

    I think you should use ternary operator, as it is defined in the Standard, rather than compiler extension. To use the Standard ternary operator, you’ve to provide the second expression as well. For that, you can define a stream class, deriving from std::ostream which doesn’t print anything to anywhere. Object of such a class can be used as second expression.

    class oemptystream : std::ostream
    {
         //..
    };
    
    extern oemptystream nout; //declaration here, as definition should go to .cpp
    

    then

    #define DBG(level) ::Logger::IsToDebug((level))? nout : ::Logger::Debug
    

    Now if you use this macro, then at runtime, the expression would reduce to either this:

    nout << "message";
    

    Or this,

    ::Logger::Debug << "message";
    

    Either way, it is pretty much like this:

    std::cout << "message";
    

    So I hope it shouldn’t give compiler warning.

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

Sidebar

Related Questions

Trying to create a macro which can be used for print debug messages when
At some places in my code, I print debug messages like this: int ret
Is there a way to define/undefine debug messages using std::cout whenever inside a program?
What's the quickest way to print debug messages from Flex? You know, things like
How do I print debug messages in the Google Chrome JavaScript Console? Please note
I want to print out some debug messages in my erb template when it
A friend of mine claims that calls to Debug.Print() as well as first-chance exception
I can use the PRINT statement in a stored procedure to debug my code.
OK, This is a method from my program that keeps giving the EXC_BAD_ACCESS error
I have a core dump of an executable that was NOT built with debug

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.