c++11 - << operator overloading in C++ for logging purposes -


i have c++ class place many std::cout statements print informative text messages mass of signals class handling. intentition redirect these text messages function named log. in function, have flag named mverbose defines if log text should printed. content of function follows:

 void xxxproxy::log(std::stringstream& ss)  {    if(mverbose)    {      std::cout << ss;      ss << "";    }  } 

then, caller code snippet function follows: std::stringstream logstr;

 logstr << "se"         << getaddr().tostring()         << ": waiting on epoll..."         << std::endl;        log(logstr); 

i overload << operator in xxxproxy in way can rid of creating std::stringstream object , calling log function. want able log text messages below , let << operator aggregate into:

 << "se"  << getaddr().tostring()  << ": waiting on epoll..."  << std::endl;       

so wouldlike have member << function looks like:

 void xxxproxy::operator << (std::stringstream& ss)  {    if(mverbose)    {      std::cout << ss;      ss << "";    }  }  

question

i relatively novice c++ developer , lots of compilation errors when attemting write above stated << operator. please make suggestions or direct me links me correctly implement << operator. thanks.

if don't want use std::cout directly, want have own log class, implement simple wrapper providing same interface of std::ostream: operator<<:

class log { private:      std::ostream& _out_stream;      //constructor: user provides custom output stream, or uses default (std::cout).     public: log(std::ostream& stream = std::cout): _out_stream(stream) {}       //implicit conversion std::ostream     operator std::ostream() {         return _out_stream;     }       //templated operator>> uses std::ostream: has defined      //an operator<< std::ostream (everithing "printable" std::cout      //and colleages) can use function.         template<typename t>      log& operator<< (const t& data)      {         _out_stream << data;     } } 

so if implement std::ostream& operator>>(std::ostream& os , const yourclass& object) classes, can use log class.
advantage of approach use same mecanism make std::cout << your_class_object work, , make class work log.

example:

struct foo {     int x = 0; //you marked question c++11, in class initializers                 //are allowed.       //std::ostream::operator<< overload foo:     friend std::ostream& operator<<(std::ostream& os , const foo& foo)     {         os << foo.x;     } };  int main() {   log my_log;   foo my_foo;    my_foo.x = 31415;    my_log << my_foo << std::endl; //this prints "31415" using std::cout. } 

possible improvements:

  • you write extern const of class log, , make class implement singleton. allows access log everywhere in program.
  • its common in log outputs have header, log output (17:57): log message. that, use std::endl sentinel; , store flag says when next output beginning of line (the beginning of log message). ckeckout the next answer complete , working implementation.

reference:


Comments