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)
return size;
std::string_view msg{ str, size_t(size) };
std::string_view msg{ str, static_cast<size_t>(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<unsigned>(marker) < static_cast<unsigned>(All))
return static_cast<Level>(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;
}

@ -5,15 +5,13 @@
#include <components/files/conversion.hpp>
#include <components/misc/strings/conversion.hpp>
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<unsigned char>(level);

@ -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();

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

Loading…
Cancel
Save