Tuesday, January 27, 2009

On some useful MACROS in C/C++ for debugging purposes

Like a stupid idiot I begin all my C++ files with the following construct:

#include
static const std::string thisFileName = "foo.cpp" ;


and then within each function I declare the following

const string thisFuctionName = "void bar()" ;


I do this so that I can print some useful information when I write an error summary to the error stream.

if ( !paraFile.is_open() )
{
cerr << "File: " << thisFileName << endl ;
cerr << "Function: " << thisFunctionName << endl ;
cerr << "ERROR: Could not open "<< paraFileName << endl ;
exit(EXIT_FAILURE);
}


This way I don't have to individually change the filename and function
name keeping the general structure of the construct more or less fixed.

Today I found this useful presentation on this page which informed me that some
useful MACROS are predefined that take care of such situations.

__FILE__

and

__LINE__



I feel like a first class idiot not knowing these things.

No comments: