How to prefix *all* printf() calls with a timestamp string? (for Semihost Console mode )

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

How to prefix *all* printf() calls with a timestamp string? (for Semihost Console mode )

1,245 次查看
Gretsch
Contributor II

I prefer all printf() calls to include a prefix timestamp, so 

  printf("something happened");

gives output

 00:01:30.555 something happened

in other words, the printf happened 1minute,30 seconds 555 milliseconds into the current debug session.

Typically I override printf() itself, and then redirect to the proper call.

I've gotten this to work when using a serial port UART (DEBUGCONSOLE_DISABLE), but I need it to print timestamps in Semihost Console mode DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN

I can't seem to find out how to create a hook into your SDK to override printf().

Hopefully, someone has done this before for Semihost Console mode.

 

Here's a portion of my changes to dbg_console_printf.h.  Also, see my attached 

 

/*! @brief Definition to select redirect toolchain printf, scanf to uart or not.

 *

 *  if SDK_DEBUGCONSOLE defined to 0,it represents select toolchain printf, scanf.

 *  if SDK_DEBUGCONSOLE defined to 1,it represents select SDK version printf, scanf.

 *  if SDK_DEBUGCONSOLE defined to 2,it represents disable debugconsole function.

 */

#if SDK_DEBUGCONSOLE == DEBUGCONSOLE_DISABLE /* Disable debug console */

static inline int DbgConsole_Disabled(void)

{

    return -1;

}

#define PRINTF(...)  DbgConsole_Disabled()

#define SCANF(...)   DbgConsole_Disabled()

#define PUTCHAR(...) DbgConsole_Disabled()

#define GETCHAR()    DbgConsole_Disabled()

#elif SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK /* Select printf, scanf, putchar, getchar of SDK version. */

extern void DbgPrintTimestamp_Baremetal();

extern int gCrashLogTimestampLength;

#define PRINTF(fmt, ...) do { \

    gCrashLogTimestampLength = 0; \

    DbgPrintTimestamp_Baremetal(); \

    DbgConsole_Printf(fmt, ##__VA_ARGS__); \

} while(0)

 

#define SCANF   DbgConsole_Scanf

#define PUTCHAR DbgConsole_Putchar

#define GETCHAR DbgConsole_Getchar

#elif SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN /* Select printf, scanf, putchar, getchar of toolchain. \ \

                                                              */

#define PRINTF  printf

#define SCANF   scanf

#define PUTCHAR putchar

#define GETCHAR getchar

#endif /* SDK_DEBUGCONSOLE */

 

 

0 项奖励
回复
1 回复

1,179 次查看
ErichStyger
Specialist I

Hi @Gretsch ,

I have been used the GNU linker wrap option for this for a long time. Your question reminded me that I probably should write about this, so I just finished it:

https://mcuoneclipse.com/2023/07/22/gnu-linker-wizardry-wrapping-printf-with-timestamps/

printf with timestamps.png

The cool thing with this is that you don't have to change your source code, just add a wrapper and a linker option. I'm using the is approach successfully for all kind of standard functions.

I hope this helps,

Erich

0 项奖励
回复