FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(log): add logging library using `spdlog` by Miou-zora · Pull Request #56 · EngineSquared/EngineSquared · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
748b46d
feat(utils): add string utility functions
Miou-zora Nov 12, 2024
85406af
feat(log): add log plugin with basic functions
Miou-zora Nov 12, 2024
a4f0c68
refacto: change std::cout and std::cerr for log functions
Miou-zora Nov 12, 2024
ef06977
style: apply linter
github-actions[bot] Nov 12, 2024
ce32428
Merge branch 'main' into 49-add-logging-possibility
Miou-zora Nov 12, 2024
b407854
fix: resolve merge conflict
Miou-zora Nov 12, 2024
60d699b
Merge branch 'main' into 49-add-logging-possibility
ripel2 Nov 13, 2024
4aacbdc
refacto: change context of log lib from Plugin to Utils
Miou-zora Nov 14, 2024
bd4a119
refacto(VkWrapper): create string before logging it
Miou-zora Nov 14, 2024
e7d2d22
refacto(VkWrapper): put severity map in header file (with inline static)
Miou-zora Nov 14, 2024
c5f42ba
fix(VkWrapper): add missing include
Miou-zora Nov 14, 2024
4c3f95c
style: apply linter
github-actions[bot] Nov 14, 2024
9cbdb42
test(String): split tests into multiple TEST's gtest call
Miou-zora Nov 14, 2024
79c7573
Merge branch '49-add-logging-possibility' of https://github.com/Engin…
Miou-zora Nov 14, 2024
bd6dacd
Merge branch 'main' into 49-add-logging-possibility
Miou-zora Nov 14, 2024
de02a60
refacto(String): make String utils rather than plugin
Miou-zora Nov 14, 2024
fa70458
style: apply linter
github-actions[bot] Nov 14, 2024
d5ec91b
refacto(Scene): remove useless namespace prefix
Miou-zora Nov 14, 2024
4cb5f40
Merge branch '49-add-logging-possibility' of https://github.com/Engin…
Miou-zora Nov 14, 2024
File filter

Filter by extension

Filter by extension .cpp  (7) .hpp  (4) .lua  (6) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 6 additions & 7 deletions src/plugin/scene/src/resource/SceneManager.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Engine.hpp"

#include "Logger.hpp"

#include "AScene.hpp"

