473,721 Members | 2,002 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::printl n("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 1714
"Nick Forrington" <n.**********@w arwick.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::printl n("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::printl n(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.**********@w arwick.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::prin tln("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::printl n(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.**********@w arwick.ac.uk> wrote...
Victor Bazarov wrote:
"Nick Forrington" <n.**********@w arwick.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::pri ntln("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::printl n(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.**********@w arwick.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(fil ename);

Jul 22 '05 #5
"Nick Forrington" <n.**********@w arwick.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::printl n("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::ostringstr eam? 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.**********@w arwick.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::printl n("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_buf fer;
std::ostream the_console(&th e_console_buffe r);

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.**********@w arwick.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::printl n("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::ostringstr eam() << "" << output \
).str().c_str() ;
You can then write:
Console::printl n(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.**********@w arwick.ac.uk> wrote in message news:<cn******* ***@wisteria.cs v.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::printl n("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<typena me T>
inline Console& operator<<(cons t T& t);

//! @name IO stream manipulators.
//@{
inline Console& operator<<(
std::ostream& (*func)(std::os tream&)
);
inline Console& operator<<(
std::ios& (*func)(std::io s&)
);
inline Console& operator<<(
std::ios_base& (*func)(std::io s_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::printl n("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
2211
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 #define DEBUG_COLLISION
8
2693
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 partially? Something like: fileA.c fileB.c And I can compile fileA.c with debug info and compile fileB.c without debug info?
3
2147
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 the release version of the app but not when developing in the IDE. I would rather turn the e-mail into a MessageBox. What I would like to do is something like this: private void MyErrorHandler() if( <in debug mode> )
2
1620
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 thrown, a window would appear giving me a bit information, but noo... He is running windows 2k... What do i have to do in order to fix that?
1
1329
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 element in web.config. What ts the difference? Which one wins? Which one should I use and When? Thank You,
4
10511
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 doesn't seem to write to the screen in the way I would expect. The output is:
3
15516
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 line number of code that throws an exception. Specifically, I can have an error logging routine that, amongst other things parses the call stack and tells me the specific line of code that choked. It is also my understanding that if building in...
3
3239
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 flashcards.txt and then gets the categories from there. Anyhow, I understand how to do this, and there is nothing wrong with my syntax, because it compiles properly, but whenever it runs, it just starts outputting a bunch of random text, although I did...
5
1796
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 debug databases with them...
0
8736
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9227
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8019
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4497
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4761
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3202
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2588
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2137
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.