Consolidation function



  • There is a programme where there are many conclusions on the console of different data for diagnostics, sometimes very many, sometimes not. I'd like to make it possible to disconnect the alternating press. bool, but there's an incorrect code in every such message. if (bool){cout << "blablabla";}

    The question is, of course.how to perform such a function, taking into account that cout different quantities (1-99) and different types (intfloatstring etc.?

    Sold something like that:

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

    But we need to get out of here. int and float variables and logically, mistakes appear. ♪ ♪



  • You can write your ceiling logger, which will turn std:cout (or any other flow). Make him feel like writing or not, and reload the operator station into a controlled flow only if the flag is in the right state. Like:

    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. i.e. file stream, string stream e.t.c.
    log << "a";
    log << 2;
    log.Switch();
    log << "A" << 10 << "\n";
    log.Switch();
    log << "C" << 50 << "\n";

    Get out.

    a2C50


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2