Rick van Vonderen
5 years ago
7 changed files with 143 additions and 10 deletions
@ -0,0 +1,53 @@
|
||||
#include <cstdio> // printf |
||||
|
||||
#include "log.h" |
||||
|
||||
namespace Engine { |
||||
|
||||
// Reserve memory
|
||||
std::shared_ptr<Logger> Log::m_engineLogger; |
||||
std::shared_ptr<Logger> Log::m_gameLogger; |
||||
|
||||
void Log::init() |
||||
{ |
||||
// Create engine Logger
|
||||
m_engineLogger = std::make_shared<Logger>("Engine"); |
||||
// Create game Logger
|
||||
m_gameLogger = std::make_shared<Logger>("Game"); |
||||
} |
||||
|
||||
Logger::Logger(const char *name) : |
||||
m_name(name) |
||||
{ |
||||
} |
||||
|
||||
Logger::~Logger() |
||||
{ |
||||
} |
||||
|
||||
void Logger::log(const char *message, const char *level) |
||||
{ |
||||
printf("%s%s: %s\033[0m\n", level, m_name, message); |
||||
} |
||||
|
||||
void Logger::info(const char *message) |
||||
{ |
||||
this->log(message, "\x1B[34m"); |
||||
} |
||||
|
||||
void Logger::warn(const char *message) |
||||
{ |
||||
this->log(message, "\x1B[33m"); |
||||
} |
||||
|
||||
void Logger::danger(const char *message) |
||||
{ |
||||
this->log(message, "\x1B[31m"); |
||||
} |
||||
|
||||
void Logger::success(const char *message) |
||||
{ |
||||
this->log(message, "\x1B[32m"); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,55 @@
|
||||
#ifndef LOG_H |
||||
#define LOG_H |
||||
|
||||
#include <memory> |
||||
|
||||
namespace Engine { |
||||
|
||||
class Logger; |
||||
|
||||
class Log |
||||
{ |
||||
public: |
||||
static void init(); |
||||
|
||||
// -----------------------------------------
|
||||
|
||||
inline static std::shared_ptr<Logger> &getEngineLogger() { return m_engineLogger; } |
||||
inline static std::shared_ptr<Logger> &getGameLogger() { return m_gameLogger; } |
||||
|
||||
private: |
||||
static std::shared_ptr<Logger> m_engineLogger; |
||||
static std::shared_ptr<Logger> m_gameLogger; |
||||
}; |
||||
|
||||
class Logger |
||||
{ |
||||
public: |
||||
Logger(const char *name); |
||||
~Logger(); |
||||
|
||||
void log(const char *message, const char *level = ""); |
||||
void info(const char *message); |
||||
void warn(const char *message); |
||||
void danger(const char *message); |
||||
void success(const char *message); |
||||
|
||||
private: |
||||
const char *m_name; |
||||
}; |
||||
|
||||
} |
||||
|
||||
#define LOG_ENGINE_LOG(x) Engine::Log::getEngineLogger()->log(x) |
||||
#define LOG_ENGINE_INFO(x) Engine::Log::getEngineLogger()->info(x) |
||||
#define LOG_ENGINE_WARN(x) Engine::Log::getEngineLogger()->warn(x) |
||||
#define LOG_ENGINE_DANGER(x) Engine::Log::getEngineLogger()->danger(x) |
||||
#define LOG_ENGINE_SUCCESS(x) Engine::Log::getEngineLogger()->success(x) |
||||
|
||||
#define LOG_LOG(x) Engine::Log::getGameLogger()->log(x) |
||||
#define LOG_INFO(x) Engine::Log::getGameLogger()->info(x) |
||||
#define LOG_WARN(x) Engine::Log::getGameLogger()->warn(x) |
||||
#define LOG_DANGER(x) Engine::Log::getGameLogger()->danger(x) |
||||
#define LOG_SUCCESS(x) Engine::Log::getGameLogger()->success(x) |
||||
|
||||
#endif // LOG_H
|
Loading…
Reference in new issue