Start working on logging

This commit is contained in:
SeanOMik 2022-10-28 18:49:29 -04:00
parent 8493e2aa86
commit e634a4dfce
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
6 changed files with 173 additions and 0 deletions

View File

@ -21,12 +21,16 @@ find_package(GLEW REQUIRED)
find_package(glfw3 CONFIG REQUIRED)
find_package(glm CONFIG REQUIRED)
find_package(OpenGL REQUIRED)
find_package(fmt REQUIRED)
find_package(spdlog REQUIRED)
find_package(assimp CONFIG REQUIRED)
# Link sources
file(GLOB_RECURSE source_list src/*.cpp)
add_library(simpleengine STATIC ${source_list})
target_compile_definitions(simpleengine PUBLIC SPDLOG_FMT_EXTERNAL)
# Link headers
target_include_directories(simpleengine PUBLIC include PRIVATE include/simpleengine)
@ -43,6 +47,8 @@ cmrc_add_resource_library(
# Link dependencies
target_link_libraries(simpleengine PUBLIC GLEW::GLEW)
target_link_libraries(simpleengine PUBLIC glfw)
target_link_libraries(simpleengine PUBLIC fmt)
target_link_libraries(simpleengine PUBLIC spdlog)
target_link_libraries(simpleengine PUBLIC ${GLM_LIBRARIES})
target_link_libraries(simpleengine PUBLIC ${OPENGL_LIBRARIES})
if(WIN32)

View File

@ -21,6 +21,7 @@
#include <simpleengine/scene.h>
#include <simpleengine/shader_program.h>
#include <simpleengine/vertex.h>
#include <simpleengine/log/logger.h>
#include <assimp/material.h>
#include <glm/ext/matrix_clip_space.hpp>
@ -29,6 +30,7 @@
#include <chrono>
#include <iostream>
#include <memory>
#include <spdlog/common.h>
#include <sstream>
#include <stdint.h>
@ -102,6 +104,11 @@ public:
int main(int argc, char *argv[]) {
se::Game game(640, 480, "SimpleEngine 3D OpenGL - Developer Testing", GLFW_OPENGL_CORE_PROFILE, 4, 4, false);
se::log::LoggerManager::set_level(spdlog::level::trace);
se::log::LoggerPtr logger = se::log::LoggerManager::get_core_logger();
logger->info("Hmmmm very cool");
// Load core shaders from SimpleEngine resources
se::gfx::shaders::Core3dShader core_shader;
@ -113,6 +120,8 @@ int main(int argc, char *argv[]) {
renderer->initialize();
//game.add_renderable(renderer);
logger->error("AHHHHH SOMETHING BAD!");
// Create a Scene and give it the renderer
auto scene = std::make_shared<se::Scene>(renderer, camera);
//game.add_event(scene);

View File

@ -0,0 +1,122 @@
//#define SPDLOG_FMT_EXTERNAL
#include <memory>
#include <string>
#include <unordered_map>
#include <spdlog/logger.h>
#include <spdlog/spdlog.h>
#include <spdlog/common.h>
#include <spdlog/sinks/stdout_color_sinks.h>
namespace simpleengine::log {
class Logger {
private:
std::shared_ptr<spdlog::logger> inner;
public:
Logger(std::string name) {
inner = spdlog::get(name);
if (!inner) {
inner = spdlog::stderr_color_mt(name);
}
}
Logger(std::shared_ptr<spdlog::logger> logger) : inner(logger) {
}
std::shared_ptr<spdlog::logger> get_inner() {
return inner;
}
void set_level(spdlog::level::level_enum lvl) {
inner->set_level(lvl);
}
void log(spdlog::level::level_enum lvl, spdlog::string_view_t msg) {
inner->log(spdlog::source_loc{}, lvl, msg);
}
template<typename T>
void trace(const T &msg) {
log(spdlog::level::trace, msg);
}
template<typename T>
void debug(const T &msg) {
log(spdlog::level::debug, msg);
}
template<typename T>
void info(const T &msg) {
log(spdlog::level::info, msg);
}
template<typename T>
void warn(const T &msg) {
log(spdlog::level::warn, msg);
}
template<typename T>
void error(const T &msg) {
log(spdlog::level::err, msg);
}
template<typename T>
void critical(const T &msg) {
log(spdlog::level::critical, msg);
}
template<typename... Args>
void trace(spdlog::format_string_t<Args...> fmt, Args &&... args) {
log(spdlog::level::trace, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void debug(spdlog::format_string_t<Args...> fmt, Args &&... args) {
log(spdlog::level::debug, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void info(spdlog::format_string_t<Args...> fmt, Args &&... args) {
log(spdlog::level::info, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void warn(spdlog::format_string_t<Args...> fmt, Args &&... args) {
log(spdlog::level::warn, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void error(spdlog::format_string_t<Args...> fmt, Args &&... args) {
log(spdlog::level::err, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void critical(spdlog::format_string_t<Args...> fmt, Args &&... args) {
log(spdlog::level::critical, fmt, std::forward<Args>(args)...);
}
};
using LoggerPtr = std::shared_ptr<Logger>;
class LoggerManager {
private:
static std::shared_ptr<Logger> core_logger;
public:
/* LoggerManager() {
spdlog::set_pattern("[%R - %D] [%l] [thread %t] [%n] %v");
core_logger = std::make_shared<Logger>("Engine");
} */
static void init();
static void set_level(spdlog::level::level_enum lvl);
static std::shared_ptr<Logger> get_core_logger();
static Logger create_logger(std::string name);
static Logger get_logger(std::string name);
};
}

View File

@ -12,5 +12,6 @@ pkgs.mkShell {
glfw
glm
assimp
spdlog
];
}

View File

@ -1,6 +1,7 @@
#include "game.h"
#include "event/event.h"
#include "renderable.h"
#include "log/logger.h"
#include <chrono>
#include <iostream>
@ -45,6 +46,8 @@ simpleengine::Game::Game(int w, int h, const std::string& window_name, const int
update_enabled_vsync();
last_frame_time = std::chrono::high_resolution_clock::now();
log::LoggerManager::init();
}
void simpleengine::Game::initialize(const int& gl_profile, const int& major_version, const int& minor_version,

32
src/log/logger.cpp Normal file
View File

@ -0,0 +1,32 @@
#include "log/logger.h"
#include <spdlog/common.h>
#include <spdlog/spdlog.h>
namespace simpleengine::log {
void LoggerManager::init() {
spdlog::set_pattern("[%T %D] [thread %t] [%n] [%l] %v");
core_logger = std::make_shared<Logger>("engine");
spdlog::set_default_logger(core_logger->get_inner());
set_level(spdlog::level::info);
}
void LoggerManager::set_level(spdlog::level::level_enum lvl) {
spdlog::set_level(lvl);
}
std::shared_ptr<Logger> LoggerManager::core_logger;
std::shared_ptr<Logger> LoggerManager::get_core_logger() {
return core_logger;
}
Logger LoggerManager::create_logger(std::string name) {
return Logger(name);
}
Logger LoggerManager::get_logger(std::string name) {
return Logger(spdlog::get(name));
}
}