From c83acac32c5cb2b3d098c35ea4ba508a9e98162c Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 15 Jun 2024 13:08:13 +0200 Subject: [PATCH] Add a function to get debug level --- components/debug/debugging.cpp | 51 +++++++++++++++------------------- components/debug/debugging.hpp | 2 ++ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index 2b7cd39bbc..05327a2646 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -108,12 +108,6 @@ namespace Debug class DebugOutputBase : public boost::iostreams::sink { public: - DebugOutputBase() - { - if (CurrentDebugLevel == NoLevel) - fillCurrentDebugLevel(); - } - virtual std::streamsize write(const char* str, std::streamsize size) { if (size <= 0) @@ -174,29 +168,6 @@ namespace Debug return NoLevel; } - static void fillCurrentDebugLevel() - { - const char* env = getenv("OPENMW_DEBUG_LEVEL"); - if (env) - { - std::string value(env); - if (value == "ERROR") - CurrentDebugLevel = Error; - else if (value == "WARNING") - CurrentDebugLevel = Warning; - else if (value == "INFO") - CurrentDebugLevel = Info; - else if (value == "VERBOSE") - CurrentDebugLevel = Verbose; - else if (value == "DEBUG") - CurrentDebugLevel = Debug; - - return; - } - - CurrentDebugLevel = Verbose; - } - virtual std::streamsize writeImpl(const char* str, std::streamsize size, Level debugLevel) { return size; @@ -380,8 +351,30 @@ namespace Debug return Misc::Locked(*rawStderrMutex, getRawStderr()); } + Level getDebugLevel() + { + if (const char* env = getenv("OPENMW_DEBUG_LEVEL")) + { + const std::string_view value(env); + if (value == "ERROR") + return Error; + if (value == "WARNING") + return Warning; + if (value == "INFO") + return Info; + if (value == "VERBOSE") + return Verbose; + if (value == "DEBUG") + return Debug; + } + + return Verbose; + } + void setupLogging(const std::filesystem::path& logDir, std::string_view appName) { + CurrentDebugLevel = getDebugLevel(); + #if !(defined(_WIN32) && defined(_DEBUG)) const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log"; logfile.open(logDir / logName, std::ios::out); diff --git a/components/debug/debugging.hpp b/components/debug/debugging.hpp index 7efbffa657..68cdec93a2 100644 --- a/components/debug/debugging.hpp +++ b/components/debug/debugging.hpp @@ -34,6 +34,8 @@ namespace Debug Misc::Locked getLockedRawStderr(); + Level getDebugLevel(); + // Redirect cout and cerr to the log file void setupLogging(const std::filesystem::path& logDir, std::string_view appName);