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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:34:24+00:00 2026-06-15T06:34:24+00:00

I’m working on a simple logger wrapper for my projects which will let me

  • 0

I’m working on a simple logger wrapper for my projects which will let me to easily swap out the backend.
This is my ideal interface:

log::error << "some" << " log " << "message";

The way I implemented it was:

  1. log::error#operator<< returns a temporary Sink object.

  2. Sink#operator<< returns *this and defines a move constructor.

  3. The complete message can be utilized in Sink‘s destructor which is called at the end of the invocation chain.

Contrived Implementation:

#include <iostream>
#include <string>

struct Sink {

  Sink (std::string const& msg) : m_message(msg) {}

  // no copying
  Sink (Sink const& orig) = delete;

  // move constructor
  Sink (Sink && orig) : m_message(std::move(orig.m_message)) {};

  // use the complete string in the destructor
  ~Sink() { std::cerr << m_message << std::endl;}

  Sink operator<< (std::string const& msg) {
    m_message.append(msg);
    return std::move(*this);
  }

  std::string m_message;
};

struct Level {
  Sink operator<< (std::string const& msg) { return Sink(msg); }
};

int main() {
  Level log;

  log << "this" << " is " << "a " << "test";
}

This works fine except I need a clean way of disabling logging.
If I wasn’t using chaining, my log function could use a pre-processor directive to remove the function’s content

void log (std::string) {
  #ifdef LOGGING_ENABLED
    // log message
  #endif
}

The compiler would then optimize and remove the empty function call. But I don’t know how I’d do that with the api I’m trying to achieve. I know it’s possible because glog does it somehow.

Using directives like this defeats the purpose of having a nice api.

#ifdef LOGGING_ENABLED
  log << "this" << " is " << "a " << "test";
#endif

What is a clean way of disabling these types of chained calls?
Any help is appreciated.

  • 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-15T06:34:25+00:00Added an answer on June 15, 2026 at 6:34 am

    You have to imlement another Sink which does nothing when logging. Glog calls this a null-stream:

    // A class for which we define operator<<, which does nothing.
    class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream {
     public:
      // Initialize the LogStream so the messages can be written somewhere
      // (they'll never be actually displayed). This will be needed if a
      // NullStream& is implicitly converted to LogStream&, in which case
      // the overloaded NullStream::operator<< will not be invoked.
      NullStream() : LogMessage::LogStream(message_buffer_, 1, 0) { }
      NullStream(const char* /*file*/, int /*line*/,
                 const CheckOpString& /*result*/) :
          LogMessage::LogStream(message_buffer_, 1, 0) { }
      NullStream &stream() { return *this; }
     private:
      // A very short buffer for messages (which we discard anyway). This
      // will be needed if NullStream& converted to LogStream& (e.g. as a
      // result of a conditional expression).
      char message_buffer_[2];
    };
    
    // Do nothing. This operator is inline, allowing the message to be
    // compiled away. The message will not be compiled away if we do
    // something like (flag ? LOG(INFO) : LOG(ERROR)) << message; when
    // SKIP_LOG=WARNING. In those cases, NullStream will be implicitly
    // converted to LogStream and the message will be computed and then
    // quietly discarded.
    template<class T>
    inline NullStream& operator<<(NullStream &str, const T &) { return str; }
    

    In your case, a simple implementation would look like

    #ifdef LOGGING_ENABLED
      /* your sink */
    #else
      struct Sink {
        Sink (std::string)  {}
        Sink (Sink const& orig) {};
      };
      template <typename T> Sink operator<<(Sink s, T) { return s; }
    #endif
    

    This is very simple and can be optimized away from the compiler.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.