There is a certain program in which there are numerous conclusions to the console of various data for diagnostics, sometimes there are a lot of them, sometimes not. I want to make it possible to disable the debug print of the bool variable, but it turns out an ugly code in each such message if (bool){cout << "blablabla";} .

Actually the question is how to implement such a function, given that the variables in cout can be a different number (1-99) and different types ( int , float , string , etc.)?

I tried something like this:

 void Comment(string Args, ...) { тут вывод } 

But it is necessary to output both int and float variables and it is logical that errors appear ...

  • you can try to do something like this. stackoverflow.com/questions/519741/... or something similar, or just want a function? - pavel
  • one
    Maybe you need a null-object pattern? - VladD
  • I think you should try to do it in a manner similar to the implementation of printf, when one of the parameters passes a format string, parsing which we can determine the number of arguments and their types (and process them in the appropriate way Args ...) - goldstar_labs
  • Can you do a little more with null-object? I'm not a big specialist in OOP = ( - Neuromanser

1 answer 1

You can write your thread logger that will wrap std :: cout (or any other stream). Make it a state to write to the log or not, and to overload the operator << in which the output to the monitored stream will be only if the flag is in the desired state. Like this:

 class Log { public: Log(std::ostream& os, bool log = true) : _os(os), _log(log) {}; template<typename T> Log& operator<<(const T& obj) { if (_log) _os << obj; return *this; } void Switch() { _log = !_log; } private: std::ostream& _os; bool _log; }; 

Example of use:

 Log log(std::cout); //Can be any output stream. ie file stream, string stream etc log << "a"; log << 2; log.Switch(); log << "A" << 10 << "\n"; log.Switch(); log << "C" << 50 << "\n"; 

Output

 a2C50 
  • Thanks for the help! But still, it seems to me that this does not solve the problem. After all, in fact, I switch the log code, which cumbersome this very code and creates a large number of conditional statements (if turned on, turned off, switch on, etc., etc.), and the goal is to make just one single point to turn on \ turning off the entire log in the program. Well ... The class you have written for me is still very difficult, but I will try to understand in detail. Thank! - Neuromanser
  • @Neuromanser well, actually the log is initialized 1 time. You can hard-code it in cout instead of _os and turn off the output right here. - pavel
  • @pavel has already figured out less, thanks! Hastened with the comment :) - Neuromanser
  • @Neuromanser 1) This class is only a wrapper over the output stream, respectively, you can create an object of this class anywhere, different accessories of the Log object can exist in parallel and do not block each other. Create several objects in different places, or one global. 2) Hardcode is a pretty bad idea then. - Andrey Buran