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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:40:15+00:00 2026-05-23T08:40:15+00:00

I have been using Petru’s logging framework a bit. This is part of his

  • 0

I have been using Petru’s logging framework a bit. This is part of his code:

class Output2FILE
{
public:
  static FILE*& Stream() {
    static FILE* pStream = stderr;
    return pStream;
  }
};

Which is very nice as it simply logs to stderr without any action, but with the function that can afterwards be set to anything including stdout and a file. However, I think this approach can’t be used for formatted data as one needs to use fprintf.

Hence I am trying to come up with something similar that lets one use stdout by default and that can be switched to a file, but using the “<<” operator for formatted data.

Maybe something along the lines of

std::ostream myOutput(std::cout);

with the idea of then doing myOutput << "Hello" << 1 << 1.5;. Above line however makes the compiler complain though.

What is the correct way?

Thanks!

  • 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-23T08:40:16+00:00Added an answer on May 23, 2026 at 8:40 am

    You could use a pointer to an std::ostream much like the FILE* version.

    std::ostream* os = &std::cerr;
    if (log_to_file) {
      os = new std::ofstream("my.log");
    }
    
    *os << "Hello Log!" << std::endl;
    
    if (log_to_file) { // or less 'safe' os != &std::cerr ...
        // close file here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using mySQL Quite a bit however, this is the first time
i have been using a modular structure for my zend framework application. This is
I have been using an API to do some work. This is how I
I have been using these files all day... And then suddenly this is now
I have been using this script for a while and suddenly it just stopped
I have been using the examples from this site ( http://dojotoolkit.org/documentation/tutorials/1.6/datagrid/ ) to render
I have been using explode(.,$mystring) to split a paragraph into sentences. However this doen't
I have been using git to push my local code to a server. I
I have been using namespaces to package my JavaScript code. In some of the
I have been using mplot3d (part of matplotlib) for some various 3d plotting, and

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.