Archived
1
Fork 0

Replace old logging and format system with fmt

This commit is contained in:
redstrate 2021-09-13 23:41:54 -04:00
parent 626e314ec2
commit 529bc27702
19 changed files with 57 additions and 155 deletions

View file

@ -20,7 +20,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path path) {
auto file = prism::open_file(path, true); auto file = prism::open_file(path, true);
if(!file.has_value()) { 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; return nullptr;
} }
@ -29,7 +29,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path path) {
if(version == 5 || version == 6) { if(version == 5 || version == 6) {
} else { } 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; return nullptr;
} }
@ -190,7 +190,7 @@ std::unique_ptr<Texture> load_texture(const prism::path path) {
auto file = prism::open_file(path, true); auto file = prism::open_file(path, true);
if(!file.has_value()) { 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; return nullptr;
} }
@ -202,7 +202,7 @@ std::unique_ptr<Texture> load_texture(const prism::path path) {
int width, height, channels; int width, height, channels;
unsigned char* data = stbi_load_from_memory(file->cast_data<unsigned char>(), file->size(), &width, &height, &channels, 4); unsigned char* data = stbi_load_from_memory(file->cast_data<unsigned char>(), file->size(), &width, &height, &channels, 4);
if(!data) { if(!data) {
prism::log::error(System::Renderer, "Failed to load texture from {}!", path); prism::log("Failed to load texture from {}!", path.string());
return nullptr; return nullptr;
} }
@ -247,7 +247,7 @@ std::unique_ptr<Material> load_material(const prism::path path) {
auto file = prism::open_file(path); auto file = prism::open_file(path);
if(!file.has_value()) { 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 {}; return {};
} }
@ -258,7 +258,7 @@ std::unique_ptr<Material> load_material(const prism::path path) {
mat->path = path.string(); mat->path = path.string();
if(!j.count("version") || j["version"] != 2) { 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; return mat;
} }

View file

@ -39,7 +39,7 @@ void prism::console::invoke_command(const std::string_view name, const prism::co
invalid_format = true; invalid_format = true;
if(invalid_format) { if(invalid_format) {
prism::log::info(System::Core, "Invalid command format!"); prism::log("Invalid command format!");
} else { } else {
command_data.function(console::arguments()); 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; invalid_format = true;
if(invalid_format) { if(invalid_format) {
prism::log::info(System::Core, "Wrong or empty variable type!"); prism::log("Wrong or empty variable type!");
} else { } else {
auto argument = arguments[0]; auto argument = arguments[0];
switch(argument.query_type()) { 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) { void prism::console::parse_and_invoke_command(const std::string_view command) {

View file

@ -26,10 +26,10 @@
using prism::engine; using prism::engine;
engine::engine(const int argc, char* argv[]) { 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&) { 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); 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); auto file = prism::open_file(path);
if(!file.has_value()) { 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; return nullptr;
} }
@ -197,7 +197,7 @@ Animation engine::load_animation(const prism::path& path) {
auto file = prism::open_file(path, true); auto file = prism::open_file(path, true);
if(!file.has_value()) { 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 {}; return {};
} }
@ -257,7 +257,7 @@ void engine::load_cutscene(const prism::path& path) {
auto file = prism::open_file(path); auto file = prism::open_file(path);
if(!file.has_value()) { 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; return;
} }
@ -332,7 +332,7 @@ Object engine::add_prefab(Scene& scene, const prism::path& path, const std::stri
auto file = prism::open_file(path); auto file = prism::open_file(path);
if(!file.has_value()) { 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; return NullObject;
} }

View file

@ -21,7 +21,7 @@ std::optional<prism::file> prism::open_file(const prism::path path, const bool b
auto str = get_file_path(path).string(); auto str = get_file_path(path).string();
FILE* file = fopen(str.c_str(), binary_mode ? "rb" : "r"); FILE* file = fopen(str.c_str(), binary_mode ? "rb" : "r");
if(file == nullptr) { 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 {}; return {};
} }

View file

@ -167,14 +167,14 @@ VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
void *pUserData) { void *pUserData) {
prism::log::debug(System::GFX, pCallbackData->pMessage); prism::log("{}", pCallbackData->pMessage);
return VK_FALSE; return VK_FALSE;
} }
VkResult name_object(VkDevice device, VkObjectType type, uint64_t object, std::string_view name) { VkResult name_object(VkDevice device, VkObjectType type, uint64_t object, std::string_view name) {
if(object == 0x0) { 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; 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) { 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) { void GFXVulkan::copy_texture(GFXTexture* from, GFXBuffer* to) {
@ -1641,7 +1641,7 @@ void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const int identifier) {
} }
break; break;
default: 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; break;
} }
} }
@ -2092,7 +2092,7 @@ void GFXVulkan::cacheDescriptorState(GFXVulkanPipeline* pipeline, VkDescriptorSe
VkResult error = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet); VkResult error = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet);
if(error != VK_SUCCESS || descriptorSet == VK_NULL_HANDLE) { 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; return;
} }

