I'm trying to write a logging function that multiple users can use in their functions with minimal effort (1 line required, no extra variables). Forgive me if I am completely out to lunch here. I'm still learning. I tried the Beginner forum but got no responses. Does anyone have any tips?
It needs to be able to take anything they may want to output in a cout-type style (stings and variables mixed). I'd also like to be able to add a short mask that will give me options (like colour, or outputing to a file).
Essentially I need to turn the following macro into a function (where fout is an ofstream object).
1 2 3 4
#define LOGOUT(w,m) { \
if (m & 0xf0) fout << w << endl << flush; \
if (m & 0x0f) cout << w << endl; \
}
My problem is, how can I pass a stream through to a function? I've tried using a stringstream as a way of passing this and haven't been able to get it to work.
I can see why it doesn't work but can't figure out what will work.
If you have studied classes, you should create a Logger class, not a Logger function. The logger class can then have a configurable filename, for example, that gets passed to the object in the constructor:
Logger log = "myLogFile.txt";
As for your code above, AFAIK the ostream data type (class) is an abstract one, meaning you cannot create objects of this type. You must create objects of derived types, like ofstream, stringstream and the like.