i am writing a logger class in c++ i want to redirect all the logs from different files to a function which decides where to write the log.
for example,
func A()
{
int a =10;
std::string b = "test";
cout<<"printing"<<a<<b<<endl;
}
func B()
{
unsigned long t = 6700;
cout<<t<<endl;
}
Instead of cout i want to have a function which takes variable arguments and sent to common function which finally prints or writes into a file. New to c++, please let me know if anyone knows how to do this
In the scope of @Repeater's tiny example program, yes.
In general, log is interested in built-in operator, and not some user overload of it. It's possible to call the user's operator, for example, if writeToLog is overloaded for a user type, or there's ADL involved:
Consider using the cerr instead stream instead of cout. This is the standard error stream. On UNIX and Linux, it defaults to the cout stream. You can redirect it on the command line:
myprog 2>myLogFile
If you must send it to a function (and about the only reason I can think of is to change the log destination at runtime), then the most flexible thing is to do is create custom streambuf class. Then you can create a logging stream that uses the streambuf and you get all the ostream goodness in the logging.
If you want to use one for real that's similar to mbozzi's excellent examples, there's this: https://github.com/gabime/spdlog (git@github.com:gabime/spdlog.git)
i'm really new in c++, but i think have an idea for the "logging" process.
The point is, to send a message -the same - to a (hard) file and - alternatively - to the console or even to both, in order to have information for debugging the behaivor of the program.
The function "works" (please see ) but by now i have to use two instructions for each message: the ostringstream assignament and the call for the function.
The control is the "int dstny" wich will say where the message should be sent. Of course i made a small version just for the case, and the "hard" file use is not shown here.