Follow+Star PublicAccount to not miss exciting content
Source | Embedded Miscellaneous
In embedded development, logging and debugging are undoubtedly the most commonly used and practical debugging methods. However, many engineers may not fully leverage the power of logging!
Today, I will share several logging techniques to help you progress from a “printf novice” to a “logging master”!
1. Technique One: Module Tags – Quickly Locate the Source of Problems
1.1 Modular Logging System

1.2 Example

Logging Output Effect:
[WIFI]Starting network scan...
[WIFI]Found 3 networks
[WIFI]Network[0]: MyHome_WiFi, RSSI: -45
[WIFI]Network[1]: Office_5G, RSSI: -67
[WIFI]Network[2]: Guest_Network, RSSI: -78
2. Technique Two: Precise Positioning – When to Log?
2.1 Error Handling

2.2 Critical Operations

2.3 System Startup and Shutdown

2.4 Performance Monitoring

2.5 Communication Logs

2.6 User Behavior

2.7 Branch Execution: Program Flow Tracking

2.8 Crash Information: Fault Scene Protection

3. Technique Three: Logging Switch – Flexibly Control Log Output
3.1 Module-Level Logging Switch

4. Technique Four: Timestamps – Precise Temporal Analysis
4.1 High-Precision Timestamps

4.2 Performance Analysis Logs

5. Technique Five: Log Levels – Hierarchical Information Management
5.1 Standard Log Level Definitions

5.2 Practical Application Examples

6. Technique Six: Unified Format – Structured Log Information
6.1 Standard Log Format Design

Output Effect:
[2024-01-15 14:30:25.123456][INFO][main.c:45][wifi_init] WiFi module initialized
[2024-01-15 14:30:25.234567][DEBUG][sensor.c:128][read_temperature] Temperature: 25.6°C
[2024-01-15 14:30:25.345678][WARN][storage.c:67][save_data] Storage almost full: 95%
7. Technique Seven: Dynamic Filtering – Real-Time Adjustment of Log Output
7.1 Dynamic Filtering Based on EasyLogger

7.2 Command Line Log Control

8. Technique Eight: Smart Switching Between Release/Debug
8.1 Log Control at Compile Time

In Conclusion
A good logging system not only helps you quickly locate problems but also allows you to deeply understand the system’s operational status and even prevent potential issues.
Key Points:
- Log at critical locations
- Use module tags for categorization
- Set log levels reasonably
- Include precise timestamps
- Provide dynamic control capabilities
Common Pitfalls to Avoid
- Excessive Logging: More is not always better
- Insufficient Information: Lacking critical context
- Disorganized Format: Difficult to automate processing
- Performance Neglect: Affects system performance
- Security Oversight: Leaks sensitive information
———— END ————

How difficult is it to cultivate an excellent embedded engineer?

A/B Partitioning in MCU OTA Upgrades: Classic Solutions and Implementation Logic

Several Popular Embedded Open Source Projects