[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
Re: Log class problems
Forgot to add a damn class - sorry :)
--
Alexey Parshin,
http://www.sptk.net
#include <iostream>
#include <strstream>
using namespace std;
class CLogStreamBuf : public strstreambuf {
iostream *m_parent;
char *m_start;
char *m_end;
protected:
virtual int_type overflow(int_type c);
virtual streamsize xsputn(const char_type* s, streamsize n);
public:
CLogStreamBuf() { m_start = m_end = 0L; }
void setParent(iostream *parent) { m_parent = parent; }
~CLogStreamBuf() { cout << "Destructor" << endl; }
};
class CLog : public iostream {
CLogStreamBuf m_buffer;
public:
CLog() : iostream(&m_buffer),ios(0) { m_buffer.setParent(this); }
~CLog() { flush(); }
};
strstreambuf::int_type CLogStreamBuf::overflow(strstreambuf::int_type c) {
int bytes = pptr() - pbase();
//cout << "in overflow, char = '" << char(c) << "' " << c << " - was " << bytes << " bytes" << endl;
strstreambuf::int_type rc = strstreambuf::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 = strstreambuf::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;
}