Search code examples
c++loggingcoutostream

C++ Logging using std::ostream and std::cout


I want to write a program and enable the user to have control over the logging of the package by setting the program to display progress or disable that.

I know that std::cout is an std::ostream with the difference that std::cout redirects the results to standard output.

I want my class to have an std::ostream member that I log everything to. Then if the user enables display, that member will be attached to std::cout and displays the results, otherwise it wont.

What I have in mind is something similar to this:

class log {
private:
    std::ostream display;
public:
    void func();
    void show_display();
}

void log::func(){
    display << "called by func";
}
void log::show_display(){
    // redirect display to standard output
}

Is there any way similar to above to do this? If not, how can I have similar results?

Thanks.


Solution

  • Stream handles aren't copyable objects, nor are there any std::ostream objects that you the user can instantiate. So your class as written cannot work. What you could do instead is store a pointer:

    #include <iostream>
    
    class log {
      std::ostream* out_;
    
    public:
      void func() {
        if (out) { *out << "called by func"; }
        // ...
      }
    
      void show_display() {
        out = &std::cout;
      }
    
      void hide_display() {
        out = nullptr;
      }
    };
    

    If you plan to build a more general-purpose logging system, you should consider whether logging to a disabled output requires evaluation of operands; users may expect that a log statement such as log << expensive_function(); is cheap when the logging is disabled.