From 12591e2cb83331f2f1d203af062c1d7d447de465 Mon Sep 17 00:00:00 2001 From: Rick van Vonderen <0945444@hr.nl> Date: Wed, 11 Dec 2019 15:26:16 +0100 Subject: [PATCH] Add logging class --- engine/src/engine.h | 6 ++++ engine/src/engine/application.cpp | 16 ++++++--- engine/src/engine/application.h | 4 +-- engine/src/engine/entrypoint.h | 17 ++++++++-- engine/src/engine/log.cpp | 53 +++++++++++++++++++++++++++++ engine/src/engine/log.h | 55 +++++++++++++++++++++++++++++++ game/src/game.cpp | 2 +- 7 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 engine/src/engine/log.cpp create mode 100644 engine/src/engine/log.h diff --git a/engine/src/engine.h b/engine/src/engine.h index a45e2db..de7f8e2 100644 --- a/engine/src/engine.h +++ b/engine/src/engine.h @@ -3,7 +3,13 @@ // This file is for use by the game +// ----------------------------------------- + #include "engine/application.h" +#include "engine/log.h" + +// ----------------------------------------- + #include "engine/entrypoint.h" #endif // ENGINE_H diff --git a/engine/src/engine/application.cpp b/engine/src/engine/application.cpp index 2071ab1..69fd032 100644 --- a/engine/src/engine/application.cpp +++ b/engine/src/engine/application.cpp @@ -4,6 +4,7 @@ #include #include "application.h" +#include "log.h" namespace Engine { @@ -15,9 +16,9 @@ namespace Engine { { } - void Application::Run() + void Application::run() { - printf("Hello from Application!\n"); + LOG_ENGINE_LOG("Startup!"); glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); @@ -27,20 +28,25 @@ namespace Engine { GLFWwindow* window = glfwCreateWindow(1280, 720, "Engine", NULL, NULL); if (window == NULL) { - printf("Failed to create GLFW window\n"); + LOG_ENGINE_DANGER("Failed to create GLFW window"); glfwTerminate(); return;// -1; } glfwMakeContextCurrent(window); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { - printf("Failed to initialize GLAD\n"); + LOG_ENGINE_DANGER("Failed to initialize GLAD"); return;// -1; } glViewport(0, 0, 1280, 720); while(!glfwWindowShouldClose(window)) { + + if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { + glfwSetWindowShouldClose(window, GL_TRUE); + } + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -49,6 +55,8 @@ namespace Engine { } glfwTerminate(); + + LOG_ENGINE_LOG("Shutdown!"); } } diff --git a/engine/src/engine/application.h b/engine/src/engine/application.h index 5d9576d..6bebf18 100644 --- a/engine/src/engine/application.h +++ b/engine/src/engine/application.h @@ -9,11 +9,11 @@ namespace Engine { Application(); virtual ~Application(); - void Run(); + void run(); }; // To be defined in the game - Application *CreateApplication(); + Application *createApplication(); } diff --git a/engine/src/engine/entrypoint.h b/engine/src/engine/entrypoint.h index 3d1a081..c8bac17 100644 --- a/engine/src/engine/entrypoint.h +++ b/engine/src/engine/entrypoint.h @@ -1,9 +1,16 @@ +/* + * `m_` for member variables, + * `s_` for static variables, + * `g_` for global variables. + */ + #ifndef ENTRYPOINT_H #define ENTRYPOINT_H #include "application.h" +#include "log.h" -extern Engine::Application *Engine::CreateApplication(); +extern Engine::Application *Engine::createApplication(); int main(int argc, char *argv[]) { @@ -11,8 +18,12 @@ int main(int argc, char *argv[]) (void)argc; (void)argv; - auto app = Engine::CreateApplication(); - app->Run(); + // Initialize Log + Engine::Log::init(); + + // Start application + auto app = Engine::createApplication(); + app->run(); delete app; return 0; diff --git a/engine/src/engine/log.cpp b/engine/src/engine/log.cpp new file mode 100644 index 0000000..648719a --- /dev/null +++ b/engine/src/engine/log.cpp @@ -0,0 +1,53 @@ +#include // printf + +#include "log.h" + +namespace Engine { + + // Reserve memory + std::shared_ptr Log::m_engineLogger; + std::shared_ptr Log::m_gameLogger; + + void Log::init() + { + // Create engine Logger + m_engineLogger = std::make_shared("Engine"); + // Create game Logger + m_gameLogger = std::make_shared("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"); + } + +} diff --git a/engine/src/engine/log.h b/engine/src/engine/log.h new file mode 100644 index 0000000..0a0a94c --- /dev/null +++ b/engine/src/engine/log.h @@ -0,0 +1,55 @@ +#ifndef LOG_H +#define LOG_H + +#include + +namespace Engine { + + class Logger; + + class Log + { + public: + static void init(); + +// ----------------------------------------- + + inline static std::shared_ptr &getEngineLogger() { return m_engineLogger; } + inline static std::shared_ptr &getGameLogger() { return m_gameLogger; } + + private: + static std::shared_ptr m_engineLogger; + static std::shared_ptr 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 diff --git a/game/src/game.cpp b/game/src/game.cpp index 44427ce..0127718 100644 --- a/game/src/game.cpp +++ b/game/src/game.cpp @@ -7,7 +7,7 @@ public: ~Game() {}; }; -Engine::Application *Engine::CreateApplication() +Engine::Application *Engine::createApplication() { return new Game(); }