-
July 11th, 2012, 04:21 AM
#1
std::string debug function
Hello,
I'm trying to write a debug functions for my application so I can see all the values I like to.
My goal is it looks like this:
Code:
void Debug(std::message, ...)
So that I can call it like this:
Code:
Debug("Error: %i, %i", 34, 35);
I've no idea how I can work with %i, or if there are better solutions.
This is what I've so far (not much), just a basic idea:
Code:
void Debug( std::string message, ...)
{
va_list list;
va_start(list, message);
??? va_arg(list, ?? );
va_end(list);
// here message shoul contain everything and be ready for output ?
cout << message << endl;
}
Greetz
-
July 11th, 2012, 04:26 AM
#2
Re: std::string debug function
Why not just forward the call to sprintf / fprintf?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 12th, 2012, 03:56 PM
#3
Re: std::string debug function
Originally Posted by NLscotty
Hello,
I'm trying to write a debug functions for my application so I can see all the values I like to.
My goal is it looks like this:
Code:
void Debug(std::message, ...)
That code is ill-formed. For a function that has ellipses (...), the argument to the left of the ellipses must be a POD type. So forget about it.
Use sprintf(), std::ostringstream(), or boost::format from the boost library.
Regards,
Paul McKenzie
-
July 13th, 2012, 12:57 AM
#4
Re: std::string debug function
Is this what you are looking for?
Code:
#include <iostream>
#include <Windows.h>
inline void Trace( const char* format , ... )
{
#ifndef NDEBUG
char buffer[ 0x400 ];
va_list fmt_list;
va_start( fmt_list, format );
vsprintf_s( buffer, 0x400, format, fmt_list );
va_end( fmt_list );
strcat_s( buffer, 0x400 ,"\n" );
// ::OutputDebugString( buffer );
std::cout << buffer;
#endif // NDEBUG
}
int main()
{
Trace( "Error: %i , %i" , 34 , 35 );
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|