Use type system and function overloading instead of enums and switch/case for selecting log levels. This should improve function inlining for the LOG(level) calls.

This commit is contained in:
Sebastian Meßmer 2015-10-17 16:00:13 +02:00
parent 0d6eef7ed2
commit 82e4aefdd2

View File

@ -7,10 +7,11 @@
namespace cpputils {
namespace logging {
enum Level {
ERROR, WARN, INFO, DEBUG
};
extern struct ERROR_TYPE {} ERROR;
extern struct WARN_TYPE {} WARN;
extern struct INFO_TYPE {} INFO;
extern struct DEBUG_TYPE {} DEBUG;
inline void setLogger(std::shared_ptr<spdlog::logger> newLogger) {
logger().setLogger(newLogger);
@ -20,24 +21,36 @@ namespace cpputils {
logger().reset();
}
inline void setLevel(Level level) {
switch(level) {
case ERROR: logger().setLevel(spdlog::level::err); return;
case WARN: logger().setLevel(spdlog::level::warn); return;
case INFO: logger().setLevel(spdlog::level::info); return;
case DEBUG: logger().setLevel(spdlog::level::debug); return;
}
throw std::logic_error("Unknown logger level");
inline void setLevel(ERROR_TYPE) {
logger().setLevel(spdlog::level::err);
}
inline spdlog::details::line_logger LOG(Level level) {
switch(level) {
case ERROR: return logger()->error();
case WARN: return logger()->warn();
case INFO: return logger()->info();
case DEBUG: return logger()->debug();
}
throw std::logic_error("Unknown logger level");
inline void setLevel(WARN_TYPE) {
logger().setLevel(spdlog::level::warn);
}
inline void setLevel(INFO_TYPE) {
logger().setLevel(spdlog::level::info);
}
inline void setLevel(DEBUG_TYPE) {
logger().setLevel(spdlog::level::debug);
}
inline spdlog::details::line_logger LOG(ERROR_TYPE) {
return logger()->error();
}
inline spdlog::details::line_logger LOG(WARN_TYPE) {
return logger()->warn();
}
inline spdlog::details::line_logger LOG(INFO_TYPE) {
return logger()->info();
}
inline spdlog::details::line_logger LOG(DEBUG_TYPE) {
return logger()->debug();
}
}
}