I am trying to create a Log class for my project at school. It needs to either be able to write information to the stdout or to a file depending on the parameters it is passed. I was looking into how to do this and I stumbled upon a thread with a similar question here: Obtain a std::ostream either from std::cout or std::ofstream(file)
The only difference between this thread and my own is that I want to do it inside of a class. Looking at the solution though they use std::ostream out(buf) and construct the ostream on the fly with buf. How can i declare this properly in my Log class to be able to construct the "out" object only once i enter my Log constructor?
I took a quick stab at it below but I am not sure if it is correct or if I am on the right track. Appreciate any help, thanks.
EDIT: I want to be able to do out << "Some string" << endl; after i get this Log class working properly.
EDIT2: An error I am now receiving with the new code below error : 'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char, _Traits = std::char_traits<char>]' is protected
// log.h #include <string> #include <fstream> #ifndef LOG_H_ #define LOG_H_ class Log { public: enum Mode { STDOUT, FILE }; // Needed by default Log(const char *file = NULL); ~Log(); // Writing methods void write(char *); void write(std::string); private: Mode mode; std::streambuf *buf; std::ofstream of; std::ostream out; }; #endif // log.cpp #include "log.h" #include <iostream> #include <stdlib.h> #include <time.h> Log::Log(const char *file) { if (file != NULL) { of.open(file); buf = of.rdbuf(); mode = FILE; } else { buf = std::cout.rdbuf(); mode = STDOUT; } // Attach to out out.rdbuf(buf); } Log::~Log() { if (mode == FILE) of.close(); } void Log::write(std::string s) { out << s << std::endl; } void Log::write(char *s) { out << s << std::endl; }
char *file = NULLor something similar, so that you don't have to pass any arguments in case of theLogto stdout. - And give your default arguments in the header file, not the implementation [you need them in any code that calls Log(), which is probably not your "log.cpp" or whatever it may be called.