Separate the log level and the flag for writing the log level

pull/3236/head
elsid 7 months ago
parent 98a8dcbdbf
commit 6a67263d6b
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -113,12 +113,15 @@ namespace Debug
{ {
if (size <= 0) if (size <= 0)
return size; return size;
std::string_view msg{ str, size_t(size) }; std::string_view msg{ str, static_cast<size_t>(size) };
// Skip debug level marker // Skip debug level marker
Level level = getLevelMarker(str); Level level = All;
if (level != NoLevel) if (Log::sWriteLevel)
{
level = getLevelMarker(msg[0]);
msg = msg.substr(1); msg = msg.substr(1);
}
char prefix[32]; char prefix[32];
std::size_t prefixSize; std::size_t prefixSize;
@ -159,14 +162,11 @@ namespace Debug
virtual ~DebugOutputBase() = default; virtual ~DebugOutputBase() = default;
protected: protected:
static Level getLevelMarker(const char* str) static Level getLevelMarker(char marker)
{ {
if (unsigned(*str) <= unsigned(Marker)) if (0 <= marker && static_cast<unsigned>(marker) < static_cast<unsigned>(All))
{ return static_cast<Level>(marker);
return Level(*str); return All;
}
return NoLevel;
} }
virtual std::streamsize writeImpl(const char* str, std::streamsize size, Level debugLevel) virtual std::streamsize writeImpl(const char* str, std::streamsize size, Level debugLevel)
@ -216,7 +216,7 @@ namespace Debug
return DarkGray; return DarkGray;
case Debug: case Debug:
return DarkGray; return DarkGray;
case NoLevel: case All:
return Reset; return Reset;
} }
return Reset; return Reset;
@ -378,7 +378,8 @@ namespace Debug
void setupLogging(const std::filesystem::path& logDir, std::string_view appName) void setupLogging(const std::filesystem::path& logDir, std::string_view appName)
{ {
CurrentDebugLevel = getDebugLevel(); Log::sMinDebugLevel = getDebugLevel();
Log::sWriteLevel = true;
#if !(defined(_WIN32) && defined(_DEBUG)) #if !(defined(_WIN32) && defined(_DEBUG))
const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log"; const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log";
@ -473,7 +474,9 @@ namespace Debug
// Restore cout and cerr // Restore cout and cerr
std::cout.rdbuf(rawStdout->rdbuf()); std::cout.rdbuf(rawStdout->rdbuf());
std::cerr.rdbuf(rawStderr->rdbuf()); std::cerr.rdbuf(rawStderr->rdbuf());
CurrentDebugLevel = NoLevel;
Log::sMinDebugLevel = All;
Log::sWriteLevel = false;
return ret; return ret;
} }

@ -5,15 +5,13 @@
#include <components/files/conversion.hpp> #include <components/files/conversion.hpp>
#include <components/misc/strings/conversion.hpp> #include <components/misc/strings/conversion.hpp>
namespace Debug
{
Level CurrentDebugLevel = Level::NoLevel;
}
static std::mutex sLock; static std::mutex sLock;
Debug::Level Log::sMinDebugLevel = Debug::All;
bool Log::sWriteLevel = false;
Log::Log(Debug::Level level) 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 // No need to hold the lock if there will be no logging anyway
if (!mShouldLog) if (!mShouldLog)
@ -22,9 +20,7 @@ Log::Log(Debug::Level level)
// Locks a global lock while the object is alive // Locks a global lock while the object is alive
sLock.lock(); sLock.lock();
// If the app has no logging system enabled, log level is not specified. if (!sWriteLevel)
// Show all messages without marker - we just use the plain cout in this case.
if (Debug::CurrentDebugLevel == Debug::NoLevel)
return; return;
std::cout << static_cast<unsigned char>(level); std::cout << static_cast<unsigned char>(level);

@ -6,24 +6,23 @@
namespace Debug namespace Debug
{ {
enum Level enum Level : unsigned
{ {
Error = 1, Error = 1,
Warning = 2, Warning = 2,
Info = 3, Info = 3,
Verbose = 4, Verbose = 4,
Debug = 5, Debug = 5,
Marker = Debug, All = 6,
NoLevel = 6 // Do not filter messages in this case
}; };
extern Level CurrentDebugLevel;
} }
class Log class Log
{ {
public: public:
static Debug::Level sMinDebugLevel;
static bool sWriteLevel;
explicit Log(Debug::Level level); explicit Log(Debug::Level level);
~Log(); ~Log();

@ -40,7 +40,7 @@ namespace osgMyGUI
MyGUI::LogLevel LogFacility::getCurrentLogLevel() const MyGUI::LogLevel LogFacility::getCurrentLogLevel() const
{ {
switch (Debug::CurrentDebugLevel) switch (Log::sMinDebugLevel)
{ {
case Debug::Error: case Debug::Error:
return MyGUI::LogLevel::Error; return MyGUI::LogLevel::Error;

Loading…
Cancel
Save