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

  • Home
  • SEARCH
  • 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 3958338
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:35:54+00:00 2026-05-20T02:35:54+00:00

I have a logging class which has operator<< overloaded. So I can do things

  • 0

I have a logging class which has operator<< overloaded. So I can do things like this:

oLogger << "Log this" << " and this" << " and " << 10 << endl;
oLogger`<< "Something else" << endl;

The logger does this without any problems. But, I want the logger object to be shared among threads. Then, I don’t want it printing out something like this:

//LogFILE
Log this and this Something else
 and 10

So, I need to lock a whole chain of operator<<s. I am guessing this can be done with RAII, I haven’t given it much thought yet. In the meantime, is there any traditional way of getting this done? (Except ending input with a manipulator?)

  • 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-20T02:35:54+00:00Added an answer on May 20, 2026 at 2:35 am

    Slight alternative to Nim’s answer:

    Create

    class LockedLog {
        static MutEx mutex; // global mutex for logging
        ScopedLock lock; // some scoped locker to hold the mutex
        Logger &oLogger; // reference to the log writer itself
    public:
        LockedLog(Logger &oLogger) : oLogger(oLogger), lock(mutex) {}
        template <typename T>
        LockedLog &operator<<(const T &value) { oLogger << value; return *this; }
    };
    

    And either just do:

    LockedLog(oLogger) << "Log this" << " and this " << " and " << 10 << endl;
    

    Or change Logger::operator<< to normal method, call this method in LockedLog::operator<<, add cast-operator to Logger:

    operator LockedLog() { return LockedLog(*this); }
    

    and that should add locking to your current code.

    Update: That locks across all the calls to operator<< and may even lock around evaluation of their arguments (depends on whether compiler will evaluate left or right argument first and it may choose). To reduce that, one could:

    class LockedLog {
        static MutEx mutex; // global mutex for logging
        std::stringstream buffer; // temporary formatting buffer;
        Logger &oLogger; // reference to the log writer itself
    public:
        LockedLog(Logger &oLogger) : oLogger(oLogger), lock(mutex) {}
        template <typename T> 
        LockedLog &operator<<(const T &value) { buffer << value; return *this; }
        ~LockedLog() { ScopedLock lock(mutex); oLogger << buffer.str() << std::flush; }
    };
    

    But the stringstream adds another overhead.

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

Sidebar

Related Questions

I'm looking at making a logging class which has members like Info, Error etc
I have an instance of a logging class logger, this class has a function
I have a logging table which has three columns. One column is a unique
I have a framework library which would do a lot of things including logging.
I have an ASP.NET MVC app which has a bootstrapper class that configures log4net
Hi I am trying to build an application which has models resembling something like
I have a class (for logging) which derives from std::ostream. Stripped down, it looks
So I'm creating this class that has two buttons, one for logging in, and
At the company I work for, I have created a Error Logging class to
I have an application for which log4j logging is configured in a log4j.properties file.

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.