What i’m trying to do is:
I want to redirect my error message erither to std::cerr or to a file
depending upon the command-line argument. If there is no log file provided then the program should output the error message on screen.
Here is my approach:
class A {
void SetLogFileStream(T& err_outstream);
};
//main.cpp
A a;
std::string filename;
T1* t1;
if(argc>2) {
filename = argv[1]; //if provided by command line
std::ofstream fp(filename);
t1 = &fp;
}
else {
std::ostream* err_outstream = &std::cerr;
t1 = err_outstream;
}
a.SetLogFileStream(t1);
what should be the argument type of the function SetLogFileStream
or the type T1
so that i can pass a pointer either to file or to std::cerr
Declare the method as this:
There are several problems with your code. The file stream opened gets out of scope and is destroyed. You need to fix it like this: