How to create a function call logging in C

1 Answer

0 votes
#include <stdio.h> 
 
int funcAdd(int a, int b) { 
    return a + b; 
}
 
void logging(const char* file, const char* function, const int line, const char* args) {
    printf("file:%s line: %i function: %s args: %s\n", file, line, function, args);
}
 
#define funcAdd(...) \
    (logging(__FILE__, __FUNCTION__, __LINE__, "" #__VA_ARGS__), funcAdd(__VA_ARGS__))
 
 
void funcA(void) {
    funcAdd(3, 7);
}
 
void funcB(void) {
    funcAdd(33, 876);
}
 
 
int main(void){
    funcAdd(2, 9);
 
    funcAdd(1, 88);
 
    int result = funcAdd(41, 90);
     
    printf("result = %d\n", result);
 
    funcA();
     
    funcB();
} 
 
     
     
/*
run:
     
file:main.c line: 25 function: main args: 2, 9
file:main.c line: 27 function: main args: 1, 88
file:main.c line: 29 function: main args: 41, 90
result = 131
file:main.c line: 16 function: funcA args: 3, 7
file:main.c line: 20 function: funcB args: 33, 876
    
*/

 



answered Apr 5, 2024 by avibootz
edited Apr 5, 2024 by avibootz

Related questions

1 answer 161 views
1 answer 101 views
1 answer 144 views
1 answer 121 views
1 answer 117 views
2 answers 287 views
287 views asked Aug 30, 2017 by avibootz
2 answers 248 views
...