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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:42:58+00:00 2026-05-27T19:42:58+00:00

I am trying to do a simple logging library only for my own. I

  • 0

I am trying to do a simple logging library only for my own. I know there exists several once, but I have not found any header-only, small and very “c++” like logging library which fits into my application.

Currently I have the following syntax:

logger << debug << "A debug message" << end; //debug and end is my custom manipulators

I have implemented all necessary operator<< and it works great, specially when it have backward compatibility with std::ostream. But I wonder, just for efficiency if it is a why to stop evaluate anything if some message should not be logged (after debug in the example)? Making everything after the severity manipulator “disappear”?

Just now do I have the following code in short:

template <typename Type>
Logger & Logger::operator<<(const Type & message)
{
    if(this->get_message_level() <= this->get_severity())
    {
        BOOST_FOREACH(std::ostream* stream, this->_sinks)
        {
            *stream << message;
        }
    }
    return *this;
}

Logger & Logger::operator<< (Logger & (*pf)(Logger&))
{
    return pf(*this);
}

Logger & debug(Logger& logger)
{
    logger.lock();
    logger.set_severity(7);
    //...
    return logger;
}

Logger & end(Logger& logger)
{
    logger << std::endl;
    logger.unlock();
    return logger;
}

Thanks in 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-27T19:42:59+00:00Added an answer on May 27, 2026 at 7:42 pm

    It can be a bit tricky, depending on what compromizes you’re willing to
    accept in the syntax. If you really want to support everything that
    outputting to an ostream does, then the best you can do (as far as I
    know) is a wrapper class, along the lines of:

    class Logger
    {
        std::ostream* myDest;
    public:
        //  Appropriate constructors...
        template<typename T>
        Logger& operator<<( T const& obj )
        {
            if ( myDest != NULL )
                (*myDest) << obj;
            return *this;
        }
    };
    

    If logging is turned off (Logger::myDest == NULL), none of the
    conversion code will execute, but you’ll still evaluate each of the
    arguments. In my experience, this is not usually an issue, since most
    of the arguments are either string literals or a simple variable, but
    it’s not a total 0 cost. It also has the potential disadvantage that
    the propagated type is not std::ostream& (although I’ve never found
    this to be a problem in practice).

    A somewhat tricker solution would be to use a macro along the lines of:

    #define logger loggerInstance != NULL && (*loggerInstance)
    

    This will still allow most of the actual uses with the same syntax
    (because the && operator has very low precedence), but could fail in
    cases where someone has tried to be too clever, and embedded the logging
    output in a more complicated expression. In addition to not doing the
    conversions, the arguments are not even evaluated if logging is turned
    off.

    Finally, if you accept a different syntax, you can write something like:

    #ifndef NDEBUG
    #define LOG(argList) logger << argList
    #else
    #define LOG(argList)
    #endif
    

    This requires the user to write LOG("x = " << x), instead of log <<
    "x = " << x
    , and requires recompiling if you want to turn logging on,
    but it is the only solution I know which has absolute 0 cost if logging
    is turned off.

    In my experience, most applications can support the first solution; in a
    very few cases, you might want to use the second; and I’ve never seen an
    application where performance required the third.

    Note that even with the first, you’ll probably want to use a macro to
    get the logger instance, in order to automatically insert __FILE__ and
    __LINE__, and that in the second, you’ll probably still want to use a
    wrapper class, in order to ensure a flush in the destructor; if the
    application is multithreaded, you’ll want the wrapper in all cases, in
    order to make the entire sequence of output atomic.

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

Sidebar

Related Questions

I am trying something very simple, but for some reason it does not work.
I'm new to powershell, but I'm trying to output some simple logging in a
I'm new to powershell, but I'm trying to output some simple logging in a
I'm trying to setup a simple logging framework in my shell scripts. For this
It should be a straight forward and simple answer, but I'm trying to figure
I'm trying to get a simple REST service to work with Spring 3.0 but
I'm trying to implement a simple login system in Rails, but when I try
I trying to incorporate a simple error logging into my existing app, at the
I'm trying to do a simple redirection after logging the user. I thought I
We're trying to create a simple to use webservice for logging exceptions and messages.

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.