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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:52:42+00:00 2026-05-15T14:52:42+00:00

i would like to create a flexible logger class. I want it to be

  • 0

i would like to create a flexible logger class. I want it to be able to output data to a file or to standard output. Also, i want to use streams. The class should look something like:

class Logger
{
private:
   std::ostream m_out; // or ofstream, iostream? i don't know
public:

   void useFile( std::string fname);
   void useStdOut();

   void log( symbol_id si, int val );
   void log( symbol_id si, std::string str );
   //etc..
};

The symbol_id is an enum and defines the formatting. What i want to achieve is to be able to easily switch from standart output to a file and vice versa (this is the purpose of the use* methods). Preferably by just using m_out and simply writing m_out << "something"; without any checks whether i want to write to a file or stdout.

I know there are many ways how to get around this (using if's to test if i want to write to a file or stdout, the “C way” (using FILE* and fprintf)) and so on, but i’m sure there must be a way how to achieve this with C++ streams in a nice way. But i can’t seem to find the way how to do it. Can somebody help me please?

  • 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-15T14:52:43+00:00Added an answer on May 15, 2026 at 2:52 pm

    The way I’ve attacked this problem before is to make Logger an abstract base class and create separate FileLogger and OutStreamLogger classes. Then create a CompositeLogger object that implements the Logger interface, which just outputs all loggers:

    CompositeLogger compLogger;
    compLogger.Add(new FileLogger("output.txt"));
    compLogger.Add(new StreamLogger(std::cout));
    ...
    compLogger.log(...);
    

    If you don’t need this level of flexibility and want to keep all this in a single class you could make the m_Out variable a pointer to std::ostream and add an extra flag to keep track of whether you need to delete it on cleanup:

    private:
      std::ostream*   m_out;
      bool            m_OwnsStream;
    
    Logger() {
       m_Out=&std::cout; // defaults to stdout
       m_OwnsStream=false;
    }
    void useFile(std::string filename) {
      m_Out=new std::ofstream(filename);
      m_OwnsStream=true;
    }
    ~Logger() {
      if (m_OwnStream) 
        delete m_Out; 
    }
    

    Obviously you’d need some more checks in useFile and useStdOut to prevent memory leaks.

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

Sidebar

Related Questions

I am wondering, how to create flexible package structure for an Android application, such
I'm trying to create a PHP Session Wrapper Class and to do it -
We are evaluating Windows Workflow Foundation 4 to use in MVC 3 based Web
I have a fairly standard Java EE6 web application using JPA 2 with dependency
My code is like this: [Serializable] [StructLayout(LayoutKind.Sequential,Pack=1)] struct Foo { public byte Bar; public
I'm recording audio on an iPhone, using an AVAudioRecorder with the following settings: NSMutableDictionary
EDIT: looking for this: http://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (If this works I'll answer my own question) I've
Currently working on a 3D media engine using C# and I have come across
Simple question that I couldn't figure out how to do so far in the
Can you comment on the below design. Am I setting myself up for destruction

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.