From 529bc27702af30028657538a9d77b167eca76295 Mon Sep 17 00:00:00 2001 From: redstrate Date: Mon, 13 Sep 2021 23:41:54 -0400 Subject: [PATCH] Replace old logging and format system with fmt --- engine/asset/src/asset.cpp | 12 ++-- engine/core/src/console.cpp | 6 +- engine/core/src/engine.cpp | 12 ++-- engine/core/src/file.cpp | 2 +- engine/gfx/vulkan/src/gfx_vulkan.cpp | 10 +-- engine/log/CMakeLists.txt | 2 +- engine/log/include/log.hpp | 68 +++----------------- engine/log/src/log.cpp | 25 +------ engine/platform/include/file.hpp | 1 - engine/renderer/src/imguipass.cpp | 2 +- engine/renderer/src/materialcompiler.cpp | 2 +- engine/renderer/src/renderer.cpp | 2 +- engine/shadercompiler/src/shadercompiler.cpp | 8 +-- engine/utility/CMakeLists.txt | 1 - engine/utility/include/file_utils.hpp | 13 ---- engine/utility/include/string_utils.hpp | 17 ----- extern/CMakeLists.txt | 10 ++- tools/common/src/commoneditor.cpp | 14 ++-- tools/shadercompiler/main.cpp | 5 +- 19 files changed, 57 insertions(+), 155 deletions(-) delete mode 100644 engine/utility/include/file_utils.hpp diff --git a/engine/asset/src/asset.cpp b/engine/asset/src/asset.cpp index 8e5b094..17f9665 100644 --- a/engine/asset/src/asset.cpp +++ b/engine/asset/src/asset.cpp @@ -20,7 +20,7 @@ std::unique_ptr load_mesh(const prism::path path) { auto file = prism::open_file(path, true); if(!file.has_value()) { - prism::log::error(System::Renderer, "Failed to load mesh from {}!", path); + prism::log("Failed to load mesh from {}!", path.string()); return nullptr; } @@ -29,7 +29,7 @@ std::unique_ptr load_mesh(const prism::path path) { if(version == 5 || version == 6) { } else { - prism::log::error(System::Renderer, "{} failed the mesh version check! reported version = {}", path, std::to_string(version)); + prism::log("{} failed the mesh version check! reported version = {}", path.string(), version); return nullptr; } @@ -190,7 +190,7 @@ std::unique_ptr load_texture(const prism::path path) { auto file = prism::open_file(path, true); if(!file.has_value()) { - prism::log::error(System::Renderer, "Failed to load texture from {}!", path); + prism::log("Failed to load texture from {}!", path.string()); return nullptr; } @@ -202,7 +202,7 @@ std::unique_ptr load_texture(const prism::path path) { int width, height, channels; unsigned char* data = stbi_load_from_memory(file->cast_data(), file->size(), &width, &height, &channels, 4); if(!data) { - prism::log::error(System::Renderer, "Failed to load texture from {}!", path); + prism::log("Failed to load texture from {}!", path.string()); return nullptr; } @@ -247,7 +247,7 @@ std::unique_ptr load_material(const prism::path path) { auto file = prism::open_file(path); if(!file.has_value()) { - prism::log::error(System::Core, "Failed to load material from {}!", path); + prism::log("Failed to load material from {}!", path.string()); return {}; } @@ -258,7 +258,7 @@ std::unique_ptr load_material(const prism::path path) { mat->path = path.string(); if(!j.count("version") || j["version"] != 2) { - prism::log::error(System::Core, "Material {} failed the version check!", path); + prism::log("Material {} failed the version check!", path.string()); return mat; } diff --git a/engine/core/src/console.cpp b/engine/core/src/console.cpp index d93f507..36c666b 100644 --- a/engine/core/src/console.cpp +++ b/engine/core/src/console.cpp @@ -39,7 +39,7 @@ void prism::console::invoke_command(const std::string_view name, const prism::co invalid_format = true; if(invalid_format) { - prism::log::info(System::Core, "Invalid command format!"); + prism::log("Invalid command format!"); } else { command_data.function(console::arguments()); } @@ -59,7 +59,7 @@ void prism::console::invoke_command(const std::string_view name, const prism::co invalid_format = true; if(invalid_format) { - prism::log::info(System::Core, "Wrong or empty variable type!"); + prism::log("Wrong or empty variable type!"); } else { auto argument = arguments[0]; switch(argument.query_type()) { @@ -73,7 +73,7 @@ void prism::console::invoke_command(const std::string_view name, const prism::co } } - prism::log::info(System::Core, "{} is not the name of a valid command or variable!", name.data()); + prism::log("{} is not the name of a valid command or variable!", name.data()); } void prism::console::parse_and_invoke_command(const std::string_view command) { diff --git a/engine/core/src/engine.cpp b/engine/core/src/engine.cpp index 69f4e0d..dd5948e 100755 --- a/engine/core/src/engine.cpp +++ b/engine/core/src/engine.cpp @@ -26,10 +26,10 @@ using prism::engine; engine::engine(const int argc, char* argv[]) { - log::info(System::Core, "Prism Engine loading..."); + log("Prism Engine loading..."); console::register_command("test_cmd", console::argument_format(0), [](const console::arguments&) { - log::info(System::Core, "Test cmd!"); + log("Test cmd!"); }); console::register_variable("rs_dynamic_resolution", render_options.dynamic_resolution); @@ -123,7 +123,7 @@ Scene* engine::load_scene(const prism::path& path) { auto file = prism::open_file(path); if(!file.has_value()) { - prism::log::error(System::Core, "Failed to load scene from {}!", path); + prism::log("Failed to load scene from {}!", path.string()); return nullptr; } @@ -197,7 +197,7 @@ Animation engine::load_animation(const prism::path& path) { auto file = prism::open_file(path, true); if(!file.has_value()) { - prism::log::error(System::Core, "Failed to load animation from {}!", path); + prism::log("Failed to load animation from {}!", path.string()); return {}; } @@ -257,7 +257,7 @@ void engine::load_cutscene(const prism::path& path) { auto file = prism::open_file(path); if(!file.has_value()) { - prism::log::error(System::Core, "Failed to load cutscene from {}!", path); + prism::log("Failed to load cutscene from {}!", path.string()); return; } @@ -332,7 +332,7 @@ Object engine::add_prefab(Scene& scene, const prism::path& path, const std::stri auto file = prism::open_file(path); if(!file.has_value()) { - prism::log::error(System::Core, "Failed to load prefab from {}!", path); + prism::log("Failed to load prefab from {}!", path.string()); return NullObject; } diff --git a/engine/core/src/file.cpp b/engine/core/src/file.cpp index 1cd3a18..be770de 100755 --- a/engine/core/src/file.cpp +++ b/engine/core/src/file.cpp @@ -21,7 +21,7 @@ std::optional prism::open_file(const prism::path path, const bool b auto str = get_file_path(path).string(); FILE* file = fopen(str.c_str(), binary_mode ? "rb" : "r"); if(file == nullptr) { - prism::log::error(System::File, "Failed to open file handle from {}!", str); + prism::log("Failed to open file handle from {}!", str); return {}; } diff --git a/engine/gfx/vulkan/src/gfx_vulkan.cpp b/engine/gfx/vulkan/src/gfx_vulkan.cpp index bd12ce7..4887331 100755 --- a/engine/gfx/vulkan/src/gfx_vulkan.cpp +++ b/engine/gfx/vulkan/src/gfx_vulkan.cpp @@ -167,14 +167,14 @@ VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback( const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData) { - prism::log::debug(System::GFX, pCallbackData->pMessage); + prism::log("{}", pCallbackData->pMessage); return VK_FALSE; } VkResult name_object(VkDevice device, VkObjectType type, uint64_t object, std::string_view name) { if(object == 0x0) { - prism::log::error(System::GFX, "Failed to name object {}", name.data()); + prism::log("Failed to name object {}", name); return VK_ERROR_DEVICE_LOST; } @@ -580,7 +580,7 @@ void GFXVulkan::copy_texture(GFXTexture* texture, void* data, GFXSize size) { } void GFXVulkan::copy_texture(GFXTexture* from, GFXTexture* to) { - prism::log::error(System::GFX, "Copy Texture->Texture unimplemented!"); + prism::log("Copy Texture->Texture unimplemented!"); } void GFXVulkan::copy_texture(GFXTexture* from, GFXBuffer* to) { @@ -1641,7 +1641,7 @@ void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const int identifier) { } break; default: - prism::log::error(System::GFX, "Unhandled GFX Command Type {}", utility::enum_to_string(command.type)); + prism::log("Unhandled GFX Command Type {}", utility::enum_to_string(command.type)); break; } } @@ -2092,7 +2092,7 @@ void GFXVulkan::cacheDescriptorState(GFXVulkanPipeline* pipeline, VkDescriptorSe VkResult error = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet); if(error != VK_SUCCESS || descriptorSet == VK_NULL_HANDLE) { - prism::log::error(System::GFX, "ERROR: COULD NOT CACHE BECAUSE OUT OF DESCRIPTOR SETS."); + prism::log("ERROR: COULD NOT CACHE BECAUSE OUT OF DESCRIPTOR SETS."); return; } diff --git a/engine/log/CMakeLists.txt b/engine/log/CMakeLists.txt index ea0cffc..cd18263 100755 --- a/engine/log/CMakeLists.txt +++ b/engine/log/CMakeLists.txt @@ -4,5 +4,5 @@ set(SRC add_library(Log STATIC ${SRC}) target_include_directories(Log PUBLIC include) -target_link_libraries(Log PRIVATE Utility) +target_link_libraries(Log PUBLIC fmt::fmt) set_engine_properties(Log) diff --git a/engine/log/include/log.hpp b/engine/log/include/log.hpp index 9839e9e..6ceacce 100755 --- a/engine/log/include/log.hpp +++ b/engine/log/include/log.hpp @@ -1,66 +1,14 @@ #pragma once -#include -#include -#include +#include -enum class System { - None, - Core, - Renderer, - Game, - File, - GFX -}; - -enum class Level { - Info, - Warning, - Error, - Debug -}; - -namespace prism::log { - inline void internal_format(std::string &msg, const std::string &arg) { - auto pos = msg.find_first_of("{}"); - msg.replace(pos, 2, arg); +namespace prism { + inline void vlog(fmt::string_view format, fmt::format_args args) { + fmt::vprint(format, args); } - inline void internal_format(std::string &msg, const char *&arg) { - auto pos = msg.find_first_of("{}"); - msg.replace(pos, 2, arg); + template + inline void log(const S& format, Args&&... args) { + vlog(format, fmt::make_args_checked(format, args...)); } - - void process_message(const Level level, const System system, const std::string_view message); - - template - void internal_print(const Level level, const System system, const std::string_view format, Args &&... args) { - auto msg = std::string(format); - - ((internal_format(msg, args)), ...); - - process_message(level, system, msg); - } - - template - void info(const System system, const std::string_view format, Args &&... args) { - internal_print(Level::Info, system, format, args...); - } - - template - void warning(const System system, const std::string_view format, Args &&... args) { - internal_print(Level::Warning, system, format, args...); - } - - template - void error(const System system, const std::string_view format, Args &&... args) { - internal_print(Level::Error, system, format, args...); - } - - template - void debug(const System system, const std::string_view format, Args &&... args) { - internal_print(Level::Debug, system, format, args...); - } - - inline std::vector stored_output; -} +} \ No newline at end of file diff --git a/engine/log/src/log.cpp b/engine/log/src/log.cpp index 490d183..dfcfffa 100755 --- a/engine/log/src/log.cpp +++ b/engine/log/src/log.cpp @@ -1,24 +1 @@ -#include "log.hpp" - -#include -#include -#include -#include "string_utils.hpp" -#include "utility.hpp" - -void prism::log::process_message(const Level level, const System system, const std::string_view message) { - auto now = std::chrono::system_clock::now(); - std::time_t t_c = std::chrono::system_clock::to_time_t(now); - - std::string date; - date.resize(30); - - std::strftime(&date[0], date.size(), "%Y-%m-%d %H:%M:%S", std::localtime(&t_c)); - utility::erase(date, '\0'); // strftime will insert \0 for us, but it's not needed here - - std::string s = utility::format("{} {} {}: {}", date, utility::enum_to_string(system), utility::enum_to_string(level), message); - - std::cout << s << '\n'; - - stored_output.push_back(s); -} +#include "log.hpp" \ No newline at end of file diff --git a/engine/platform/include/file.hpp b/engine/platform/include/file.hpp index 439429c..03596ac 100755 --- a/engine/platform/include/file.hpp +++ b/engine/platform/include/file.hpp @@ -8,7 +8,6 @@ #include #include "log.hpp" -#include "file_utils.hpp" #include "path.hpp" namespace prism { diff --git a/engine/renderer/src/imguipass.cpp b/engine/renderer/src/imguipass.cpp index 8b2aaa6..e9f31d8 100755 --- a/engine/renderer/src/imguipass.cpp +++ b/engine/renderer/src/imguipass.cpp @@ -162,7 +162,7 @@ void ImGuiPass::load_font(const std::string_view filename) { io.Fonts->AddFontFromMemoryTTF(font_file->cast_data(), font_file->size(), 15.0 * platform::get_window_dpi(0)); ImGui::GetIO().FontGlobalScale = 1.0 / platform::get_window_dpi(0); } else { - prism::log::error(System::Renderer, "Failed to load font file for imgui!"); + prism::log("Failed to load font file for imgui!"); return; } } diff --git a/engine/renderer/src/materialcompiler.cpp b/engine/renderer/src/materialcompiler.cpp index ac3aa83..3108f4c 100755 --- a/engine/renderer/src/materialcompiler.cpp +++ b/engine/renderer/src/materialcompiler.cpp @@ -13,7 +13,7 @@ ShaderSource get_shader(std::string filename, bool skinned, bool cubemap) { auto shader_file = prism::open_file(prism::internal_domain / filename); if(!shader_file.has_value()) { - prism::log::error(System::Renderer, "Failed to open shader file {}!", filename); + prism::log("Failed to open shader file {}!", filename); return {}; } diff --git a/engine/renderer/src/renderer.cpp b/engine/renderer/src/renderer.cpp index 0f1660c..fa2440f 100755 --- a/engine/renderer/src/renderer.cpp +++ b/engine/renderer/src/renderer.cpp @@ -678,7 +678,7 @@ void renderer::create_post_pipelines() { void renderer::create_font_texture() { auto file = prism::open_file(prism::app_domain / "font.fp", true); if(file == std::nullopt) { - prism::log::error(System::Renderer, "Failed to load font file!"); + prism::log("Failed to load font file!"); return; } diff --git a/engine/shadercompiler/src/shadercompiler.cpp b/engine/shadercompiler/src/shadercompiler.cpp index 501c328..b5c6f4f 100755 --- a/engine/shadercompiler/src/shadercompiler.cpp +++ b/engine/shadercompiler/src/shadercompiler.cpp @@ -53,7 +53,7 @@ std::vector compile_glsl_to_spv(const std::string_view source_string, file_includer.pushExternalLocalDirectory(path); if (!shader.parse(&DefaultTBuiltInResource, 100, false, EShMsgDefault, file_includer)) { - prism::log::error(System::Renderer, "{}", shader.getInfoLog()); + prism::log("{}", shader.getInfoLog()); return {}; } @@ -62,7 +62,7 @@ std::vector compile_glsl_to_spv(const std::string_view source_string, Program.addShader(&shader); if(!Program.link(EShMsgDefault)) { - prism::log::error(System::None, "Failed to link shader: {} {} {}", source_string.data(), shader.getInfoLog(), shader.getInfoDebugLog()); + prism::log("Failed to link shader: {} {} {}", source_string.data(), shader.getInfoLog(), shader.getInfoDebugLog()); return {}; } @@ -77,7 +77,7 @@ std::vector compile_glsl_to_spv(const std::string_view source_string, std::optional ShaderCompiler::compile(const ShaderLanguage from_language, const ShaderStage shader_stage, const ShaderSource& shader_source, const ShaderLanguage to_language, const CompileOptions& options) { if(from_language != ShaderLanguage::GLSL) { - prism::log::error(System::Renderer, "Non-supported input language!"); + prism::log("Non-supported input language!"); return std::nullopt; } @@ -96,7 +96,7 @@ std::optional ShaderCompiler::compile(const ShaderLanguage from_la auto spirv = compile_glsl_to_spv(shader_source.as_string(), lang, options); if(spirv.empty()) { - prism::log::error(System::Renderer, "SPIRV generation failed!"); + prism::log("SPIRV generation failed!"); return std::nullopt; } diff --git a/engine/utility/CMakeLists.txt b/engine/utility/CMakeLists.txt index 64efa70..d0bd632 100755 --- a/engine/utility/CMakeLists.txt +++ b/engine/utility/CMakeLists.txt @@ -4,7 +4,6 @@ set(SRC include/string_utils.hpp include/timer.hpp include/common.hpp - include/file_utils.hpp include/assertions.hpp include/path.hpp diff --git a/engine/utility/include/file_utils.hpp b/engine/utility/include/file_utils.hpp deleted file mode 100644 index ce22941..0000000 --- a/engine/utility/include/file_utils.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include -#include - -#include "path.hpp" - -namespace prism::log { - inline void internal_format(std::string& msg, const prism::path& arg) { - auto pos = msg.find_first_of("{}"); - msg.replace(pos, 2, arg.string()); - } -} \ No newline at end of file diff --git a/engine/utility/include/string_utils.hpp b/engine/utility/include/string_utils.hpp index 0a9bd2c..ae8dd0b 100755 --- a/engine/utility/include/string_utils.hpp +++ b/engine/utility/include/string_utils.hpp @@ -12,20 +12,3 @@ bool string_starts_with(const std::string_view haystack, const std::string_view bool is_numeric(const std::string_view string); std::vector tokenize(const std::string_view string, const std::string_view& delimiters = ","); - -namespace utility { - template - inline void format_internal_format(std::string& msg, const Arg& arg) { - auto pos = msg.find_first_of("{}"); - msg.replace(pos, 2, arg); - } - - template - std::string format(const std::string_view format, Args&&... args) { - auto msg = std::string(format); - - ((format_internal_format(msg, args)), ...); - - return msg; - } -} diff --git a/extern/CMakeLists.txt b/extern/CMakeLists.txt index f222971..4c9a912 100755 --- a/extern/CMakeLists.txt +++ b/extern/CMakeLists.txt @@ -106,4 +106,12 @@ if(BUILD_TOOLS) ) FetchContent_MakeAvailable(assimp) -endif() \ No newline at end of file +endif() + +FetchContent_Declare( + fmt + GIT_REPOSITORY https://github.com/fmtlib/fmt + GIT_TAG 8.0.1 +) + +FetchContent_MakeAvailable(fmt) \ No newline at end of file diff --git a/tools/common/src/commoneditor.cpp b/tools/common/src/commoneditor.cpp index 07e52bd..0838449 100755 --- a/tools/common/src/commoneditor.cpp +++ b/tools/common/src/commoneditor.cpp @@ -703,7 +703,7 @@ void CommonEditor::set_undo_stack(UndoStack *stack) { bool mesh_readable(const prism::path path) { auto file = prism::open_file(path); if(!file.has_value()) { - prism::log::error(System::Renderer, "Failed to load mesh from {}!", path); + prism::log("Failed to load mesh from {}!", path.string()); return false; } @@ -716,7 +716,7 @@ bool mesh_readable(const prism::path path) { bool material_readable(const prism::path path) { auto file = prism::open_file(path); if(!file.has_value()) { - prism::log::error(System::Core, "Failed to load material from {}!", path); + prism::log("Failed to load material from {}!", path.string()); return false; } @@ -1014,9 +1014,11 @@ void CommonEditor::drawConsole() { } ImGui::BeginChild("console_output", ImVec2(-1, -1), true); - - for(const auto& message : prism::log::stored_output) - ImGui::TextWrapped("%s", message.c_str()); + + // TODO: implement for new log system + //for(const auto& message : prism::log::stored_output) + // ImGui::TextWrapped("%s", message.c_str()); + ImGui::Text("unimplemented :-("); ImGui::EndChild(); } @@ -1092,7 +1094,7 @@ void CommonEditor::save_thumbnail_cache() { FILE* file = fopen("thumbnail-cache", "wb"); if(file == nullptr) { - prism::log::error(System::Core, "Failed to write thumbnail cache!"); + prism::log("Failed to write thumbnail cache!"); return; } diff --git a/tools/shadercompiler/main.cpp b/tools/shadercompiler/main.cpp index 4ce5bdd..73bc23f 100755 --- a/tools/shadercompiler/main.cpp +++ b/tools/shadercompiler/main.cpp @@ -5,7 +5,6 @@ #include "shadercompiler.hpp" #include "log.hpp" #include "string_utils.hpp" -#include "file_utils.hpp" bool has_extension(const std::filesystem::path path, const std::string_view extension) { return string_contains(path.filename().string(), extension); @@ -13,7 +12,7 @@ bool has_extension(const std::filesystem::path path, const std::string_view exte int main(int argc, char* argv[]) { if(argc < 2) { - prism::log::error(System::Core, "Not enough arguments!"); + prism::log("Not enough arguments!"); return -1; } @@ -46,7 +45,7 @@ int main(int argc, char* argv[]) { const auto compiled_source = shader_compiler.compile(ShaderLanguage::GLSL, stage, ShaderSource(buffer.str()), language, options); if(!compiled_source.has_value()) { - prism::log::error(System::Core, "Error when compiling {}!", source_path); + prism::log("Error when compiling {}!", source_path.string()); return -1; }