Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. STL, "upgrade" from sprintf to ostream in a logclass

STL, "upgrade" from sprintf to ostream in a logclass

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++xml
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    justin223
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • J justin223

      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

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      try overloading the "<<" or ">>" operators.

      J 1 Reply Last reply
      0
      • A Anonymous

        try overloading the "<<" or ">>" operators.

        J Offline
        J Offline
        justin223
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups