![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
Senior Member
Join Date: Sep 2005
Location: Brighton, UK
Posts: 584
|
When debugging a program I generally use a combination of breakpoints and logging. For convenience I use std::cout. It is an instance of std::ostream that redirects its input to the stdio console. std::ostream just provides operations to perform output operations on stream buffer.
Most of the time using this stream is sufficient, but sometimes, for example when working on a windows application, you don't have access to the console. Rather than change the code that relies on std::cout it is best to redirect its output. Luckily ostream has been designed to have its stream buffer changed. A simple approach is to redirect the output to a file. For example: Code:
Sometimes redirecting to a file is inconvient. To write to something else we need to create a new stream buffer. As I don't want to create to many dependencies and I'd like the streambuf to be a bit easier to use I've created a simple wrapper (its inline for brevity). Code:
This class creates buffer for storing streamed output. The class is abstract; virtual void writeString(const std::string &str) = 0; must be overriden. writeString should implement your output operation, this could write the text to an overlay on screen, perhaps some kind of console or whatever you want. At the end of a line, or when the ostream receives a flush sync is called. This causes the current buffer to be passed as a string to writeString(). When this buffer is filled, the overridden function overflow is called. This causes the stream to flush and attempts to store the character that has overflowed. The buffer is optional, when the bufferSize is zero it constantly overflows. This is a less than optimal way of operating as it has to output each character individually, but it doesn’t take any extra memory. This wrapper has been written with simplicity in mind, it might not be the most robust, but it only depends on the STL and its pretty short. So enough of the details, lets make it do stuff! Here are some examples: Code:
The above class implements a log to visual studio's Output/Debug window. To use it, take a similar approach to redirecting to a file. Code:
Here is another example buffer: Code:
This one displays a message box when flushed. Its probably not such a good one if you've got lots of text, but it just shows what can be done. Finally, here is a useful buffer for chaining these buffers together. Code:
This stores characters till the buffer is filled or flushed then it puts the characters in the other buffers and forces them to sync. Used like this: Code:
It will print to both the debug window and to std::cout. Again this is written for simplicity not performance. Feel free to pick this code apart. I welcome constructive criticism. Cheers Dave |
|
|
|
|
|
#2 |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
You can also do this in applications that don't have a console:
Code:
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. |
|
|
|
|
|
#3 |
|
Member
Join Date: Mar 2006
Location: Missouri, USA
Posts: 74
|
This is cool. Very useful for a Quake-like console. I think I'll be using this in my next game project.
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|