diff --git a/src/util/log/log.cpp b/src/util/log/log.cpp index 93f7e3c3d..ddc4024e6 100644 --- a/src/util/log/log.cpp +++ b/src/util/log/log.cpp @@ -1,39 +1,73 @@ #include "log.h" -#include "../../dxvk/dxvk_main.h" +#include "../util_env.h" namespace dxvk { Logger::Logger(const std::string& file_name) - : m_fileStream(file_name) { } + : m_minLevel(getMinLogLevel()), + m_fileStream(file_name) { } + + Logger::~Logger() { } void Logger::trace(const std::string& message) { - s_instance.log(std::string("trace: ") + message); + s_instance.log(LogLevel::Trace, message); + } + + + void Logger::debug(const std::string& message) { + s_instance.log(LogLevel::Debug, message); } void Logger::info(const std::string& message) { - s_instance.log(std::string("info: ") + message); + s_instance.log(LogLevel::Info, message); } void Logger::warn(const std::string& message) { - s_instance.log(std::string("warn: ") + message); + s_instance.log(LogLevel::Warn, message); } void Logger::err(const std::string& message) { - s_instance.log(std::string("err: ") + message); + s_instance.log(LogLevel::Error, message); } - void Logger::log(const std::string& message) { - std::lock_guard lock(m_mutex); - std::cerr << message << std::endl; - m_fileStream << message << std::endl; - m_fileStream.flush(); + void Logger::log(LogLevel level, const std::string& message) { + if (level >= m_minLevel) { + std::lock_guard lock(m_mutex); + + static std::array s_prefixes + = {{ "trace: ", "debug: ", "info: ", "warn: ", "err: " }}; + + std::cerr << s_prefixes.at(static_cast(level)) << message << std::endl; + m_fileStream << message << std::endl; + m_fileStream.flush(); + } + } + + + LogLevel Logger::getMinLogLevel() { + const std::array, 5> logLevels = {{ + { "trace", LogLevel::Trace }, + { "debug", LogLevel::Debug }, + { "info", LogLevel::Info }, + { "warn", LogLevel::Warn }, + { "error", LogLevel::Error }, + }}; + + const std::string logLevelStr = env::getEnvVar(L"DXVK_LOG_LEVEL"); + + for (const auto& pair : logLevels) { + if (logLevelStr == pair.first) + return pair.second; + } + + return LogLevel::Info; } } \ No newline at end of file diff --git a/src/util/log/log.h b/src/util/log/log.h index 2d42af796..c39bf0bba 100644 --- a/src/util/log/log.h +++ b/src/util/log/log.h @@ -5,10 +5,16 @@ #include #include -#include "../rc/util_rc.h" - namespace dxvk { + enum class LogLevel : uint32_t { + Trace = 0, + Debug = 1, + Info = 2, + Warn = 3, + Error = 4, + }; + /** * \brief Logger * @@ -23,6 +29,7 @@ namespace dxvk { ~Logger(); static void trace(const std::string& message); + static void debug(const std::string& message); static void info (const std::string& message); static void warn (const std::string& message); static void err (const std::string& message); @@ -31,10 +38,14 @@ namespace dxvk { static Logger s_instance; + const LogLevel m_minLevel; + std::mutex m_mutex; std::ofstream m_fileStream; - void log(const std::string& message); + void log(LogLevel level, const std::string& message); + + static LogLevel getMinLogLevel(); };