Browse Source

Add logging class

master
Rick van Vonderen 5 years ago
parent
commit
12591e2cb8
  1. 6
      engine/src/engine.h
  2. 16
      engine/src/engine/application.cpp
  3. 4
      engine/src/engine/application.h
  4. 17
      engine/src/engine/entrypoint.h
  5. 53
      engine/src/engine/log.cpp
  6. 55
      engine/src/engine/log.h
  7. 2
      game/src/game.cpp

6
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

16
engine/src/engine/application.cpp

@ -4,6 +4,7 @@
#include <GLFW/glfw3.h>
#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!");
}
}

4
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();
}

17
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;

53
engine/src/engine/log.cpp

@ -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");
}
}

55
engine/src/engine/log.h

@ -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

2
game/src/game.cpp

@ -7,7 +7,7 @@ public:
~Game() {};
};
Engine::Application *Engine::CreateApplication()
Engine::Application *Engine::createApplication()
{
return new Game();
}

Loading…
Cancel
Save