Skip to content

Commit

Permalink
Merge pull request #17977 from hrydgard/modernize-log-types
Browse files Browse the repository at this point in the history
Log level/type: Use enum class instead of the awkward namespace trick.
  • Loading branch information
hrydgard authored Aug 25, 2023
2 parents 308e983 + c59e9e5 commit 6b89788
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 177 deletions.
37 changes: 16 additions & 21 deletions Common/ConsoleListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ void ConsoleListener::LogWriterThread()

for (char *Text = logLocal, *End = logLocal + logLocalSize; Text < End; )
{
LogTypes::LOG_LEVELS Level = LogTypes::LINFO;
LogLevel Level = LogLevel::LINFO;

char *next = (char *) memchr(Text + 1, '\033', End - Text);
size_t Len = next - Text;
Expand All @@ -367,7 +367,7 @@ void ConsoleListener::LogWriterThread()

if (Text[0] == '\033' && Text + 1 < End)
{
Level = (LogTypes::LOG_LEVELS) (Text[1] - '0');
Level = (LogLevel)(Text[1] - '0');
Len -= 2;
Text += 2;
}
Expand All @@ -384,7 +384,7 @@ void ConsoleListener::LogWriterThread()
delete [] logLocal;
}

void ConsoleListener::SendToThread(LogTypes::LOG_LEVELS Level, const char *Text)
void ConsoleListener::SendToThread(LogLevel Level, const char *Text)
{
// Oops, we're already quitting. Just do nothing.
if (logPendingWritePos == (u32) -1)
Expand Down Expand Up @@ -462,7 +462,7 @@ void ConsoleListener::SendToThread(LogTypes::LOG_LEVELS Level, const char *Text)
SetEvent(hTriggerEvent);
}