View file

@ -4,5 +4,5 @@ set(SRC
add_library(Log STATIC ${SRC}) add_library(Log STATIC ${SRC})
target_include_directories(Log PUBLIC include) target_include_directories(Log PUBLIC include)
target_link_libraries(Log PRIVATE Utility) target_link_libraries(Log PUBLIC fmt::fmt)
set_engine_properties(Log) set_engine_properties(Log)

View file

@ -1,66 +1,14 @@
#pragma once #pragma once
#include <string_view> #include <fmt/format.h>
#include <string>
#include <vector>
enum class System { namespace prism {
None, inline void vlog(fmt::string_view format, fmt::format_args args) {
Core, fmt::vprint(format, args);
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);
} }
inline void internal_format(std::string &msg, const char *&arg) { template <typename S, typename... Args>
auto pos = msg.find_first_of("{}"); inline void log(const S& format, Args&&... args) {
msg.replace(pos, 2, arg); vlog(format, fmt::make_args_checked<Args...>(format, args...));
} }
}
void process_message(const Level level, const System system, const std::string_view message);
template<typename... Args>
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<typename... Args>
void info(const System system, const std::string_view format, Args &&... args) {
internal_print(Level::Info, system, format, args...);
}
template<typename... Args>
void warning(const System system, const std::string_view format, Args &&... args) {
internal_print(Level::Warning, system, format, args...);
}
template<typename... Args>
void error(const System system, const std::string_view format, Args &&... args) {
internal_print(Level::Error, system, format, args...);
}
template<typename... Args>
void debug(const System system, const std::string_view format, Args &&... args) {
internal_print(Level::Debug, system, format, args...);
}
inline std::vector<std::string> stored_output;
}

View file

@ -1,24 +1 @@
#include "log.hpp" #include "log.hpp"
#include <iostream>
#include <chrono>
#include <iomanip>
#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);
}

View file

@ -8,7 +8,6 @@
#include <filesystem> #include <filesystem>
#include "log.hpp" #include "log.hpp"
#include "file_utils.hpp"
#include "path.hpp" #include "path.hpp"
namespace prism { namespace prism {

View file

@ -162,7 +162,7 @@ void ImGuiPass::load_font(const std::string_view filename) {
io.Fonts->AddFontFromMemoryTTF(font_file->cast_data<unsigned char>(), font_file->size(), 15.0 * platform::get_window_dpi(0)); io.Fonts->AddFontFromMemoryTTF(font_file->cast_data<unsigned char>(), font_file->size(), 15.0 * platform::get_window_dpi(0));
ImGui::GetIO().FontGlobalScale = 1.0 / platform::get_window_dpi(0); ImGui::GetIO().FontGlobalScale = 1.0 / platform::get_window_dpi(0);
} else { } else {
prism::log::error(System::Renderer, "Failed to load font file for imgui!"); prism::log("Failed to load font file for imgui!");
return; return;
} }
} }

View file

