diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index 3bfc024856..32aec8f0fc 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -113,12 +113,15 @@ namespace Debug { if (size <= 0) return size; - std::string_view msg{ str, size_t(size) }; + std::string_view msg{ str, static_cast(size) }; // Skip debug level marker - Level level = getLevelMarker(str); - if (level != NoLevel) + Level level = All; + if (Log::sWriteLevel) + { + level = getLevelMarker(msg[0]); msg = msg.substr(1); + } char prefix[32]; std::size_t prefixSize; @@ -159,14 +162,11 @@ namespace Debug virtual ~DebugOutputBase() = default; protected: - static Level getLevelMarker(const char* str) + static Level getLevelMarker(char marker) { - if (unsigned(*str) <= unsigned(Marker)) - { - return Level(*str); - } - - return NoLevel; + if (0 <= marker && static_cast(marker) < static_cast(All)) + return static_cast(marker); + return All; } virtual std::streamsize writeImpl(const char* str, std::streamsize size, Level debugLevel) @@ -216,7 +216,7 @@ namespace Debug return DarkGray; case Debug: return DarkGray; - case NoLevel: + case All: return Reset; } return Reset; @@ -378,7 +378,8 @@ namespace Debug void setupLogging(const std::filesystem::path& logDir, std::string_view appName) { - CurrentDebugLevel = getDebugLevel(); + Log::sMinDebugLevel = getDebugLevel(); + Log::sWriteLevel = true; #if !(defined(_WIN32) && defined(_DEBUG)) const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log"; @@ -473,7 +474,9 @@ namespace Debug // Restore cout and cerr std::cout.rdbuf(rawStdout->rdbuf()); std::cerr.rdbuf(rawStderr->rdbuf()); - CurrentDebugLevel = NoLevel; + + Log::sMinDebugLevel = All; + Log::sWriteLevel = false; return ret; } diff --git a/components/debug/debuglog.cpp b/components/debug/debuglog.cpp index 2bdb8ab139..f8ab034a1c 100644 --- a/components/debug/debuglog.cpp +++ b/components/debug/debuglog.cpp @@ -5,15 +5,13 @@ #include #include -namespace Debug -{ - Level CurrentDebugLevel = Level::NoLevel; -} - static std::mutex sLock; +Debug::Level Log::sMinDebugLevel = Debug::All; +bool Log::sWriteLevel = false; + Log::Log(Debug::Level level) - : mShouldLog(level <= Debug::CurrentDebugLevel) + : mShouldLog(level <= sMinDebugLevel) { // No need to hold the lock if there will be no logging anyway if (!mShouldLog) @@ -22,9 +20,7 @@ Log::Log(Debug::Level level) // Locks a global lock while the object is alive sLock.lock(); - // If the app has no logging system enabled, log level is not specified. - // Show all messages without marker - we just use the plain cout in this case. - if (Debug::CurrentDebugLevel == Debug::NoLevel) + if (!sWriteLevel) return; std::cout << static_cast(level); diff --git a/components/debug/debuglog.hpp b/components/debug/debuglog.hpp index 090f64c5ed..05ebe5fdaa 100644 --- a/components/debug/debuglog.hpp +++ b/components/debug/debuglog.hpp @@ -6,24 +6,23 @@ namespace Debug { - enum Level + enum Level : unsigned { Error = 1, Warning = 2, Info = 3, Verbose = 4, Debug = 5, - Marker = Debug, - - NoLevel = 6 // Do not filter messages in this case + All = 6, }; - - extern Level CurrentDebugLevel; } class Log { public: + static Debug::Level sMinDebugLevel; + static bool sWriteLevel; + explicit Log(Debug::Level level); ~Log(); diff --git a/components/myguiplatform/myguiloglistener.cpp b/components/myguiplatform/myguiloglistener.cpp index 89de63c46f..3e52e75ad2 100644 --- a/components/myguiplatform/myguiloglistener.cpp +++ b/components/myguiplatform/myguiloglistener.cpp @@ -40,7 +40,7 @@ namespace osgMyGUI MyGUI::LogLevel LogFacility::getCurrentLogLevel() const { - switch (Debug::CurrentDebugLevel) + switch (Log::sMinDebugLevel) { case Debug::Error: return MyGUI::LogLevel::Error;