#include "SceneManager.hpp"
Expand All @@ -8,24 +10,20 @@ void ES::Plugin::Scene::Resource::SceneManager::Update(ES::Engine::Registry &reg
{
if (!_nextScene.has_value())
{
std::cout
<< "[WARNING] ES::Plugin::Scene::Resource::SceneManager: Unable to load next scene: No next scene provided"
<< std::endl;
ES::Utils::Log::Warn("Unable to load next scene: No next scene provided");
return;
}
if (_currentScene.has_value())
{
std::cout << "[INFO] ES::Plugin::Scene::Resource::SceneManager: Unloading scene: " << _currentScene.value()
<< std::endl;
_unloadScene(registry, _currentScene.value());
}
std::cout << "[INFO] ES::Plugin::Scene::Resource::SceneManager: Loading scene: " << _nextScene.value() << std::endl;
_loadScene(registry, _nextScene.value());
_currentScene = _nextScene;
_nextScene.reset();
}
void ES::Plugin::Scene::Resource::SceneManager::_loadScene(ES::Engine::Registry &registry, const std::string &name)
{
ES::Utils::Log::Info("Loading scene: " + _nextScene.value());
std::optional<std::shared_ptr<ES::Plugin::Scene::Utils::AScene>> scene = _getScene(name);
if (scene.has_value())
{
Expand All @@ -35,6 +33,7 @@ void ES::Plugin::Scene::Resource::SceneManager::_loadScene(ES::Engine::Registry

void ES::Plugin::Scene::Resource::SceneManager::_unloadScene(ES::Engine::Registry &registry, const std::string &name)
{
ES::Utils::Log::Info("Unloading scene: " + _currentScene.value());
std::optional<std::shared_ptr<ES::Plugin::Scene::Utils::AScene>> scene = _getScene(name);
if (scene.has_value())
{
Expand All @@ -52,7 +51,7 @@ ES::Plugin::Scene::Resource::SceneManager::_getScene(const std::string &name)
}
else
{
std::cerr << "[ERROR] ES::Plugin::Scene::Resource::SceneManager: Scene not found: " << name << std::endl;
ES::Utils::Log::Error("Scene not found: " + name);
return std::nullopt;
}
}
5 changes: 3 additions & 2 deletions src/plugin/scene/src/resource/SceneManager.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "Engine.hpp"

#include "Logger.hpp"

#include "AScene.hpp"

namespace ES::Plugin::Scene::Resource {
Expand Down Expand Up @@ -43,8 +45,7 @@ class SceneManager {
"TScene must inherit from ES::Plugin::Scene::Utils::AScene");
if (_scenes.find(name) != _scenes.end())
{
std::cerr << "[WARNING] ES::Plugin::Scene::Resource::SceneManager: Scene " << name << " already exists"
<< std::endl;
ES::Utils::Log::Warn("Scene " + name + " already exists");
}
std::shared_ptr<TScene> new_scene = std::make_shared<TScene>();
_scenes[name] = new_scene;
Expand Down
22 changes: 12 additions & 10 deletions src/plugin/scene/tests/SceneTest.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "Engine.hpp"

#include "String.hpp"

#include "Scene.hpp"

using namespace ES::Plugin::Scene;
Comment thread
MasterLaplace marked this conversation as resolved.
Expand All @@ -19,20 +21,20 @@ class SceneTest : public Utils::AScene {
TEST(Scene, SceneManager)
{
ES::Engine::Registry registry;
registry.RegisterResource<ES::Plugin::Scene::Resource::SceneManager>(ES::Plugin::Scene::Resource::SceneManager());
registry.GetResource<ES::Plugin::Scene::Resource::SceneManager>().RegisterScene<SceneTest>("scene1");
registry.GetResource<ES::Plugin::Scene::Resource::SceneManager>().RegisterScene<SceneTest>("scene2");
registry.RegisterResource<Resource::SceneManager>(Resource::SceneManager());
registry.GetResource<Resource::SceneManager>().RegisterScene<SceneTest>("scene1");
registry.GetResource<Resource::SceneManager>().RegisterScene<SceneTest>("scene2");

registry.GetResource<ES::Plugin::Scene::Resource::SceneManager>().SetNextScene("scene1");
registry.GetResource<Resource::SceneManager>().SetNextScene("scene1");

testing::internal::CaptureStdout();
registry.RegisterSystem(ES::Plugin::Scene::System::UpdateScene);
registry.RegisterSystem(System::UpdateScene);
registry.RunSystems();

registry.GetResource<ES::Plugin::Scene::Resource::SceneManager>().SetNextScene("scene2");
registry.GetResource<Resource::SceneManager>().SetNextScene("scene2");
registry.RunSystems();
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(output, "[INFO] ES::Plugin::Scene::Resource::SceneManager: Loading scene: scene1\n"
"[INFO] ES::Plugin::Scene::Resource::SceneManager: Unloading scene: scene1\n"
"[INFO] ES::Plugin::Scene::Resource::SceneManager: Loading scene: scene2\n");
std::vector<std::string> output = ES::Plugin::Utils::String::Split(testing::internal::GetCapturedStdout(), '\n');
EXPECT_TRUE(ES::Plugin::Utils::String::EndsWith(output[0], "Loading scene: scene1"));
EXPECT_TRUE(ES::Plugin::Utils::String::EndsWith(output[1], "Unloading scene: scene1"));
EXPECT_TRUE(ES::Plugin::Utils::String::EndsWith(output[2], "Loading scene: scene2"));
}
10 changes: 7 additions & 3 deletions src/plugin/scene/xmake.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
add_rules("mode.debug", "mode.release")
add_requires("entt")
add_requires("entt", "spdlog")
add_requires("gtest", {optional = true})

includes("../../engine/xmake.lua")
includes("../../utils/log/xmake.lua")
includes("../../utils/string/xmake.lua")

target("PluginScene")
set_kind("static")
set_languages("cxx20")
set_policy("build.warning", true)
add_packages("entt")
add_packages("entt", "spdlog")

add_deps("EngineSquaredCore")
add_deps("UtilsLog")
add_deps("UtilsString")

add_files("src/**.cpp")
add_includedirs("src/", {public = true})
Expand All @@ -33,7 +37,7 @@ for _, file in ipairs(os.files("tests/**.cpp")) do
set_languages("cxx20")
add_links("gtest")
add_tests("default")
add_packages("glm", "entt", "gtest")
add_packages("glm", "entt", "gtest", "spdlog")

add_deps("PluginScene")
add_deps("EngineSquaredCore")
Expand Down
6 changes: 4 additions & 2 deletions src/plugin/utils/xmake.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
add_rules("mode.debug", "mode.release")

add_requires("gtest", {optional = true})

target("PluginUtils")
set_kind("static")
set_languages("cxx20")
set_policy("build.warning", true)

add_headerfiles("src/**.h", { public = true })
add_includedirs("src/", {public = true})
add_includedirs("src/", {public = true})


12 changes: 7 additions & 5 deletions src/plugin/vk-wrapper/src/VkWrapper.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "Logger.hpp"

#include "VkWrapper.hpp"

namespace ES::Plugin {
Expand Down Expand Up @@ -40,15 +42,15 @@ void VkWrapper::PrintAvailableExtensions()
std::vector<VkExtensionProperties> extensions(extensionCount);
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());

std::cout << "available extensions (" << extensionCount << "):" << std::endl;
Comment thread
Miou-zora marked this conversation as resolved.

std::string available_extensions = "available extensions (" + std::to_string(extensionCount) + "):";
for (const auto &extension : extensions)
std::cout << '\t' << extension.extensionName << std::endl;
available_extensions += "\t" + std::string(extension.extensionName);
ES::Utils::Log::Info(available_extensions);
}

void VkWrapper::PrintVersion() { std::cout << "VkWrapper version: " << VKWRAPPER_VERSION_STRING << std::endl; }
void VkWrapper::PrintVersion() { ES::Utils::Log::Info("VkWrapper version: " VKWRAPPER_VERSION_STRING); }

void VkWrapper::PrintConfig() { std::cout << "VkWrapper config:\n" << VKWRAPPER_CONFIG_STRING << std::endl; }
void VkWrapper::PrintConfig() { ES::Utils::Log::Info("VkWrapper config:\n" VKWRAPPER_CONFIG_STRING); }

void VkWrapper::ResizeCallback(GLFWwindow *window, int width, int height)
{
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ VKAPI_ATTR VkBool32 VKAPI_CALL DebugMessenger::Callback(VkDebugUtilsMessageSever
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
void *pUserData)
{
std::cerr << "validation layer: " << pCallbackData->pMessage << std::endl;

if (_severityMap.find(messageSeverity) != _severityMap.end())
ES::Utils::Log::Log(_severityMap[messageSeverity], "validation layer: " + std::string(pCallbackData->pMessage));
else
ES::Utils::Log::Log(ES::Utils::Log::Level::info, "validation layer: " + std::string(pCallbackData->pMessage));
return VK_FALSE;
}

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@

#include <iostream>
#include <stdexcept>
#include <unordered_map>
#include <vector>

#include "Logger.hpp"
#include "config.h"
#include "export.h"

Expand Down Expand Up @@ -128,6 +130,13 @@ class DebugMessenger {
private:
VkDebugUtilsMessengerEXT _debugMessenger = VK_NULL_HANDLE;
VkInstance _instance = VK_NULL_HANDLE;

inline static std::unordered_map<VkDebugUtilsMessageSeverityFlagBitsEXT, ES::Utils::Log::Level> _severityMap = {
{VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT, ES::Utils::Log::Level::info},
{VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT, ES::Utils::Log::Level::info},
{VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT, ES::Utils::Log::Level::warn},
{VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT, ES::Utils::Log::Level::err }
};
};

} // namespace ES::Plugin::Wrapper
Expand Down
6 changes: 4 additions & 2 deletions src/plugin/vk-wrapper/xmake.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
add_rules("mode.debug", "mode.release")
add_requires("vulkan-headers", "vulkansdk", "vulkan-hpp", "glfw")
add_requires("vulkan-headers", "vulkansdk", "vulkan-hpp", "glfw", "spdlog")

includes("../utils/xmake.lua")
includes("../../utils/log/xmake.lua")

target("PluginVkWrapper")
set_kind("static")
set_languages("cxx20")
add_packages("vulkan-headers", "vulkansdk", "vulkan-hpp", "glfw")
add_packages("vulkan-headers", "vulkansdk", "vulkan-hpp", "glfw", "spdlog")
set_policy("build.warning", true)

add_deps("PluginUtils")
add_deps("UtilsLog")

add_files("src/**.cpp")
add_includedirs("src", { public = true })
Expand Down
41 changes: 41 additions & 0 deletions src/utils/log/src/Logger.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include "spdlog/spdlog.h"

namespace ES::Utils::Log {
using Level = spdlog::level::level_enum;

template <typename T> inline void Debug(const T &msg) { spdlog::debug(msg); };

template <typename T> inline void Info(const T &msg) { spdlog::info(msg); };

template <typename T> inline void Warn(const T &msg) { spdlog::warn(msg); };

template <typename T> inline void Error(const T &msg) { spdlog::error(msg); };

template <typename T> inline void Critical(const T &msg) { spdlog::critical(msg); };

template <typename T> inline void Trace(const T &msg) { spdlog::trace(msg); };

template <typename T> inline void Log(Level level, const T &msg)
{
if (level == Level::info)
Log::Info(msg);
else if (level == Level::warn)
Log::Warn(msg);
else if (level == Level::err)
Log::Error(msg);
else if (level == Level::critical)
Log::Critical(msg);
else if (level == Level::debug)
Log::Debug(msg);
else
Log::Trace(msg);
};

inline void SetPattern(const std::string &pattern,
spdlog::pattern_time_type time_type = spdlog::pattern_time_type::local)
{
spdlog::set_pattern(pattern, time_type);
};
} // namespace ES::Utils::Log
10 changes: 10 additions & 0 deletions src/utils/log/xmake.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_rules("mode.debug", "mode.release")
add_requires("spdlog")
set_languages("cxx20")

target("UtilsLog")
set_kind("static")
add_packages("spdlog")

add_files("src/**.cpp")
add_includedirs("src/", {public = true})
20 changes: 20 additions & 0 deletions src/utils/string/src/String.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <sstream>

#include "String.hpp"

std::vector<std::string> ES::Plugin::Utils::String::Split(const std::string &str, const char &delimiter)
{
std::vector<std::string> result;
std::string token;
std::istringstream tokenStream(str);
while (std::getline(tokenStream, token, delimiter))
{
result.push_back(token);
}
return std::move(result);
}

bool ES::Plugin::Utils::String::EndsWith(const std::string &str, const std::string &suffix)
{
return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
}
22 changes: 22 additions & 0 deletions src/utils/string/src/String.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <string>
#include <vector>

namespace ES::Plugin::Utils::String {
/**
* @brief Split a string by a delimiter
*
* @param str The string to split
* @param delimiter The delimiter to split by
* @return std::vector<std::string> The split string
*/
std::vector<std::string> Split(const std::string &str, const char &delimiter);

/**
* @brief Check if a string ends with a suffix
*
* @param str The string to check
* @param suffix The suffix to check for
* @return bool True if the string ends with the suffix, false otherwise
*/
bool EndsWith(const std::string &str, const std::string &suffix);
} // namespace ES::Plugin::Utils::String
Loading

Back | FazBrowse Home | New Git URL