Archived

This topic is now archived and is closed to further replies.

Custom C++ stream

This topic is 5382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to make a logger class that dumps messages onto the screen, and I''d like to make it work like the iostream clases. Now, unfortunetly most of my C++ resources predate the standard, and the ones that don''t stear clear of iostreams, so my understanding of this portion of C++ isn''t stellar. I''d like to be able to write this: logger << "OnConnect (0x" << hex << some_pointer << ")" << endl; It appears that flush is a function on the buffer, not on the stream object, so do I have to derive from a buffer and make my own custom buffer and stream objects? Is there a more direct way to do what I want?

Share this post


Link to post
Share on other sites
I think you can do it pretty easy :

Give Logger a member variable of type ofstream (the file to write to), then all you have to do is add a template member function to logger :


    

template<class T>
Logger & Logger::operator << (const T & t)
{
file << t; // file is the file to write to

return *this;
}




[edited by - George2 on March 23, 2003 6:50:56 AM]

Share this post


Link to post
Share on other sites
The idea with iostreams is that you don't derive your own stream classes, you derive your own streambufs. Derivation of new stream classes is generally only done to save you from having to explicitly initialise an istream/ostream yourself (which is rarely that much of a hardship, really).

For this kind of application, you only need to derive a streambuf that supports output. I give you this example copied almost verbatim from the Josuttis standard library book, "The C++ Standard Library - A Tutorial and Reference":

    
#include <iostream>

class outbuf : public std::streambuf
{
protected:
/* central output function
* - print characters in uppercase mode
*/

virtual int_type overflow (int_type c) {
if (c != EOF) {
// write the character to the standard output

if (putchar(c) == EOF) {
return EOF;
}
}
return c;
}
};

int main()
{
outbuf ob; // create special output buffer

std::ostream out(&ob); // initialize output stream with that output buffer


out << "31 hexadecimal: " << std::hex << 31 << std::endl;
}

Obviously in your application you may want to appropriately template-ize the class so it can be used for different character types and different char_traits, etc. And this is the minimal, inefficient version. You will probably want to define xsputn() too in order to accommodate efficient writing of multiple values.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]


[edited by - Kylotan on March 23, 2003 9:08:37 AM]

Share this post


Link to post
Share on other sites
No, only one stream can use a stream buffer at a time.

Also, I was intending to use a stringstream, as I do not want/need to open a file. I''m trying to put the strings on the screen in a listbox, and want to switch to the next item in the list when a flush occurs.

The stringstream classes do not appear to have a ctor that accepts a stringbuf to operate on.

Share this post


Link to post
Share on other sites
Magmai, why not just use the standard clog class? If I''m not mistaken it defaults to displaying on the screen. If you later want it redirect its output to file or debugger or messagebox, just change the streambuf on the fly like so:

clog.rdbuf(file.rdbuf());

Also, flush is a manipulator and it can be used like so:

clog << "my text" << std::flush;

However, if you use std::endl it flushes automatically.

For the record, that''s what I''m doing but I''m going to create my own log class that''s derived from a C++ stream. The reason is I''m going to include functions to allow the user to add/remove multiple streambufs so the logger can actually log to more than one location at a time (ie, screen, debugger, and to file).


- Houdini

Share this post


Link to post
Share on other sites
google @ cuj.com, I seem to remember there was some stuff on stream buffers.
Also, I think the boost file group at yahoo has examples, as does libGDN (but, I think is far more complex then what you neeed).

-----------------------------
Gamedev for learning.
libGDN for putting it all together.

Share this post


Link to post
Share on other sites