Archived

This topic is now archived and is closed to further replies.

Skibum

inline functions using __LINE__ and __FILE__

Recommended Posts

I''m wanting a way to throw an error using an inline function that dosn''t expand the macros __LINE__ and __FILE__ where the inline function is, instead i would like them to be expanded where the inline function is used in the code, is there a way to do this? The closest I have come so far is a mix between, doing this: inline foo(char *msg, int line, char *file) { blah... } and using it like this: #define INFO __LINE__, __FILE__ foo("Your error here.", INFO); what I would like to be able to do is this: inline foo(char *msg, int line = __LINE__, char *file=__FILE__) { blah... } is there a way to do this?

Share this post


Link to post
Share on other sites
You can macro it:

If it''s simple:

  
#define Log(txt){printf("%s:%u - %s\r\n", __FILE__, __LINE__, txt);}

int _tmain(int argc, _TCHAR* argv[])
{
Log("Hello");
Log("Test");
return 0;
}


If it''s complex:

  
#define Log(txt){_Log(txt, __FILE__, __LINE__);}

void _Log(char *txt, char *file, int line)
{
printf("%s:%u - %s\r\n", file, line, txt);
}

int _tmain(int argc, _TCHAR* argv[])
{
Log("Hello");
Log("Test");
return 0;
}


So either write it all out in a macro or just use a macro to wrap the default parameters for the function. You can''t specify __FILE__ or __LINE__ as default parameters, so the macro wrapper is the best way in my opinion.

Share this post


Link to post
Share on other sites
In addition use pre-processing directives to remove the code from release builds :


      
#ifdef _DEBUG

#define Log(txt){_Log(txt, __FILE__, __LINE__);}

void _Log(char *txt, char *file, int line)
{
printf("%s:%u - %s\r\n", file, line, txt);
}
#else

#define Log(txt) ;

#endif

int _tmain(int argc, _TCHAR* argv[])
{
Log("Hello");
Log("Test");
return 0;
}


[edited by - RobTheBloke on July 17, 2002 3:45:18 PM]

Share this post


Link to post
Share on other sites