最近学sylar,看到的宏函数的运用手法。觉得很妙,分享一下。
static std::map<std::string, std::function<FormatItem::ptr(const std::string& str)> > s_format_items = {
#define XX(str, C) \
(#str, [](const std::string& fmt) { return FormatItem::ptr(new C(fmt));})
XX(m, MessageFormatItem),
XX(p, LevelFormatItem),
XX(r, ElapseFormatItem),
XX(c, NameFormatItem),
XX(t, ThreadIdFormatItem),
XX(n, NewLineFormatItem),
XX(d, DateTimeFormatItem),
XX(f, FilenameFormatItem),
XX(l, LineFormatItem);
#undef XX
};
const char* LogLevel::ToString(LogLevel::Level level) {
switch(level) {
#define XX(name) \
case LogLevel::name \
return #name; \
break; // here will be a mistake
XX(DEBUG);
XX(INFO);
XX(WARN);
XX(ERROR);
XX(FATAL);
#undef XX
default:
return "UNKNOW";
}
return "UNKNUW";
}