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
Re: std::string debug function
Why not just forward the call to sprintf / fprintf?
Re: std::string debug function
Quote:
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
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 );
}