C debbugging printf macro
da Nss il agosto 3, 2010
Ci farebbero comodo dei messaggi di debug durante la stesura del nostro software, ma vorremmo fare in modo che alla fine della codifica non sia necessario cancellare tutte le singole righe manualmente…
Niente di più semplice, basterà inserire nel file .h la seguente macro
#ifdef DEBUG #define YELLOW ”\033[1;33m” #define RED "\033[31m" #define GREEN "\033[32m" #endif #ifdef DEBUG /**PRINTE... PRINTError*/ #define PRINTE(format, args...) printf(RED format NORMAL "\n", ##args); /**PRINTW... PRINTWarning*/ #define PRINTW(format, args...) printf(YELLOW format NORMAL "\n", ##args); /**PRINTA... PRINTAlarm*/ #define PRINTA(format, args...) printf(GREEN format NORMAL "\n", ##args); #else #define PRINTE(format, args...) #define PRINTW(format, args...) #define PRINTA(format, args...) #endif
e nel codice, volendo lasciare un messaggio di debug, al posto della solita printf, basterà usare una tra le seguenti macro PRINTE/PRINTW/PRINTA.
Per abilitare il debug basterà compilare con l’opzione -DDEBUG e le macro faranno il resto.
Se il codice viene compilato senza tale opzione, le chiamate a PRINTE/PRINTW/PRINTA verranno completamente eliminate dal software.
Lascia un commento