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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:21:00+00:00 2026-05-18T20:21:00+00:00

A related question here shows how to do this with just clog: How to

  • 0

A related question here shows how to do this with just clog:

How to redefine clog to tee to original clog and a log file?

The question now is how to also do this for cerr at the same time. With the above question, output to cerr does not end up in the log file where it is also needed.

The goal is that whatever goes to either clog or cerr ends up in the log file once, so both clog and cerr need to be teed to a shared log file.

  • 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-18T20:21:01+00:00Added an answer on May 18, 2026 at 8:21 pm

    this code will redirect both std::cout and std::cerr to an output file :

    // create an output stream
    std::ofstream trace_log ( "/tmp/foo.log" );
    
    // connect stream buffers
    std::streambuf *coutbuf = std::cout.rdbuf();
    std::cout.rdbuf(trace_log.rdbuf () );
    
    std::streambuf *cerrbuf = std::cerr.rdbuf();
    std::cerr.rdbuf(trace_log.rdbuf () );
    
    // log 
    std::cout << "cout here" << std::endl;
    std::cerr << "cerr here" << std::endl;
    
    // restore
    std::cout.flush ();
    std::cout.rdbuf(cerrbuf);
    
    std::cerr.flush ();
    std::cerr.rdbuf(cerrbuf);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A related question here shows how to do this with just clog: How to
This question is related to the question posted here: Why isn't my custom WCF
This is related to my previous question here . I want 4 divs (absolute
Ok, so the initial search in StackOverflow shows nothing related for this question. So
I asked and answered a question here that is related to this issue and
This problem is related to this question. Here is the data type I wish
This is slightly related to the question asked here yet the answer does not
I have seen the related question here: An object with the same key already
I'm building a custom autoloader based on Zend Framework's autoloading (related question here ).
Related question [stackoverflow] here . I'm trying to do the above, but I want

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.