cancel
Showing results for 
Search instead for 
Did you mean: 

can't get standard c-function/macro (va_list, va_start, va_end) to work properly. compiler bug?

matt-crc
Associate II

Hello,

The following code compiles, but doesn't give the me results I'm expecting:

 

 

void syslog2(uint8_t pri, const char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	printf(fmt, args);
	va_end(args);
}
main() {
  printf("%s\r\n", _tx_version_id);
  syslog2(SYSLOG_INFO, "Azure RTOS - %s\r\n", "test" /*_tx_version_id*/);
}

 

 

 this is the output:

mattcrc_0-1740191974871.png

what am I missing?

thanks

Matthew

 

1 ACCEPTED SOLUTION

Accepted Solutions
AA1
Senior III

This is a C question and not a ST microcontrollers question. With va_list arguments, you should use vprintf. See https://cplusplus.com/reference/cstdio/vprintf/

 

View solution in original post

2 REPLIES 2
AA1
Senior III

This is a C question and not a ST microcontrollers question. With va_list arguments, you should use vprintf. See https://cplusplus.com/reference/cstdio/vprintf/

 

Hi @AA1 

Where were you 2 hours ago??  LOL

I ended up writing my own macro to get around this issue:

 

#define _LOG_FORMAT_STD(letter, format) "%s" format "\r\n", SysLog_prefix( letter )

#define _LOG_FORMAT(letter, format)      \
    "%s [%s:%u] %s(): " format "\r\n", SysLog_prefix( letter ), GetFileName( __FILE__), __LINE__, __FUNCTION__


#define syslog(level, format, ...)     \
	if (level == SYSLOG_FATAL) {			 \
    printf(_LOG_FORMAT(level, format), ##__VA_ARGS__); \
	} else if (level <= syslog_level)            \
    printf(_LOG_FORMAT_STD(level, format), ##__VA_ARGS__)

 

But I did test your suggestion (vprintf), and it works as well.

thanks

Matthew