473,511 Members | 10,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

outputting debug info

Hi, I'm making a program and have a static Console class that I'm using
to output things, these get sent to the console and also to the graphics
on screen (I'm using SDL). One thing I'm having a bit of trouble with is
passing strings and numbers in the same arguement

e.g.

Console::println("X=" + x);

kind of thing?

anyone suggest a good solution? the only way I can seem to do it now is
by creating a string and doing something like

string s = "X=";
s += x;

This obviously gets more messy, but I'll have to stick with it if noone
has a better idea.

Thanks
Nick
Jul 22 '05 #1
9 1698
"Nick Forrington" <n.**********@warwick.ac.uk> wrote...
Hi, I'm making a program and have a static Console class that I'm using to
output things, these get sent to the console and also to the graphics on
screen (I'm using SDL). One thing I'm having a bit of trouble with is
passing strings and numbers in the same arguement

e.g.

Console::println("X=" + x);

kind of thing?
"X=" has type 'const char[3]'. Adding anything to it causes conversion
of that array to a pointer to const char. Pointers accept only values
of type 'ptrdiff_t' to be added to them. How is "x" declared? Another
pointer? Then you cannot do that. There is a simple work-around:

Console::println(string("X=") + x);

anyone suggest a good solution? the only way I can seem to do it now is by
creating a string and doing something like

string s = "X=";
s += x;

This obviously gets more messy, but I'll have to stick with it if noone
has a better idea.


You could overload 'println' to accept more than one argument...

V
Jul 22 '05 #2
Victor Bazarov wrote:
"Nick Forrington" <n.**********@warwick.ac.uk> wrote...
Hi, I'm making a program and have a static Console class that I'm using to
output things, these get sent to the console and also to the graphics on
screen (I'm using SDL). One thing I'm having a bit of trouble with is
passing strings and numbers in the same arguement

e.g.

Console::println("X=" + x);

kind of thing?

"X=" has type 'const char[3]'. Adding anything to it causes conversion
of that array to a pointer to const char. Pointers accept only values
of type 'ptrdiff_t' to be added to them. How is "x" declared? Another
pointer? Then you cannot do that. There is a simple work-around:

Console::println(string("X=") + x);

anyone suggest a good solution? the only way I can seem to do it now is by
creating a string and doing something like

string s = "X=";
s += x;

This obviously gets more messy, but I'll have to stick with it if noone
has a better idea.

You could overload 'println' to accept more than one argument...

V

Thanks for the help. So is

Console::error(string("File ") + string(filename) + " does not exist");

the tidiest way to do that?

Also I just tried something similar when trying to output a double, and
I'm either doing something stupid (which is entirely possible) or you
can't convert a double to a string?

Any ideas?
Nick
Jul 22 '05 #3
"Nick Forrington" <n.**********@warwick.ac.uk> wrote...
Victor Bazarov wrote:
"Nick Forrington" <n.**********@warwick.ac.uk> wrote...
Hi, I'm making a program and have a static Console class that I'm using
to output things, these get sent to the console and also to the graphics
on screen (I'm using SDL). One thing I'm having a bit of trouble with is
passing strings and numbers in the same arguement

e.g.

Console::println("X=" + x);

kind of thing?

"X=" has type 'const char[3]'. Adding anything to it causes conversion
of that array to a pointer to const char. Pointers accept only values
of type 'ptrdiff_t' to be added to them. How is "x" declared? Another
pointer? Then you cannot do that. There is a simple work-around:

Console::println(string("X=") + x);

anyone suggest a good solution? the only way I can seem to do it now is
by creating a string and doing something like

string s = "X=";
s += x;

This obviously gets more messy, but I'll have to stick with it if noone
has a better idea.

You could overload 'println' to accept more than one argument...

V

Thanks for the help. So is

Console::error(string("File ") + string(filename) + " does not exist");

the tidiest way to do that?


Actually, you don't need the second 'string'. Once the first literal is
converted into a temporary string, you can keep "adding" literals to it.

As to that way being the tidiest, I don't know.

Also I just tried something similar when trying to output a double, and
I'm either doing something stupid (which is entirely possible) or you
can't convert a double to a string?


*I* can. <g>

Of course, you can't expect to do it using the straight + operator.
You need a function to convert the 'double' to a string first. See
http://groups.google.com/ for the 'toString' template. Then use it
like this:

double d = 3.1415926;
Console::error(string("Pi is ") + toString(d));

V
Jul 22 '05 #4
On Thu, 18 Nov 2004 03:09:36 +0000 in comp.lang.c++, Nick Forrington
<n.**********@warwick.ac.uk> wrote,
Thanks for the help. So is

Console::error(string("File ") + string(filename) + " does not exist");

the tidiest way to do that?


std::perror(filename);

Jul 22 '05 #5
"Nick Forrington" <n.**********@warwick.ac.uk> wrote in message
news:cn**********@wisteria.csv.warwick.ac.uk...
Hi, I'm making a program and have a static Console class that I'm using to
output things, these get sent to the console and also to the graphics on
screen (I'm using SDL). One thing I'm having a bit of trouble with is
passing strings and numbers in the same arguement

e.g.

Console::println("X=" + x);

kind of thing?

anyone suggest a good solution? the only way I can seem to do it now is by
creating a string and doing something like

string s = "X=";
s += x;

This obviously gets more messy, but I'll have to stick with it if noone
has a better idea.

Thanks
Nick


How about you make Console extend std::ostringstream? This way a Console
object behaves much like std::cout. You can write code like this:

console << "X = " << x << std::endl;

This is what I do in my own graphics application. Its a life saver when
debugging code.
Jul 22 '05 #6

"Nick Forrington" <n.**********@warwick.ac.uk> wrote in message
news:cn**********@wisteria.csv.warwick.ac.uk...
Hi, I'm making a program and have a static Console class that I'm using to
output things, these get sent to the console and also to the graphics on
screen (I'm using SDL). One thing I'm having a bit of trouble with is
passing strings and numbers in the same arguement

e.g.

Console::println("X=" + x);

kind of thing?

anyone suggest a good solution? the only way I can seem to do it now is by
creating a string and doing something like

string s = "X=";
s += x;

This obviously gets more messy, but I'll have to stick with it if noone
has a better idea.


You need to make a stream buffer class. Then you can write

ConsoleBuffer the_console_buffer;
std::ostream the_console(&the_console_buffer);

the_console << "X=" << x;

Creating stream classes is a very useful thing to know how to do,
unfortunately it would take to long to explain this is a newsgroup post. But
here's a hint for your own research, you do not need to inherit from
std::ostream, you need to inherit from std::streambuf. In the code above
ConsoleBuffer inherits from std::streambuf.

Try googling for information on this.

john
Jul 22 '05 #7
"Nick Forrington" <n.**********@warwick.ac.uk> wrote in message
news:cn**********@wisteria.csv.warwick.ac.uk...
Hi, I'm making a program and have a static Console class that I'm using to
output things, these get sent to the console and also to the graphics on
screen (I'm using SDL). One thing I'm having a bit of trouble with is
passing strings and numbers in the same arguement

e.g.

Console::println("X=" + x);

kind of thing?

anyone suggest a good solution? the only way I can seem to do it now is by
creating a string and doing something like

string s = "X=";
s += x;

This obviously gets more messy, but I'll have to stick with it if noone
has a better idea.


First of all, the C++ standard library already has built-in output
streams for writing debugging information: cerr, clog, or even cout.
These outputs can be redirected to a file or a console window using
OS facilities.
In your program, you can also replace the 'stream buffer' behind any
of these logging outputs, and transmit the data to the destination of
your choice. ( using the rdbuf() method)
See this example for redirection to a file:
http://groups.google.com/groups?selm...nnrp1.deja.com
For other output destinations, you need to implement a custom subclass
of std::stream_buf. Dietmar Kühl, an expert on this topic, has a couple
examples available on his website: (see GUI window)
http://www.informatik.uni-konstanz.de/~kuehl/
Of course you don't *have* to reuse cout, you can also create another
ostream instance for your custom streambuf.

If you only want to generate formatted strings and use 'println' as above,
I would recomment taking a look at the excellent, free, peer_reviewed
boost::format library: http://www.boost.org/libs/format/index.html
This is a safe and flexible solution.
It might well be the best/easiest option for what you are wanting to do.

Take also a look at boost's lexical_cast, a 2-way superset of Victor's
"toString": http://www.boost.org/libs/conversion/lexical_cast.htm
I will also mention two tricks that can be used, but which I
cannot recommend for any serious development:

You could use a macro hack to use stringstream on the fly.
Something like:
#include <sstream>
#define TO_STRING( output ) \
static_cast<std::ostringstream&>( \
std::ostringstream() << "" << output \
).str().c_str();
You can then write:
Console::println(TO_STRING( "pi is" << 3.1416 << " !");
Another alternative is to emulate the sprintf interface using a function
that in turn calls vsprintf to convert the string, then outputs it.
This however tends to be bug-prone (you need to use non-standard library
functions to guarantee no buffer overflow, and this has all the other
caveats of std::printf).
I hope this helps, let me know of the approach you'll choose ;)
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #8
Nick Forrington <n.**********@warwick.ac.uk> wrote in message news:<cn**********@wisteria.csv.warwick.ac.uk>...
Hi, I'm making a program and have a static Console class that I'm using
to output things, these get sent to the console and also to the graphics
on screen (I'm using SDL). One thing I'm having a bit of trouble with is
passing strings and numbers in the same arguement

e.g.

Console::println("X=" + x);

kind of thing?

anyone suggest a good solution? the only way I can seem to do it now is
by creating a string and doing something like

string s = "X=";
s += x;

This obviously gets more messy, but I'll have to stick with it if noone
has a better idea.

Thanks
Nick


Why do you not provide a few output operators for Console?
Snipets (adjusted) from my logging interface:

class Console {
// ...
public:
//! Output operator for arbitrary types and objects.
template<typename T>
inline Console& operator<<(const T& t);

//! @name IO stream manipulators.
//@{
inline Console& operator<<(
std::ostream& (*func)(std::ostream&)
);
inline Console& operator<<(
std::ios& (*func)(std::ios&)
);
inline Console& operator<<(
std::ios_base& (*func)(std::ios_base&)
);
//@}
// ...
};

2 or 3 years ago I got the necessary output operators from c.l.c++.m,
I think from James Kanze (please don't blame me if I remember the
wrong name).

My logging interface also provide functions to initiate a message
(it's actually the only way how you can get a Console&):

class Console {
// ...
public:
//! Start a fatal error message.
inline Console& fatal();
//! Start an error message.
inline Console& error();
//! Start a warning.
inline Console& warn();
//! Start a normal message.
inline Console& info();
/*!
@brief Start a debugging message.

@param level Debugging level (4-7):
1 corresponds to log level 4.
*/
inline Console& debug(int level);
//! Continue a message: useful in loops.
inline Console& cont();
// ...
};

Then you just output to Console as to a std::ostream:

void foo(Console& console, double d)
{
console.debug(4) << "foo start with: " << d << std::endl;
}

I guess you get an idea.
The classes I'm using provide more functionality, too big to post
unless you ask for it.

Stephan Brönnimann
br****@osb-systems.com
Open source rating and billing engine for communication networks.
Jul 22 '05 #9
Nick Forrington wrote:
Hi, I'm making a program and have a static Console class that I'm using
to output things, these get sent to the console and also to the graphics
on screen (I'm using SDL). One thing I'm having a bit of trouble with is
passing strings and numbers in the same arguement

e.g.

Console::println("X=" + x);

kind of thing?

anyone suggest a good solution? the only way I can seem to do it now is
by creating a string and doing something like

string s = "X=";
s += x;

This obviously gets more messy, but I'll have to stick with it if noone
has a better idea.

Thanks
Nick

Thanks for the help all of you.

In the end I opted to use the toString template method, as I wanted to
keep the Console methods static, also I don't really have that much time
to start learning about more complex solutions right now (it's for an
academic project). I just wanted a quick and dirty way to output some data.

Once again, thanks for the help. Much appreciated.
Nick
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2204
by: Michael | last post by:
OK Guys, lets say that I've got some code that I want from time when I'm debuging but want to be able to turn off quickly. At the moment I'm doing: #define DEBUG_TEXTURE #define DEBUG_BSP...
8
2676
by: Davy | last post by:
Hi all, I use VC and gcc/gdb to compile and debug C/C++ files. But I found some of the debug version of the compiled files are too large to be run in a small RAM. Can I compile C/C++ Debug...
3
2137
by: Greg Smith | last post by:
Hi, is there a way to tell in code if you are in Debug|Release mode? I have an error handling routine that sends me a e-mail if an error is caught. This is great when the users are working with...
2
1611
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an application that doesn't quite work on my co-workers computer. I want to install the app with debug information, in my crazy mind i thought that when an exception was...
1
1325
by: Brian | last post by:
Can sombody please outline the diference between setting complication mode from the toolbar in VS.Net (i.e. debug/release) and setting the debug attribute (i.e. true/false) on the compilation...
4
10489
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
3
15501
by: Bob Johnson | last post by:
It is my understanding - and please correct me if I'm wrong - that when building a project in debug mode, I can deploy the .pdb file along with the ..exe and thereby have access to the specific...
3
3223
by: mohaakilla51 | last post by:
Alright guys, I am working on a flashcard script... Previously I had it so that it onlty had predefined categories. People were complaining, so now I am trying to make it to where it reads...
5
1787
by: Andy B | last post by:
How do you tell vs2005 to keep debug info out of your release builds? When I build a release and a debug version, there seems to be no difference in them at all...The release builds still have the...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.