T
Thomas Matthews
Hi,
My objective is to add enable & disable functionality to
the ofstream class. I want to have a log file where I
can disable and enable output to it. When my first
tests pass, I want to disable the annotations written
to the log file, but enable the text from the newer
test cases.
I've searched the C++ newsgroups and the FAQ-Lite
and didn't find anything relevant.
"The C++ Standard Library", by Josuttis, shows how
to modify an ostream class. I've successfully implemented
the enable/disable functions using a streambuf and
attached it to an ostream class.
I've also looked at Dietmar Kuhl's site and his
site explains how to modify an ostream but not an
ofstream.
However, I would like to add the functionality to
the file stream. The ofstream class does not provide
a constructor with a streambuf parameter (at least
Borland's compiler doesn't).
In summary, I would like to either subclass the
ofstream class or assign my streambuf to an
instance of the ofstream class. How do I go
about this?
#include <iostream>
#include <fstream>
#include <streambuf>
#include <cstdlib>
using namespace std;
class logfile
: public streambuf
{
public:
logfile()
: enabled(true)
{logstream.open("log.txt");}
bool enabled;
ofstream logstream;
protected:
virtual int_type overflow (int_type c)
{
if (enabled && (c != EOF))
{
logstream << c;
}
return c;
}
virtual streamsize xsputn(const char * s, streamsize num)
{
if (enabled)
{
logstream.write(s, num);
}
return num;
}
};
int main(void)
{
logfile lf;
ostream out(&lf);
out << "This should be printed.\n";
lf.enabled = false;
out << "This should not be printed.\n";
lf.enabled = true;
out << "After re-enabling.\n";
return EXIT_SUCCESS;
}
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
My objective is to add enable & disable functionality to
the ofstream class. I want to have a log file where I
can disable and enable output to it. When my first
tests pass, I want to disable the annotations written
to the log file, but enable the text from the newer
test cases.
I've searched the C++ newsgroups and the FAQ-Lite
and didn't find anything relevant.
"The C++ Standard Library", by Josuttis, shows how
to modify an ostream class. I've successfully implemented
the enable/disable functions using a streambuf and
attached it to an ostream class.
I've also looked at Dietmar Kuhl's site and his
site explains how to modify an ostream but not an
ofstream.
However, I would like to add the functionality to
the file stream. The ofstream class does not provide
a constructor with a streambuf parameter (at least
Borland's compiler doesn't).
In summary, I would like to either subclass the
ofstream class or assign my streambuf to an
instance of the ofstream class. How do I go
about this?
#include <iostream>
#include <fstream>
#include <streambuf>
#include <cstdlib>
using namespace std;
class logfile
: public streambuf
{
public:
logfile()
: enabled(true)
{logstream.open("log.txt");}
bool enabled;
ofstream logstream;
protected:
virtual int_type overflow (int_type c)
{
if (enabled && (c != EOF))
{
logstream << c;
}
return c;
}
virtual streamsize xsputn(const char * s, streamsize num)
{
if (enabled)
{
logstream.write(s, num);
}
return num;
}
};
int main(void)
{
logfile lf;
ostream out(&lf);
out << "This should be printed.\n";
lf.enabled = false;
out << "This should not be printed.\n";
lf.enabled = true;
out << "After re-enabling.\n";
return EXIT_SUCCESS;
}
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book