   Logging Class, Accepting std::endl?
  Hey all,I've got this logging class, which is just wrapping an ofstream for the time being.  It looks like thus: class CLog{public:CLog(char *pszFilename){m_ofLog.open(pszFilename, std::ios::out);}~CLog(void){m_ofLog.close();}inline CLog& operator <<(char ch){m_ofLog << ch;return *this;}// Other Definitions...private:std::ofstream m_ofLog;};How would I accept an std::endl?  I want to put a timestamp infront of the line, after an std::endl but not quite sure how to accept one to begin with?Any ideas?EDIT:  It doesn't have to take std::endl, it could take endl.  It just has to behave like std::endl would with an ostream.  I'm not quite sure on that part, how to make it take something that isn't really a variable, like the mutators for ostreams do.[edited by - GroZZleR on February 16, 2004 12:29:04 AM]
  You''ll probably want to declare your own endl manipulator. To do so, add a overload of operator<< to your CLog class that looks like:CLog & CLog::operator<<(CLog & (*pf)(CLog &)) {  pf(*this);}  Then endl''s declaration would be like:CLog & endl(CLog & log) {  // whatever endl does} ;  error C2065: ''endl'' : undeclared identifier =(I understand the idea though, just function pointer syntax is murder =P ;  Make sure you declare the endl manipulator in the correct scope. ;  I've got it right in the .h file for the time being, if thats what you're referring to. class CLog{public:CLog(char *pszFilename){m_ofLog.open(pszFilename, std::ios::out);}~CLog(void){m_ofLog.close();}inline CLog& operator <<(char ch){m_ofLog << ch;return *this;}inline CLog& operator <<(CLog &(*pf)(CLog &)){pf(*this);}CLog &endl(CLog &log){m_ofLog << std::endl; // Testreturn *this;}// Other Definitions...private:std::ofstream m_ofLog;}; [edited by - GroZZleR on February 17, 2004 1:06:35 AM];  Well, what I did was create a simple (and empty) struct named UtEndl.  It looks like this: /**  * UtEndLine  *  * a utility class that flushes an XLogSource  * and appends a new line  *  */class UtEndLine{public:};Then at the end of the header file, put this:/*! standard end line indicator */extern UtEndLine endl;And declare it in your .cpp file (just declare it in the global namespace).  Then, in your log class, put an operator overload like this:XLogSource& operator<<( UtEndLine UtEnd );   |your log class hereand whenever that function gets called, flush your stream, add a new line to it, or whatever.   Chris PergrossiMy Realm | "Good Morning, Dave"    ;  endl() shouldn''t be a member function of log. (At least not unless you change the signature of the function pointer.) ;  Ah, I'm with you now SiCrane.The problem with moving it outside of the class is that I no longer have access to m_ofLog directly.  And I'm right back to the same problem, because I can't put an std::endl in there without public access of the stream itself.c t o a n your solution is definitely 'creative' =P  Atleast I've got something to fall back if this doesn't pan out =)   [edited by - GroZZleR on February 17, 2004 1:25:00 AM];  I'd be tempted to do something like this:   class CLog {public:    CLog(const char* filename) : f(filename) { }    std::ofstream& line()    {   // print timestamp and anything else   return f;    }private:    std::ofstream f;};Or even:   class CLog {public:    CLog(const char* filename) : f(filename) { }    template <class T>    std::ofstream& operator << (T rhs)    {   // print timestamp and anything else   return f << rhs;    }private:    std::ofstream f;};[/source [edited by - Beer Hunter on February 17, 2004 1:35:06 AM];quote:Original post by GroZZleR  The problem with moving it outside of the class is that I no longer have access to m_ofLog directly.  And I''m right back to the same problem, because I can''t put an std::endl in there without public access of the stream itself.One way to handle this would be to make endl() a friend of CLog. Or you could make it a static member function of CLog. Or you can change the function pointer''s signature to be a member function. Or you can make the timestamp functionality a public member function of CLog and call that member function inside endl().... I could go on with different ways, but I''m sure you get the idea.