If you want to achieve
LogInfo("log info %d %d %d", 1, 2, 3);
LogError("log info %d %d %d", 1, 2, 3);
Then you may want just to write some wrapper functions around your original Logger. Like this:
void LogInfo(const char* fmt...) {
SomeNamespace::Logger::instance().log(SomeNamespace::Logger::Priority::Info, fmt);
}
void LogError(const char* fmt...) {
SomeNamespace::Logger::instance().log(SomeNamespace::Logger::Priority::Error, fmt);
}
But if you want to reduce code duplication and automatically generate all these functions depending on your Priority
contents, then I do not see any way except macro, because there is no mechanisms in C++ to generate function declarations with different names. Sorry, but I can only advise this solution:
#define DeclareLogFunction(LEVEL) \
void Log##LEVEL(const char* fmt...) { \
SomeNamespace::Logger::instance().log(SomeNamespace::Logger::Priority::LEVEL, fmt); \
}
DeclareLogFunction(Debug)
DeclareLogFunction(Info)
DeclareLogFunction(Error)
DeclareLogFunction(Critical)
int main(){
LogError("log info %d %d %d", 1, 2, 3);
}
Also, you probably might be interested in this approach:
template <SomeNamespace::Logger::Priority P>
void Log(const char* fmt...) {
SomeNamespace::Logger::instance().log(P, fmt);
}
int main() {
Log<SomeNamespace::Logger::Priority::Info>("log info %d %d %d", 1, 2, 3);
}
It breaks your desired code a bit, but looks fine for me, and no macros.