@ -13,7 +13,7 @@
ShaderSource get_shader(std::string filename, bool skinned, bool cubemap) { ShaderSource get_shader(std::string filename, bool skinned, bool cubemap) {
auto shader_file = prism::open_file(prism::internal_domain / filename); auto shader_file = prism::open_file(prism::internal_domain / filename);
if(!shader_file.has_value()) { 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 {}; return {};
} }

View file

@ -678,7 +678,7 @@ void renderer::create_post_pipelines() {
void renderer::create_font_texture() { void renderer::create_font_texture() {
auto file = prism::open_file(prism::app_domain / "font.fp", true); auto file = prism::open_file(prism::app_domain / "font.fp", true);
if(file == std::nullopt) { if(file == std::nullopt) {
prism::log::error(System::Renderer, "Failed to load font file!"); prism::log("Failed to load font file!");
return; return;
} }

View file

@ -53,7 +53,7 @@ std::vector<uint32_t> compile_glsl_to_spv(const std::string_view source_string,
file_includer.pushExternalLocalDirectory(path); file_includer.pushExternalLocalDirectory(path);
if (!shader.parse(&DefaultTBuiltInResource, 100, false, EShMsgDefault, file_includer)) { if (!shader.parse(&DefaultTBuiltInResource, 100, false, EShMsgDefault, file_includer)) {
prism::log::error(System::Renderer, "{}", shader.getInfoLog()); prism::log("{}", shader.getInfoLog());
return {}; return {};
} }
@ -62,7 +62,7 @@ std::vector<uint32_t> compile_glsl_to_spv(const std::string_view source_string,
Program.addShader(&shader); Program.addShader(&shader);
if(!Program.link(EShMsgDefault)) { 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 {}; return {};
} }
@ -77,7 +77,7 @@ std::vector<uint32_t> compile_glsl_to_spv(const std::string_view source_string,
std::optional<ShaderSource> ShaderCompiler::compile(const ShaderLanguage from_language, const ShaderStage shader_stage, const ShaderSource& shader_source, const ShaderLanguage to_language, const CompileOptions& options) { std::optional<ShaderSource> 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) { if(from_language != ShaderLanguage::GLSL) {
prism::log::error(System::Renderer, "Non-supported input language!"); prism::log("Non-supported input language!");
return std::nullopt; return std::nullopt;
} }
@ -96,7 +96,7 @@ std::optional<ShaderSource> ShaderCompiler::compile(const ShaderLanguage from_la
auto spirv = compile_glsl_to_spv(shader_source.as_string(), lang, options); auto spirv = compile_glsl_to_spv(shader_source.as_string(), lang, options);
if(spirv.empty()) { if(spirv.empty()) {
prism::log::error(System::Renderer, "SPIRV generation failed!"); prism::log("SPIRV generation failed!");
return std::nullopt; return std::nullopt;
} }

View file

@ -4,7 +4,6 @@ set(SRC
include/string_utils.hpp include/string_utils.hpp
include/timer.hpp include/timer.hpp
include/common.hpp include/common.hpp
include/file_utils.hpp
include/assertions.hpp include/assertions.hpp
include/path.hpp include/path.hpp

View file

@ -1,13 +0,0 @@
#pragma once
#include <string>
#include <filesystem>
#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());
}
}

View file

@ -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); bool is_numeric(const std::string_view string);
std::vector<std::string> tokenize(const std::string_view string, const std::string_view& delimiters = ","); std::vector<std::string> tokenize(const std::string_view string, const std::string_view& delimiters = ",");
namespace utility {
template<class Arg>
inline void format_internal_format(std::string& msg, const Arg& arg) {
auto pos = msg.find_first_of("{}");
msg.replace(pos, 2, arg);
}
template<class... Args>
std::string format(const std::string_view format, Args&&... args) {
auto msg = std::string(format);
((format_internal_format<Args>(msg, args)), ...);
return msg;
}
}

10
extern/CMakeLists.txt vendored
View file

@ -106,4 +106,12 @@ if(BUILD_TOOLS)
) )
FetchContent_MakeAvailable(assimp) FetchContent_MakeAvailable(assimp)
endif() endif()
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt
GIT_TAG 8.0.1
)
FetchContent_MakeAvailable(fmt)

View file

@ -703,7 +703,7 @@ void CommonEditor::set_undo_stack(UndoStack *stack) {
bool mesh_readable(const prism::path path) { bool mesh_readable(const prism::path path) {
auto file = prism::open_file(path); auto file = prism::open_file(path);
if(!file.has_value()) { 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; return false;
} }
@ -716,7 +716,7 @@ bool mesh_readable(const prism::path path) {
bool material_readable(const prism::path path) { bool material_readable(const prism::path path) {
auto file = prism::open_file(path); auto file = prism::open_file(path);
if(!file.has_value()) { 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; return false;
} }
@ -1014,9 +1014,11 @@ void CommonEditor::drawConsole() {
} }
ImGui::BeginChild("console_output", ImVec2(-1, -1), true); ImGui::BeginChild("console_output", ImVec2(-1, -1), true);
for(const auto& message : prism::log::stored_output) // TODO: implement for new log system
ImGui::TextWrapped("%s", message.c_str()); //for(const auto& message : prism::log::stored_output)
// ImGui::TextWrapped("%s", message.c_str());
ImGui::Text("unimplemented :-(");
ImGui::EndChild(); ImGui::EndChild();
} }
@ -1092,7 +1094,7 @@ void CommonEditor::save_thumbnail_cache() {
FILE* file = fopen("thumbnail-cache", "wb"); FILE* file = fopen("thumbnail-cache", "wb");
if(file == nullptr) { if(file == nullptr) {
prism::log::error(System::Core, "Failed to write thumbnail cache!"); prism::log("Failed to write thumbnail cache!");
return; return;
} }

View file

@ -5,7 +5,6 @@
#include "shadercompiler.hpp" #include "shadercompiler.hpp"
#include "log.hpp" #include "log.hpp"
#include "string_utils.hpp" #include "string_utils.hpp"
#include "file_utils.hpp"
bool has_extension(const std::filesystem::path path, const std::string_view extension) { bool has_extension(const std::filesystem::path path, const std::string_view extension) {
return string_contains(path.filename().string(), 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[]) { int main(int argc, char* argv[]) {
if(argc < 2) { if(argc < 2) {
prism::log::error(System::Core, "Not enough arguments!"); prism::log("Not enough arguments!");
return -1; 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); const auto compiled_source = shader_compiler.compile(ShaderLanguage::GLSL, stage, ShaderSource(buffer.str()), language, options);
if(!compiled_source.has_value()) { 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; return -1;
} }