Movatterモバイル変換


[0]ホーム

URL:


EmbeddedRelated.com
The 2026 Embedded Online Conference
   Code Snippets    Debug print trace macro

Debug print trace macro

March 22, 2013 Coded inC

Far be it for me to encourage over-use of preprocessor macros, but the ability of the preprocessor to embed file, function and line information makes it useful for debug trace.  The use of the standard built-in NDEBUG macro allows the code to be automatically excluded on release builds.

Note that the lack of a comma between `"%s::%s(%d)"` and `format` is deliberate. It prints a formatted string with source location prepended. 

For many systems with critical timing it is often useful to also include a timestamp using a suitable system-wide clock source, but since that is system dependent I have omitted that option from the example.  Note that where timing is critical you should ensure that your stdout implementation is buffered and non-blocking.

Support for variadic macros may not be universal hacing been standardised in C99.

#if defined NDEBUG    #define TRACE( format, ... )#else    #define TRACE( format, ... )   printf( "%s::%s(%d) " format, __FILE__, __FUNCTION__,  __LINE__, __VA_ARGS__ )#endif// Example usagevoid example(){    unsigned var = 0xdeadbeef ;    TRACE( "var=0x%x", var ) ;}// Resultant output example://    example.c::example(5) var=0xdeadbeef//

The 2026 Embedded Online Conference

Sign in

Forgot username or password?  | Create account

Blogs - Hall of Fame

So You Want To Be An Embedded Systems Developer

So You Want To Be AnEmbedded Systems Developer
Steve Branam

Introduction to Microcontrollers

Introduction toMicrocontrollers
Mike Silva

Important Programming Concepts (Even on Embedded Systems)

ImportantProgramming Concepts (Even on Embedded Systems)
Jason Sachs

How FPGAs Work and Why You'll Buy One

HowFPGAs Work and Why You'll Buy One
Yossi Krenin

MSP430 Launchpad Tutorial

MSP430 Launchpad Tutorial
Enrico Garante

Arduino Robotics

Arduino Robotics
Lonnie Honeycutt

Free PDF Downloads

Memory allocation in C
Interrupt handling in an ARM processor
CPU Memory - What Every Programmer Should Know About Memory

Quick Links

About EmbeddedRelated.com

The Related Sites


[8]ページ先頭

©2009-2026 Movatter.jp