Im trying to make my defines can be switched during runtime, so I cant enable/disable printing some info.
I have next files:
- main.c
- config.c
- config.h (included in both c files)
in config.c :
//define DEBUG // <--------------- COMMENT THIS TO SWITCH PRINT/NOT PRINT
#define PRINTF(fmt, ...) printf("TAG: " fmt "\n", ## __VA_ARGS__)
#ifdef DEBUG
#define DPRINTF PRINTF
#else
#define DPRINTF(...) do {} while (0)
#endif
int foo()
{
...
if(error){
PRINTF("error happens, code %d", code);
}
else{
DPRINF("all good, all parameters: [%d][%d]", par1, par2);
}
}
(short explanation: if DEBUG
defined then macro DPRINTF will print some info, otherwise do nothing)
What I want is be able to dynamically switch it from main.c somehow, instead of comment / uncomment and recompile program.
So my idea was to set in config.h
extern uint8_t dbg_status;
#define DGB_ON (1)
#define DGB_OFF (0)
#define ENABLE_DBG (dbg_status? (DGB_ON) : (DGB_OFF))
And in main.c do something like that:
uint8_t dbg_status;
int main(int argc, char** argv)
{
// enable debug if run with next argument:
// program.exe -DEBUG
if (!strcmp(argv[1], "-DEBUG"))
{
// enable debug prints
dbg_status= 1;
}
else
{
// disable debug prints
dbg_status= 0;
}
...
}
But Im stuck with it now, I dont know what to do next...
Add some additional defines in config.c?
#if (ENABLE_DEBUG)
#define DEBUG
#endif
I feel that Im on right way, but dont see where to move next.
UPD: Original idea from here: Changing a macro at runtime in C
But Im stuck with realization...