gcc - C Get function line number from within 2 function calls -


in process of developing small sdk, need create "error handler". make more effective want line number function called , don't know if it's possible in c (using gcc compiling).

when sdk function called, sdk function call error handler function if error occurred.

with i'd know if there way know line number user source code called function.

example :

user.c :

main{ callsdkfunction(); } 

sdk.a file :

void callsdkfunction( void ){ if ( error ){ callerrorhandler(); } } 

... ... ...

void callerrorhandler( void ){ // here want know in line in user.c callsdkfunction called} 

one approach pass __file__ , __line__ sdk function, , have sdk function pass them on error handler function. however, user won't want bothered that, need define macro in sdk's header file hides details. macro this

#define callsdkfunction(a,b) actualsdkfunction( __file__, __line__, a, b ) 

the user call sdk code this. note user doesn't need know __file__ , __line__ being passed sdk function. macro takes care of that.

int main( void ) {     int = 3;     int b = 4;     int result = callsdkfunction( a, b );       printf( "%d\n", result ); } 

and sdk implementation this

int actualsdkfunction( const char *filestr, int linenum, int a, int b ) {     if ( a+b > 5 )         callerrorhandler( filestr, linenum );     return( a+b ); }  void callerrorhandler( const char *filestr, int linenum ) {     printf( "bad values in file %s @ line %d\n", filestr, linenum ); } 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -