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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:13:53+00:00 2026-05-24T18:13:53+00:00

So I have such log class: #include <iostream> #include <sstream> #include <boost/circular_buffer.hpp> #include <boost/foreach.hpp>

  • 0

So I have such log class:

#include <iostream>
#include <sstream>
#include <boost/circular_buffer.hpp>
#include <boost/foreach.hpp>

class FlushInternal;

class Log
{
public:

    static FlushInternal* endl;

    Log(int log_length)
    {
        i = 0;
        messages_buffer = new boost::circular_buffer<std::string>(log_length);
    }

    template <class T>
    Log &operator<<(const T &v)
    {
        current_message << v;
        return *this;
    }

    Log &operator<<(std::ostream&(*f)(std::ostream&)) 
    {
        current_message << *f;
        return *this;
    }

    Log &operator<<(FlushInternal*)
    {
        ++i;
        messages_buffer->push_back(current_message.str());
        clean_stringstream(current_message);
        is_filled();
        return *this;
    }

    boost::circular_buffer<std::string> *messages_buffer;

private:
    int i;
    std::stringstream current_message;

    void is_filled()
    {
        if (i >= messages_buffer->capacity())
        {
            i = 0;
            BOOST_FOREACH(std::string s, *messages_buffer)
            {
                std::cout << ++i << ": "  << s << " ;" << std::endl;
            } 
            i = 0;
        }
    }

    void clean_stringstream(std::stringstream &message)
    {
        message.flush();
        message.clear();
        message.seekp(0);
        message.str("");
    }
};

FlushInternal* Log::endl = 0;

And I can Use it like this:

#include <log.h>
int main()
{
    Log l(2);
    l << "message one: " << 1 << Log::endl;
    l << "message two:" << " " << 2 << Log::endl;
    l << "message " << "three: " << 3 << Log::endl;
    l << "message" << " "  << "four: " << 4 << Log::endl;
    std::cin.get();
}

This would output:

1: message one: 1 ;
2: message two: 2 ;
1: message three: 3 ;
2: message four: 4 ;

As you can see I can have as many << as I want inside each log message. I want to be capable to use one instance of Log class from many threads at the same time. So I would have something like (pseudocode that compiles, runs but traces nothing.):

#include <boost/thread.hpp>
#include <log.h>

Log *l;

void fun_one()
{
    *l << "message one: " << 1 << Log::endl;
    *l << "message two:" << " " << 2 << Log::endl;
}

void fun_two()
{
    *l << "message " << "three: " << 3 << Log::endl;
    *l << "message" << " "  << "four: " << 4 << Log::endl;
}

int main()
{
    l = new Log(2);
    boost::thread(fun_one);
    boost::thread(fun_two);
    std::cin.get();
}

So as you can see I want messages to be inserted into log in multythreaded function. Lo I wonder – how to make my log cclass support this?

  • 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-24T18:13:55+00:00Added an answer on May 24, 2026 at 6:13 pm

    The approach linked by trojanfoe is pretty much the canonical one. Basically create some temporary thing for the leftmost << operator, accumulate everything, and output the message in the destructor for the temporary thing.

    The only question is the exact mechanics of this accumulator. The example used ostringstream, but I’ve seen the ofstream for the log file used directly as well (requires locking to ensure the output ends up on one line).

    Creating ostringstreams is relatively expensive on some platforms, because they may need to lock and copy some internal locale related things. You could re-implement also the << operator for interesting types, but I’d test the ostringstream approach first.

    A useful optimization is determine at the point of the construction of the temporary whether the trace will be emitted (e.g., whether tracing is enabled at that particular level), and not create the guts of the temporary at all in that case – all the insertion operations will be no-ops.

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

Sidebar

Related Questions

I have objects such as these: class Log { public matches; public function __construct()
I have such a class: public class Cycle { public List<int> Edges { get;
I have such class public unsafe class EigenSolver { public double* aPtr {get; private
I have a directory full of log files in the form ${name}.log.${year}{month}${day} such that
I have such html and css. <div class=selected> <div class=text>First</div> <div class=arrow>&nbsp;</div> </div> .selected
Suppose I have written such a class (number of functions doesn't really matter, but
I have a simple web service like this: @WebService public class MyWebService { @WebMethod
I have login view that takes a LoginPageViewModel: public class LoginPageViewModel : PageViewModel {
I have public properties of a class and my output detail line is being
I have a Runnable class like: Class R1 implements Runnable { private static final

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.