Add logging class
This commit is contained in:
@@ -3,7 +3,13 @@
|
|||||||
|
|
||||||
// This file is for use by the game
|
// This file is for use by the game
|
||||||
|
|
||||||
|
// -----------------------------------------
|
||||||
|
|
||||||
#include "engine/application.h"
|
#include "engine/application.h"
|
||||||
|
#include "engine/log.h"
|
||||||
|
|
||||||
|
// -----------------------------------------
|
||||||
|
|
||||||
#include "engine/entrypoint.h"
|
#include "engine/entrypoint.h"
|
||||||
|
|
||||||
#endif // ENGINE_H
|
#endif // ENGINE_H
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include "application.h"
|
#include "application.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
namespace Engine {
|
namespace Engine {
|
||||||
|
|
||||||
@@ -15,9 +16,9 @@ namespace Engine {
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::Run()
|
void Application::run()
|
||||||
{
|
{
|
||||||
printf("Hello from Application!\n");
|
LOG_ENGINE_LOG("Startup!");
|
||||||
|
|
||||||
glfwInit();
|
glfwInit();
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||||
@@ -27,20 +28,25 @@ namespace Engine {
|
|||||||
|
|
||||||
GLFWwindow* window = glfwCreateWindow(1280, 720, "Engine", NULL, NULL);
|
GLFWwindow* window = glfwCreateWindow(1280, 720, "Engine", NULL, NULL);
|
||||||
if (window == NULL) {
|
if (window == NULL) {
|
||||||
printf("Failed to create GLFW window\n");
|
LOG_ENGINE_DANGER("Failed to create GLFW window");
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return;// -1;
|
return;// -1;
|
||||||
}
|
}
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||||
printf("Failed to initialize GLAD\n");
|
LOG_ENGINE_DANGER("Failed to initialize GLAD");
|
||||||
return;// -1;
|
return;// -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
glViewport(0, 0, 1280, 720);
|
glViewport(0, 0, 1280, 720);
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
|
|
||||||
|
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||||
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
@@ -49,6 +55,8 @@ namespace Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
|
||||||
|
LOG_ENGINE_LOG("Shutdown!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ namespace Engine {
|
|||||||
Application();
|
Application();
|
||||||
virtual ~Application();
|
virtual ~Application();
|
||||||
|
|
||||||
void Run();
|
void run();
|
||||||
};
|
};
|
||||||
|
|
||||||
// To be defined in the game
|
// To be defined in the game
|
||||||
Application *CreateApplication();
|
Application *createApplication();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* `m_` for member variables,
|
||||||
|
* `s_` for static variables,
|
||||||
|
* `g_` for global variables.
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef ENTRYPOINT_H
|
#ifndef ENTRYPOINT_H
|
||||||
#define ENTRYPOINT_H
|
#define ENTRYPOINT_H
|
||||||
|
|
||||||
#include "application.h"
|
#include "application.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
extern Engine::Application *Engine::CreateApplication();
|
extern Engine::Application *Engine::createApplication();
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -11,8 +18,12 @@ int main(int argc, char *argv[])
|
|||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
||||||
auto app = Engine::CreateApplication();
|
// Initialize Log
|
||||||
app->Run();
|
Engine::Log::init();
|
||||||
|
|
||||||
|
// Start application
|
||||||
|
auto app = Engine::createApplication();
|
||||||
|
app->run();
|
||||||
delete app;
|
delete app;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -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
|
||||||
+1
-1
@@ -7,7 +7,7 @@ public:
|
|||||||
~Game() {};
|
~Game() {};
|
||||||
};
|
};
|
||||||
|
|
||||||
Engine::Application *Engine::CreateApplication()
|
Engine::Application *Engine::createApplication()
|
||||||
{
|
{
|
||||||
return new Game();
|
return new Game();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user