Home / Learn Chip Programming / Debugging with printf, Logs and LED Codes

Debugging with printf, Logs and LED Codes

The simplest, always-available debugging tools and how to use them well.

In shortThe simplest, always-available debugging tools and how to use them well.

Theory in plain words

Log levelsLOG_E: errorsLOG_W: warningsLOG_I: eventsLOG_D: details (debug builds)
Log levels

What it is

Log messages with levels (ERROR, WARN, INFO, DEBUG), timestamps and module tags. Use LED blink codes when no console exists. Compile logs out in release builds.

Why we use it

Most bugs are found by watching the program's behaviour. Good logging also helps with field support.

How it works

Define LOG macros that print with level and time, buffer logs and send them from a low priority task, use a ring buffer for crash logs that survive reset.

Where it is used

  • Daily debugging
  • Field diagnostics
  • RTOS timing problems

Log filter playground

Filter logs by level and moduleTry it Yourself

Timestamps show ordering and delays. Levels keep the output readable.

Compile-time logging macros
#ifdef DEBUG
#define LOG_D(tag, fmt, ...) printf("[%lu] D %s: " fmt "\r\n", millis(), tag, ##__VA_ARGS__)
#else
#define LOG_D(tag, fmt, ...) ((void)0)           // compiled out: zero cost
#endif
#define LOG_E(tag, fmt, ...) printf("[%lu] E %s: " fmt "\r\n", millis(), tag, ##__VA_ARGS__)

// LED error codes when no console: blink N times = error N
void blink_error(uint8_t n) { for (uint8_t i = 0; i < n; i++) { led(1); delay_ms(150); led(0); delay_ms(150); } delay_ms(1000); }

Quick quiz

1. Where not to printf?

2. Why timestamps in logs?

Common questions

Where not to printf?

Inside ISRs. It blocks and is slow.

Why timestamps in logs?

Show ordering and delays. Needed to understand timing bugs.