CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2009
    Posts
    219

    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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: std::string debug function

    Quote Originally Posted by NLscotty View Post
    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

  4. #4
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    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
  •  





Click Here to Expand Forum to Full Width

Featured