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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:39:00+00:00 2026-05-31T06:39:00+00:00

Our project uses a macro to make logging easy and simple in one-line statements,

  • 0

Our project uses a macro to make logging easy and simple in one-line statements, like so:

DEBUG_LOG(TRACE_LOG_LEVEL, "The X value = " << x << ", pointer = " << *x);

The macro translates the 2nd parameter into stringstream arguments, and sends it off to a regular C++ logger. This works great in practice, as it makes multi-parameter logging statements very concise. However, Scott Meyers has said, in Effective C++ 3rd Edition, “You can get all the efficiency of a macro plus all the predictable behavior and type safety of a regular function by using a template for an inline function” (Item 2). I know there are many issues with macro usage in C++ related to predictable behavior, so I’m trying to eliminate as many macros as possible in our code base.

My logging macro is defined similar to:

#define DEBUG_LOG(aLogLevel, aWhat) {  \
if (isEnabled(aLogLevel)) {            \
  std::stringstream outStr;            \
  outStr<< __FILE__ << "(" << __LINE__ << ") [" << getpid() << "] : " << aWhat;    \
  logger::log(aLogLevel, outStr.str());    \
}

I’ve tried several times to rewrite this into something that doesn’t use macros, including:

inline void DEBUG_LOG(LogLevel aLogLevel, const std::stringstream& aWhat) {
    ...
}

And…

template<typename WhatT> inline void DEBUG_LOG(LogLevel aLogLevel, WhatT aWhat) {
    ...  }

To no avail (neither of the above 2 rewrites will compile against our logging code in the 1st example). Any other ideas? Can this be done? Or is it best to just leave it as a macro?

  • 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-31T06:39:02+00:00Added an answer on May 31, 2026 at 6:39 am

    Logging remains one of the few places were you can’t completely do away with macros, as you need call-site information (__LINE__, __FILE__, …) that isn’t available otherwise. See also this question.

    You can, however, move the logging logic into a seperate function (or object) and provide just the call-site information through a macro. You don’t even need a template function for this.

    #define DEBUG_LOG(Level, What) \
      isEnabled(Level) && scoped_logger(Level, __FILE__, __LINE__).stream() << What
    

    With this, the usage remains the same, which might be a good idea so you don’t have to change a load of code. With the &&, you get the same short-curcuit behaviour as you do with your if clause.

    Now, the scoped_logger will be a RAII object that will actually log what it gets when it’s destroyed, aka in the destructor.

    struct scoped_logger
    {
      scoped_logger(LogLevel level, char const* file, unsigned line)
        : _level(level)
      { _ss << file << "(" << line << ") [" << getpid() << "] : "; }
    
      std::stringstream& stream(){ return _ss; }
      ~scoped_logger(){ logger::log(_level, _ss.str()); }
    private:
      std::stringstream _ss;
      LogLevel _level;
    };
    

    Exposing the underlying std::stringstream object saves us the trouble of having to write our own operator<< overloads (which would be silly). The need to actually expose it through a function is important; if the scoped_logger object is a temporary (an rvalue), so is the std::stringstream member and only member overloads of operator<< will be found if we don’t somehow transform it to an lvalue (reference). You can read more about this problem here (note that this problem has been fixed in C++11 with rvalue stream inserters). This “transformation” is done by calling a member function that simply returns a normal reference to the stream.

    Small live example on Ideone.

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

Sidebar

Related Questions

Our project currently uses Crystal Reports for Visual Studio 2008. We need to upgrade
I have an old project at our company that uses shell scripting a lot.
Our project constitutes of many modules (e.g. a HR management system containing modules like
Our project uses an LDAP repository for storing users. In production this will be
Our project application uses Java 5 and now when I update Java 6, there
Our project uses Hibernate (3.3.2) with c3p0 as our connection pool. For a while,
In our project we have something like this: struct PointI { // methods for
Currently, when building our project we use: javac <various command line options> C:\project\CompilationHelper.java The
Our project uses XJC to generate Java classes from an XSD. I'm using JAVA
Our web project uses spring3, Jpa (hibernate). To save CRUD and other commonly used

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.