I'm developing a log lib for myself, and want it can be used in a way like the style of iostream. For example:
log_debug << "Log body strings..." << endlog;
instead of:
log_debug( "Log body strings..." );
My codes is here:
class Log_t {
public:
friend Log_t& endlog( Log_t& rp_Logger );
friend Log_t& operator<<( Log_t& rp_Logger, const char* p_pchBody );
private:
std::stringstream m_ssBuffer;
};
Log_t& endlog( Log_t& rp_Logger ) {
std::cout << rp_Logger.m_ssBuffer.str() << std::endl;
rp_Logger.m_ssBuffer = std::stringstream();
return rp_Logger;
};
Log_t& operator<<( Log_t& rp_Logger, const char* p_pchBody ) {
rp_Logger.m_ssBuffer << p_pchBody;
return rp_Logger;
};
int main() {
Log_t log;
log << "Hello Logger!" << endlog;
return EXIT_SUCCESS;
};
These codes can not pass through compilation, and I got "no match for ‘operator<<’ (operand types are ‘Log_t’ and ‘Log_t&(Log_t&)’)".
I can't found a way to tell out the end of a single log, while with the style of calling-a-function, it is not a problem.
As calling-a-function: log_debug( "Log body strings..." );
, the end of a log has been impied by calling. --One calling, One log line--
But in the style with "<<" overloading, we can not tell where is the end of one individual log, because follows should be valid too:
log_debug << "Log " << "body " << "goes " << "here...\n"
<< "the rest of the same log goes here."
<< endlog;
It is why I coded a function "endlog()", neither for insertint a "\n" character, nor for flushing IO, rather in order to tell out "here is an end of one single log".
Would anyone please help me? Sorry for my poor English.