void ConsoleListener::WriteToConsole(LogTypes::LOG_LEVELS Level, const char *Text, size_t Len)
void ConsoleListener::WriteToConsole(LogLevel Level, const char *Text, size_t Len)
{
_dbg_assert_msg_(IsOpen(), "Don't call this before opening the console.");

Expand All @@ -479,21 +479,20 @@ void ConsoleListener::WriteToConsole(LogTypes::LOG_LEVELS Level, const char *Tex
WORD Color;
static wchar_t tempBuf[2048];

switch (Level)
{
case NOTICE_LEVEL: // light green
switch (Level) {
case LogLevel::LNOTICE: // light green
Color = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case ERROR_LEVEL: // light red
case LogLevel::LERROR: // light red
Color = FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
case WARNING_LEVEL: // light yellow
case LogLevel::LWARNING: // light yellow
Color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case INFO_LEVEL: // cyan
case LogLevel::LINFO: // cyan
Color = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
break;
case DEBUG_LEVEL: // gray
case LogLevel::LDEBUG: // gray
Color = FOREGROUND_INTENSITY;
break;
default: // off-white
Expand Down Expand Up @@ -593,7 +592,7 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool
COORD Coo = GetCoordinates(OldCursor, LBufWidth);
SetConsoleCursorPosition(hConsole, Coo);

// if (SLog.length() > 0) Log(LogTypes::LNOTICE, SLog.c_str());
// if (SLog.length() > 0) Log(LogLevel::LNOTICE, SLog.c_str());

// Resize the window too
if (Resize) MoveWindow(GetConsoleWindow(), Left,Top, (Width + 100),Height, true);
Expand All @@ -615,18 +614,16 @@ void ConsoleListener::Log(const LogMessage &msg) {
char ColorAttr[16] = "";
char ResetAttr[16] = "";

if (bUseColor)
{
if (bUseColor) {
strcpy(ResetAttr, "\033[0m");
switch (msg.level)
{
case NOTICE_LEVEL: // light green
switch (msg.level) {
case LogLevel::LNOTICE: // light green
strcpy(ColorAttr, "\033[92m");
break;
case ERROR_LEVEL: // light red
case LogLevel::LERROR: // light red
strcpy(ColorAttr, "\033[91m");
break;
case WARNING_LEVEL: // light yellow
case LogLevel::LWARNING: // light yellow
strcpy(ColorAttr, "\033[93m");
break;
default:
Expand Down Expand Up @@ -656,5 +653,3 @@ void ConsoleListener::ClearScreen(bool Cursor)
if (Cursor) SetConsoleCursorPosition(hConsole, coordScreen);
#endif
}


4 changes: 2 additions & 2 deletions Common/ConsoleListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class ConsoleListener : public LogListener {

static unsigned int WINAPI RunThread(void *lpParam);
void LogWriterThread();
void SendToThread(LogTypes::LOG_LEVELS Level, const char *Text);
void WriteToConsole(LogTypes::LOG_LEVELS Level, const char *Text, size_t Len);
void SendToThread(LogLevel Level, const char *Text);
void WriteToConsole(LogLevel Level, const char *Text, size_t Len);

static int refCount;
static HANDLE hThread;
Expand Down
28 changes: 13 additions & 15 deletions Common/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
#define DEBUG_LEVEL 5 // Detailed debugging - might make things slow.
#define VERBOSE_LEVEL 6 // Noisy debugging - sometimes needed but usually unimportant.

namespace LogTypes {

enum LOG_TYPE {
// NOTE: Needs to be kept in sync with the g_logTypeNames array.
enum class LogType {
SYSTEM = 0, // Catch-all for uncategorized things
BOOT,
COMMON,
Expand Down Expand Up @@ -67,7 +66,7 @@ enum LOG_TYPE {
ACHIEVEMENTS = HLE, // TODO: Make a real category
};

enum LOG_LEVELS : int {
enum class LogLevel : int {
LNOTICE = NOTICE_LEVEL,
LERROR = ERROR_LEVEL,
LWARNING = WARNING_LEVEL,
Expand All @@ -76,19 +75,18 @@ enum LOG_LEVELS : int {
LVERBOSE = VERBOSE_LEVEL,
};

} // namespace

void GenericLog(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type,
void GenericLog(LogLevel level, LogType type,
const char *file, int line, const char *fmt, ...)
#ifdef __GNUC__
__attribute__((format(printf, 5, 6)))
#endif
;
bool GenericLogEnabled(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type);
bool GenericLogEnabled(LogLevel level, LogType type);

// Exception for Windows - enable more log levels in release mode than on other platforms.
#if defined(_DEBUG) || defined(_WIN32)

// Needs to be an int (and not use the enum) because it's used by the preprocessor!
#define MAX_LOGLEVEL DEBUG_LEVEL

#else
Expand All @@ -102,16 +100,16 @@ bool GenericLogEnabled(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type);
// Let the compiler optimize this out.
// TODO: Compute a dynamic max level as well that can be checked here.
#define GENERIC_LOG(t, v, ...) { \
if (v <= MAX_LOGLEVEL) \
if ((int)v <= MAX_LOGLEVEL) \
GenericLog(v, t, __FILE__, __LINE__, __VA_ARGS__); \
}

#define ERROR_LOG(t,...) do { GENERIC_LOG(LogTypes::t, LogTypes::LERROR, __VA_ARGS__) } while (false)
#define WARN_LOG(t,...) do { GENERIC_LOG(LogTypes::t, LogTypes::LWARNING, __VA_ARGS__) } while (false)
#define NOTICE_LOG(t,...) do { GENERIC_LOG(LogTypes::t, LogTypes::LNOTICE, __VA_ARGS__) } while (false)
#define INFO_LOG(t,...) do { GENERIC_LOG(LogTypes::t, LogTypes::LINFO, __VA_ARGS__) } while (false)
#define DEBUG_LOG(t,...) do { GENERIC_LOG(LogTypes::t, LogTypes::LDEBUG, __VA_ARGS__) } while (false)
#define VERBOSE_LOG(t,...) do { GENERIC_LOG(LogTypes::t, LogTypes::LVERBOSE, __VA_ARGS__) } while (false)
#define ERROR_LOG(t,...) do { GENERIC_LOG(LogType::t, LogLevel::LERROR, __VA_ARGS__) } while (false)
#define WARN_LOG(t,...) do { GENERIC_LOG(LogType::t, LogLevel::LWARNING, __VA_ARGS__) } while (false)
#define NOTICE_LOG(t,...) do { GENERIC_LOG(LogType::t, LogLevel::LNOTICE, __VA_ARGS__) } while (false)
#define INFO_LOG(t,...) do { GENERIC_LOG(LogType::t, LogLevel::LINFO, __VA_ARGS__) } while (false)
#define DEBUG_LOG(t,...) do { GENERIC_LOG(LogType::t, LogLevel::LDEBUG, __VA_ARGS__) } while (false)
#define VERBOSE_LOG(t,...) do { GENERIC_LOG(LogType::t, LogLevel::LVERBOSE, __VA_ARGS__) } while (false)

// Currently only actually shows a dialog box on Windows.
bool HandleAssert(const char *function, const char *file, int line, const char *expression, const char* format, ...)
Expand Down
105 changes: 51 additions & 54 deletions Common/LogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static const char level_to_char[8] = "-NEWIDV";
#define LOG_MSC_OUTPUTDEBUG false
#endif

void GenericLog(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char *file, int line, const char* fmt, ...) {
void GenericLog(LogLevel level, LogType type, const char *file, int line, const char* fmt, ...) {
if (g_bLogEnabledSetting && !(*g_bLogEnabledSetting))
return;
va_list args;
Expand All @@ -69,64 +69,61 @@ void GenericLog(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char
va_end(args);
}

bool GenericLogEnabled(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type) {
bool GenericLogEnabled(LogLevel level, LogType type) {
if (LogManager::GetInstance())
return (*g_bLogEnabledSetting) && LogManager::GetInstance()->IsEnabled(level, type);
return false;
}

LogManager *LogManager::logManager_ = NULL;

struct LogNameTableEntry {
LogTypes::LOG_TYPE logType;
const char *name;
};

static const LogNameTableEntry logTable[] = {
{LogTypes::SYSTEM, "SYSTEM"},
{LogTypes::BOOT, "BOOT"},
{LogTypes::COMMON, "COMMON"},
{LogTypes::CPU, "CPU"},
{LogTypes::FILESYS, "FILESYS"},
{LogTypes::G3D, "G3D"},
{LogTypes::HLE, "HLE"},
{LogTypes::JIT, "JIT"},
{LogTypes::LOADER, "LOADER"},
{LogTypes::ME, "ME"}, // Media Engine
{LogTypes::MEMMAP, "MEMMAP"},
{LogTypes::SASMIX, "SASMIX"},
{LogTypes::SAVESTATE, "SAVESTATE"},
{LogTypes::FRAMEBUF, "FRAMEBUF"},
{LogTypes::AUDIO, "AUDIO"},
{LogTypes::IO, "IO"},

{LogTypes::SCEAUDIO, "SCEAUDIO"},
{LogTypes::SCECTRL, "SCECTRL"},
{LogTypes::SCEDISPLAY, "SCEDISP"},
{LogTypes::SCEFONT, "SCEFONT"},
{LogTypes::SCEGE, "SCEGE"},
{LogTypes::SCEINTC, "SCEINTC"},
{LogTypes::SCEIO, "SCEIO"},
{LogTypes::SCEKERNEL, "SCEKERNEL"},
{LogTypes::SCEMODULE, "SCEMODULE"},
{LogTypes::SCENET, "SCENET"},
{LogTypes::SCERTC, "SCERTC"},
{LogTypes::SCESAS, "SCESAS"},
{LogTypes::SCEUTILITY, "SCEUTIL"},
{LogTypes::SCEMISC, "SCEMISC"},
// NOTE: Needs to be kept in sync with the LogType enum.
static const char *g_logTypeNames[] = {
"SYSTEM",
"BOOT",
"COMMON",
"CPU",
"FILESYS",
"G3D",
"HLE",
"JIT",
"LOADER",
"ME", // Media Engine
"MEMMAP",
"SASMIX",
"SAVESTATE",
"FRAMEBUF",
"AUDIO",
"IO",

"SCEAUDIO",
"SCECTRL",
"SCEDISP",
"SCEFONT",
"SCEGE",
"SCEINTC",
"SCEIO",
"SCEKERNEL",
"SCEMODULE",
"SCENET",
"SCERTC",
"SCESAS",
"SCEUTIL",
"SCEMISC",
};

LogManager::LogManager(bool *enabledSetting) {
g_bLogEnabledSetting = enabledSetting;

for (size_t i = 0; i < ARRAY_SIZE(logTable); i++) {
_assert_msg_(i == logTable[i].logType, "Bad logtable at %i", (int)i);
truncate_cpy(log_[logTable[i].logType].m_shortName, logTable[i].name);
log_[logTable[i].logType].enabled = true;
_dbg_assert_(ARRAY_SIZE(g_logTypeNames) == (size_t)LogType::NUMBER_OF_LOGS);

for (size_t i = 0; i < ARRAY_SIZE(g_logTypeNames); i++) {
truncate_cpy(log_[i].m_shortName, g_logTypeNames[i]);
log_[i].enabled = true;
#if defined(_DEBUG)
log_[logTable[i].logType].level = LogTypes::LDEBUG;
log_[i].level = LogLevel::LDEBUG;
#else
log_[logTable[i].logType].level = LogTypes::LINFO;
log_[i].level = LogLevel::LINFO;
#endif
}

Expand Down Expand Up @@ -158,7 +155,7 @@ LogManager::LogManager(bool *enabledSetting) {
}

LogManager::~LogManager() {
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i) {
for (int i = 0; i < (int)LogType::NUMBER_OF_LOGS; ++i) {
#if !defined(MOBILE_DEVICE) || defined(_DEBUG)
RemoveListener(fileLog_);
RemoveListener(consoleLog_);
Expand Down Expand Up @@ -194,25 +191,25 @@ void LogManager::ChangeFileLog(const char *filename) {
}

void LogManager::SaveConfig(Section *section) {
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; i++) {
for (int i = 0; i < (int)LogType::NUMBER_OF_LOGS; i++) {
section->Set((std::string(log_[i].m_shortName) + "Enabled").c_str(), log_[i].enabled);
section->Set((std::string(log_[i].m_shortName) + "Level").c_str(), (int)log_[i].level);
}
}

void LogManager::LoadConfig(const Section *section, bool debugDefaults) {
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; i++) {
for (int i = 0; i < (int)LogType::NUMBER_OF_LOGS; i++) {
bool enabled = false;
int level = 0;
section->Get((std::string(log_[i].m_shortName) + "Enabled").c_str(), &enabled, true);
section->Get((std::string(log_[i].m_shortName) + "Level").c_str(), &level, debugDefaults ? (int)LogTypes::LDEBUG : (int)LogTypes::LERROR);
section->Get((std::string(log_[i].m_shortName) + "Level").c_str(), &level, (int)(debugDefaults ? LogLevel::LDEBUG : LogLevel::LERROR));
log_[i].enabled = enabled;
log_[i].level = (LogTypes::LOG_LEVELS)level;
log_[i].level = (LogLevel)level;
}
}

void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char *file, int line, const char *format, va_list args) {
const LogChannel &log = log_[type];
void LogManager::Log(LogLevel level, LogType type, const char *file, int line, const char *format, va_list args) {
const LogChannel &log = log_[(size_t)type];
if (level > log.level || !log.enabled)
return;

Expand Down Expand Up @@ -268,8 +265,8 @@ void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const
}
}

bool LogManager::IsEnabled(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type) {
LogChannel &log = log_[type];
bool LogManager::IsEnabled(LogLevel level, LogType type) {
LogChannel &log = log_[(size_t)type];
if (level > log.level || !log.enabled)
return false;
return true;
Expand Down
Loading

0 comments on commit 6b89788

Please sign in to comment.