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 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 a modular structure for my zend framework application. This is
I have been using the CSLA framework for couple of years now for windows
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 following code for loading JQuery in all of my
I have been using the python logging module for quite some time now, but
I have been using this function for a while without a problem, although I
I have been using something like this: int main(int argc, char *argv[]) { #ifdef
I have been using this piece of script to load external .js files to
I have been using Nancy Framework for my C# Applications to create Web-Based GUI's.

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.