fork download
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3.  
  4. #define config_ENABLE_DEBUG_MAIN 1
  5.  
  6. static char txBuffer[100];
  7.  
  8. void PrintDebug(char * fileName, int lineNumber, char * msg, ...) {
  9.  
  10. // NOTE: Replace all printf() calls in here with whatever is appropriate for
  11. // the microcontroller you are using, e.g. Uart_PutString(txBuffer);
  12.  
  13. // Create "header" of debug message and send to UART peripheral
  14. snprintf(txBuffer, sizeof(txBuffer), "%s, %i: ", fileName, lineNumber);
  15. printf(txBuffer);
  16.  
  17. // Format the "guts" of the debug message into a string
  18. va_list args;
  19. va_start(args, msg);
  20. vsnprintf(txBuffer, sizeof(txBuffer), msg, args);
  21.  
  22. // Send formatted "guts" to the UART peripheral
  23. printf(txBuffer);
  24. printf("\r\n");
  25.  
  26. }
  27.  
  28. #if(config_ENABLE_DEBUG_MAIN == 1)
  29. #define DEBUG(...) PrintDebug(__FILE__, __LINE__, __VA_ARGS__)
  30. #else
  31. #define DEBUG(...)
  32. #endif
  33.  
  34.  
  35. int main(void) {
  36. DEBUG("Testing debug print capabilities...");
  37.  
  38. DEBUG("This should print both the filename and line number!");
  39.  
  40. int x = 5;
  41. DEBUG("We can also use this like printf()! x = %i", x);
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
prog.c, 33: Testing debug print capabilities...
prog.c, 35: This should print both the filename and line number!
prog.c, 38: We can also use this like printf()! x = 5