CPPSERV


Home Projects Jobs Clientele Contact

cppserv


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Log class problems



Since the attachment doesn't work - sending it directly

//#include "stdafx.h"
#include <iostream>
#include <strstream>

using namespace std;

class CLogStreamBuf : public streambuf {
   iostream *m_parent;
   char     *m_start;
   char     *m_end;
   char     *m_buffer;
protected:
   virtual int_type overflow(int_type c);
   virtual streamsize xsputn(const char_type* s, streamsize n);
public:
   CLogStreamBuf() {
       m_buffer = new char[16];
       setbuf(m_buffer,16);
   }
   ~CLogStreamBuf() {
       cout << "Destructor" << endl;
       delete m_buffer;
   }
};

class CLog : public iostream {
   CLogStreamBuf m_buffer;
public:
   CLog() : iostream(&m_buffer),ios(0) {}
   ~CLog() { flush(); }
};

streambuf::int_type CLogStreamBuf::overflow(streambuf::int_type c) {
   int bytes = pptr() - pbase();
   cout << "in overflow, char = '" << char(c) << "' " << c << " - was " <<
bytes << " bytes" << endl;
   streambuf::int_type rc = streambuf::overflow(c);
   return rc;
}

streamsize CLogStreamBuf::xsputn(const char_type* s, streamsize n) {
   int bytes = pptr() - pbase();
   char *buff = new char[n+1];
   strncpy(buff,s,n); buff[n] = 0;
   cout << "in xsputn, str = " << buff << " - was " << bytes << " bytes" <<
endl;
   delete buff;
   streamsize rc = streambuf::xsputn(s,n);

   while ( sgetc() != EOF )  {
      char ch = sbumpc();
      //if (ch == 10) cout << endl;
      //else
      if (ch >= 10)
         cout << ch;
   }
   return rc;
}

int main(int argc,char *argv[]) {
   CLog cl;
   for (unsigned i = 0; i < 4; i++)
      cl << "line" << i << " " << 12345 << " out " << endl << endl;
}



--
Alexey Parshin,
http://www.sptk.net

Authoright © Total Knowledge: 2001-2008