diff --git a/src/xs_logging.cpp b/src/xs_logging.cpp index 90e2916..43f52a0 100644 --- a/src/xs_logging.cpp +++ b/src/xs_logging.cpp @@ -55,65 +55,67 @@ namespace logging void log(logging::Level level, const char * fmt, ...) { + if (level < _level) { + return; + } + std::string msg = ""; - if (level >= _level) { - switch (level) { - case Level::DEBUG: - msg += GRN; - msg += "[DEBUG] "; - break; - case Level::INFO: - msg += OFF; - msg += "[INFO] "; - break; - case Level::WARN: - msg += YLW; - msg += "[WARN] "; - break; - case Level::ERROR: - msg += RED; - msg += "[ERROR] "; - break; - case Level::FATAL: - msg += RED; - msg += "[FATAL] "; - break; - default: - break; - } + switch (level) { + case Level::DEBUG: + msg += GRN; + msg += "[DEBUG] "; + break; + case Level::INFO: + msg += OFF; + msg += "[INFO] "; + break; + case Level::WARN: + msg += YLW; + msg += "[WARN] "; + break; + case Level::ERROR: + msg += RED; + msg += "[ERROR] "; + break; + case Level::FATAL: + msg += RED; + msg += "[FATAL] "; + break; + default: + break; + } - auto duration = std::chrono::system_clock::now().time_since_epoch(); - double seconds = std::chrono::duration(duration).count(); - - msg = msg + - " [" + - std::to_string(seconds) + - "] "; - - va_list args; - va_start(args, fmt); - size_t size = 1024; - std::vector dynamicbuf(size); - char * buf = &dynamicbuf[0]; - - va_list argsTmp; - - while (1) { - va_copy(argsTmp, args); - int needed = vsnprintf(buf, size, fmt, argsTmp); - va_end(argsTmp); - if (needed < static_cast(size) - 1 && needed >= 0) { - msg.append(buf, static_cast(needed)); - break; - } - size = needed >= 0 ? needed + 2 : size * 2; - dynamicbuf.resize(size); - buf = &dynamicbuf[0]; + auto duration = std::chrono::system_clock::now().time_since_epoch(); + double seconds = std::chrono::duration(duration).count(); + + msg = msg + + "[" + + std::to_string(seconds) + + "] "; + + va_list args; + va_start(args, fmt); + size_t size = 1024; + std::vector dynamicbuf(size); + char * buf = &dynamicbuf[0]; + + va_list argsTmp; + + while (1) { + va_copy(argsTmp, args); + int needed = vsnprintf(buf, size, fmt, argsTmp); + va_end(argsTmp); + if (needed < static_cast(size) - 1 && needed >= 0) { + msg.append(buf, static_cast(needed)); + break; } - va_end(args); - msg.append(END); - std::cerr << msg.c_str(); + size = needed >= 0 ? needed + 2 : size * 2; + dynamicbuf.resize(size); + buf = &dynamicbuf[0]; } + va_end(args); + msg.append(END); + std::cerr << msg.c_str(); } void set_level(Level level)