logging - Design a C++ Logger with operator '<<' -


i designing c++ asynchronous lightweight logger. rough implementation below,

ilogger log(filename, logginglevel::warn) log << "hello" << "this sample warn logging"; 

i using concurrent queue, upload logging messages concurrent queue , thread take messages out of , write file.

i have problem though,

my overloaded operator follows,

ilogger& operator<< (string s) {     logstr += s;     return *this;     // there should point instead of returning,     // should call queue.add(logstr); } 

the problem don't know when stop appending logstr , flush string concurrent queue.

i donot want delimiter endlog log ended. there way determine end of "<<" without having special delimiter

you can create helper friend class

class logstream {     ilogger&            log_;     std::ostringstream  logs_; public:     logstream(ilogger& log) : log_(log) {     }     ~logstream() {         log_.queue.add(logs_.str());     }     template<typename t>     logstream& operator<<(const t& val) {         logs_ << val;         return *this;     } }; 

and use logstream object instead of operator <<

logstream ilogger::getstream() {     return logstream(*this); } 

so logging statement

log.getstream() << "hello" << "this sample warn logging"; 

will create temporary logstream object accept 2 strings , deleted (and append result string logger queue in destructor) before next statement.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -