I am trying to get the macro to print out the file name followed by the line number and then the print message, here is how I do this normally, and it works but it prints out the entire file path to the file:
#define TESTER1(...) printf(ANSI_COLOR_BOLD_GREEN "LOG::" __FILE__ ":" STR(__LINE__) "\t" ANSI_COLOR_RESET ANSI_COLOR_GREEN __VA_ARGS__); printf(ANSI_COLOR_RESET "\n")
this works, but the file path is way too long: Example output:
C:\Users\Work\Documents\CastEngine\CastDriver\src\main.cpp:18: Example
What I want the output to be is the following:
main.cpp:18: Example
So here is the macro for that:
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#define TESTER2(...) printf(ANSI_COLOR_BOLD_GREEN "LOG::" __FILENAME__ ":" STR(__LINE__) "\t" ANSI_COLOR_RESET ANSI_COLOR_GREEN __VA_ARGS__); printf(ANSI_COLOR_RESET "\n")
I know that __FILENAME__
works as intended because without defining TESTER2, I use this code:
printf("%s:%d", __FILENAME__, __LINE__);
and the output is exactly what I want: main.cpp:19
But when I add the TESTER2 define from above, it gives me the following error:
error: expression cannot be used as a function
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
^
C:\Users\Work\Documents\CastEngine\CastDriver\src\main.cpp:11:59: note: in expansion of macro '__FILENAME__'
#define TESTER2(...) printf(ANSI_COLOR_BOLD_GREEN "LOG::" __FILENAME__ ":" STR(__LINE__) "\t" ANSI_COLOR_RESET ANSI_COLOR_GREEN __VA_ARGS__); printf(ANSI_COLOR_RESET "\n")
^~~~~~~~~~~~
C:\Users\Work\Documents\CastEngine\CastDriver\src\main.cpp:20:5: note: in expansion of macro 'TESTER2'
TESTER2("Hello\n");
I really have no idea how to fix this; any help would be greatly appreciated.