cmacros

Calling a C macro from another macro function


I have a macro defined as below. This is used to print debugging information to the console. Now, I have to send this debugs over to a socket without disturbing the existing code. I am thinking of changing this so that the Debug_Print macro would check if it is in socket mode and call a different macro to send it over the socket. I tried dprintf and it works for my purpose. How can I change this Debug_Print macro so that it calls printf(...) vs dprintf(fd, ...) by checking is_in_socket_mode()?

bool is_in_socket_mode() function would tell if it is in socket mode.

Existing macro:

#define Debug_Print(label, ...) ( Is_Enabled(label) ? (void)printf(__VA_ARGS__) : (void)0 )

I tried to use is_in_socket_mode() in the existing macro, but I am not able to figure out how to do this. I believe I have to introduce another macro and call that within the Debug_Print macro.


Solution

  • A bit about terminology: macros are instantiated at compile time. At runtime they do not exist anymore, so "calling a macro" in the sense of calling a function is not possible.

    Since is_in_socket_mode() is a run-time function, this should be a question of checking its value in the macro:

    #define Debug_Print(label, ...) ( Is_Enabled(label) ? ((is_in_socket_mode()) ? (void)dprintf(fd, __VA_ARGS__) : (void)printf(__VA_ARGS__)) : (void)0 )