[英]I don't understand C macros
How does the LOG_FORMAT
expand in esp_log_write
?如何在
LOG_FORMAT
在扩大esp_log_write
? What does the format LOG_RESET_COLOR
do in the LOG_FORMAT
? format LOG_RESET_COLOR
在LOG_FORMAT
什么作用?
#define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%d) %s: " format LOG_RESET_COLOR "\n"
esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
void esp_log_write(esp_log_level_t level,
const char *tag,
const char *format, ...)
{
va_list arg;
va_start(arg, format);
vprintf(format, arg);
va_end(arg);
}
In macro expansion ##
means concatenation of any text with a macro argument.在宏扩展中
##
表示将任何文本与宏参数连接起来。 #
means that the macro argument which follows it will be placed in double quotes. #
表示它后面的宏参数将放在双引号中。
Now, in 'C' neighboring string literals, separated just by spaces really means a concatenation of 2 strings, ie "hello " "world"
really means "hello world"
现在,在 'C' 相邻字符串文字中,仅用空格分隔实际上意味着 2 个字符串的串联,即
"hello " "world"
真正意味着"hello world"
So, macro LOG_FORMAT(V, format)
gets expanded to因此,宏
LOG_FORMAT(V, format)
被扩展为
LOG_COLOR_V "V" " (%d) %s: " format LOG_RESET_COLOR "\n"
now, the above by itself is not a legal 'C' syntax.现在,以上本身不是合法的“C”语法。 So, most likely there are also definitions for
LOG_COLOR_V
, format
, and LOG_RESET_COLOR
.因此,很可能还有
LOG_COLOR_V
、 format
和LOG_RESET_COLOR
。 They are supposed to be defined as quoted strings themselves.它们应该被定义为带引号的字符串本身。 In this case all the above will be interpreted as a single quoted string.
在这种情况下,以上所有内容都将被解释为单引号字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.