#include <stdio.h>
#define LOG_LEVEL_NORMAL 1
#define LOG_LEVEL_ERROR 2
/* Define "Message<N>" symbols that will be replaced by the desired prefix
string.
*/
#define Message1 ""
#define Message2 "MODE_A ERROR "
// Define a macro to replace a log level with one of the "Message<N>" symbols.
#define MessageFor(log_level) Message##log_level
#define MODE_A_TRACE_1(log_level, s, p1) \
printf(MessageFor(log_level) s "\n", p1)
#define MODE_A_TRACE_2(log_level, s, p1, p2) \
printf(MessageFor(log_level) s "\n", p1, p2)
#define MODE_A_TRACE_3(log_level, s, p1, p2, p3) \
printf(MessageFor(log_level) s "\n", p1, p2, p3)
int main(void)
{
MODE_A_TRACE_1(LOG_LEVEL_NORMAL, "Normal, no prefix, value is %d", 17);
MODE_A_TRACE_1(LOG_LEVEL_ERROR, "Error, prefix, value is %d", 42);
}
输出量:
Normal, no prefix, value is 17
MODE_A ERROR Error, prefix, value is 42
1条答案
按热度按时间7jmck4yq1#
这是一个能满足你要求的组装设备。通常不建议使用这样的组装设备;你应该找到一个更好的方法来实现这个目标:
输出量: