STL, "upgrade" from sprintf to ostream in a logclass
-
Hello I've written a logfile class that writes the logentries in xml format. A typical usage: mylog.WriteLog(LOG_PRIO_HIGH, "Agroup", "This is a text with %d some %s vars", 34, "diffrent"); output This is a text with 39 some diffrent vars I want to accomplish the same thing by using streams/stringbuf. But I've not found any good examples that show me how I should do. What i've found is a couple of examples that derives a class from stringbuf and from ostream. I would be nice of I can get something like this: log << log_prio(1) << log_group("test") << "hello" << nIntVar << strBuf << log_end; log << "this is a string"; log << " something more" << log_end; log << log_prio(2) << "last line" << log_end; would look like: hello1yeye this is a string something more last line How do I accomplish this? Thanks, Jonas
-
Hello I've written a logfile class that writes the logentries in xml format. A typical usage: mylog.WriteLog(LOG_PRIO_HIGH, "Agroup", "This is a text with %d some %s vars", 34, "diffrent"); output This is a text with 39 some diffrent vars I want to accomplish the same thing by using streams/stringbuf. But I've not found any good examples that show me how I should do. What i've found is a couple of examples that derives a class from stringbuf and from ostream. I would be nice of I can get something like this: log << log_prio(1) << log_group("test") << "hello" << nIntVar << strBuf << log_end; log << "this is a string"; log << " something more" << log_end; log << log_prio(2) << "last line" << log_end; would look like: hello1yeye this is a string something more last line How do I accomplish this? Thanks, Jonas
-
Yep. I've started to do so.
class CDtLog { private: std::ostringstream m_os; bool bNewEntry; int m_nPrio; char* m_szGroup[40]; public: CDtLog() { bNewEntry = true; m_nPrio = 2; m_szGroup[0] = NULL; } template CDtLog &operator<<(const T &item) { if (bNewEntry) { m_os << ""; bNewEntry = false; } m_os << item ; return *this; }; void print() { printf(m_os.str().c_str()); } }; int _tmain(int argc, _TCHAR* argv[]) { CDtLog test; int i = 10; test << "hejsan" << std::setfill(' ') << std::setw(10) << 12 << i; test.print(); return 0; }
I need a modifier that tells me that a debugentry is done, similiar to endl, but instead of just flushing, it ends the xml tag and then flush it to the file. I also need to check every entered character to see if it's a < or >, if so I need to translate it to > or <, or it will ****up the xml. I also want modifiers that I can use to change the prio and group.