Skip to content

Commit

Permalink
add colours to log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
arozx committed Jan 26, 2025
1 parent 1ed979e commit e268c84
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/Core/Utils/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ namespace Engine {
// Move this specialization to a separate cpp file or inline header
inline std::string ToString(const Window& window);

namespace LogColors
{
constexpr const char *Reset = "\033[0m";
constexpr const char *Yellow = "\033[33m";
constexpr const char *Red = "\033[31m";
constexpr const char *Purple = "\033[35m";
}

/**
* @brief Singleton logger class for application-wide logging
*/
Expand Down Expand Up @@ -99,16 +107,34 @@ namespace Engine {
std::strftime(buffer, 32, "%c", std::localtime(&now));
std::string timestamp(buffer);

const char *colorCode;
std::string levelStr;
switch (level) {
case LogLevel::Trace: levelStr = "TRACE"; break;
case LogLevel::Info: levelStr = "INFO"; break;
case LogLevel::Warn: levelStr = "WARN"; break;
case LogLevel::Error: levelStr = "ERROR"; break;
case LogLevel::Fatal: levelStr = "FATAL"; break;
case LogLevel::Trace:
colorCode = "";
levelStr = "TRACE";
break;
case LogLevel::Info:
colorCode = "";
levelStr = "INFO";
break;
case LogLevel::Warn:
colorCode = LogColors::Yellow;
levelStr = "WARN";
break;
case LogLevel::Error:
colorCode = LogColors::Red;
levelStr = "ERROR";
break;
case LogLevel::Fatal:
colorCode = LogColors::Purple;
levelStr = "FATAL";
break;
}

std::cout << "[" << timestamp << "] [" << levelStr << "]: " << message << std::endl;
std::cout << "[" << timestamp << "] "
<< colorCode << "[" << levelStr << "]: "
<< message << LogColors::Reset << std::endl;
}

/**
Expand Down

0 comments on commit e268c84

Please sign in to comment.