Archived
1
Fork 0

Move console and log namespaces to prism

This commit is contained in:
redstrate 2021-04-20 11:23:53 -04:00
parent 8370dfb6d6
commit bae6d05184
16 changed files with 120 additions and 122 deletions

View file

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

View file

@ -6,46 +6,47 @@
#include <variant>
#include <string>
namespace console {
enum class ArgType {
namespace prism::console {
enum class argument_type {
String,
Integer,
Boolean
};
struct ConsoleArgument {
ConsoleArgument(bool data) : data(data) {}
ConsoleArgument(int data) : data(data) {}
ArgType query_type() const {
if(std::holds_alternative<std::string>(data))
return ArgType::String;
else if(std::holds_alternative<int>(data))
return ArgType::Integer;
else if(std::holds_alternative<bool>(data))
return ArgType::Boolean;
struct console_argument {
explicit console_argument(bool data) : data(data) {}
explicit console_argument(int data) : data(data) {}
[[nodiscard]] argument_type query_type() const {
if (std::holds_alternative<std::string>(data))
return argument_type::String;
else if (std::holds_alternative<int>(data))
return argument_type::Integer;
else if (std::holds_alternative<bool>(data))
return argument_type::Boolean;
}
std::variant<std::string, int, bool> data;
};
struct Arguments {
std::vector<ConsoleArgument> arguments;
};
using FunctionPtr = std::function<void(console::Arguments)>;
struct ArgumentFormat {
ArgumentFormat(const int num_arguments) : num_arguments(num_arguments) {}
using arguments = std::vector<console_argument>;
using function_ptr = std::function<void(console::arguments)>;
struct argument_format {
explicit argument_format(const int num_arguments) : num_arguments(num_arguments) {}
int num_arguments = 0;
std::vector<ArgType> argument_types;
std::vector<argument_type> argument_types;
};
void register_command(const std::string_view name, const ArgumentFormat expected_format, const FunctionPtr function);
void invoke_command(const std::string_view name, const Arguments arguments);
void parse_and_invoke_command(const std::string_view command);
void register_variable(const std::string_view name, bool& variable);
}
void
register_command(std::string_view name, argument_format expected_format, function_ptr function);
void invoke_command(std::string_view name, arguments arguments);
void parse_and_invoke_command(std::string_view command);
void register_variable(std::string_view name, bool &variable);
}

View file

@ -7,41 +7,41 @@
#include "log.hpp"
#include "string_utils.hpp"
struct RegisteredCommand {
RegisteredCommand(const console::ArgumentFormat format, const console::FunctionPtr function) : expected_format(format), function(function) {}
console::ArgumentFormat expected_format;
std::function<void(console::Arguments)> function;
struct registered_command {
registered_command(const prism::console::argument_format format, const prism::console::function_ptr function) : expected_format(format), function(function) {}
prism::console::argument_format expected_format;
std::function<void(prism::console::arguments)> function;
};
static std::unordered_map<std::string, RegisteredCommand> registered_commands;
static std::unordered_map<std::string, registered_command> registered_commands;
struct RegisteredVariable {
RegisteredVariable(bool& data) : type(console::ArgType::Boolean), data(&data) {}
console::ArgType type;
RegisteredVariable(bool& data) : type(prism::console::argument_type::Boolean), data(&data) {}
prism::console::argument_type type;
std::variant<bool*> data;
};
static std::unordered_map<std::string, RegisteredVariable> registered_variables;
void console::register_command(const std::string_view name, const ArgumentFormat expected_format, const console::FunctionPtr function) {
registered_commands.try_emplace(name.data(), RegisteredCommand(expected_format, function));
void prism::console::register_command(const std::string_view name, const prism::console::argument_format expected_format, const prism::console::function_ptr function) {
registered_commands.try_emplace(name.data(), registered_command(expected_format, function));
}
void console::invoke_command(const std::string_view name, const Arguments arguments) {
void prism::console::invoke_command(const std::string_view name, const prism::console::arguments arguments) {
for(auto& [command_name, command_data] : registered_commands) {
if(command_name == name) {
bool invalid_format = false;
if(arguments.arguments.size() != command_data.expected_format.num_arguments)
if(arguments.size() != command_data.expected_format.num_arguments)
invalid_format = true;
if(invalid_format) {
console::info(System::Core, "Invalid command format!");
prism::log::info(System::Core, "Invalid command format!");
} else {
command_data.function(console::Arguments());
command_data.function(console::arguments());
}
return;
@ -52,18 +52,18 @@ void console::invoke_command(const std::string_view name, const Arguments argume
if(variable_name == name) {
bool invalid_format = false;
if(arguments.arguments.empty())
if(arguments.empty())
invalid_format = true;
if(arguments.arguments[0].query_type() != variable_data.type)
if(arguments[0].query_type() != variable_data.type)
invalid_format = true;
if(invalid_format) {
console::info(System::Core, "Wrong or empty variable type!");
prism::log::info(System::Core, "Wrong or empty variable type!");
} else {
auto argument = arguments.arguments[0];
auto argument = arguments[0];
switch(argument.query_type()) {
case console::ArgType::Boolean:
case console::argument_type::Boolean:
*std::get<bool*>(variable_data.data) = std::get<bool>(argument.data);
break;
}
@ -73,39 +73,36 @@ void console::invoke_command(const std::string_view name, const Arguments argume
}
}
console::info(System::Core, "{} is not the name of a valid command or variable!", name.data());
prism::log::info(System::Core, "{} is not the name of a valid command or variable!", name.data());
}
void console::parse_and_invoke_command(const std::string_view command) {
void prism::console::parse_and_invoke_command(const std::string_view command) {
const auto tokens = tokenize(command, " ");
if(tokens.empty())
return;
const auto try_parse_argument = [](std::string_view arg) -> std::optional<ConsoleArgument> {
const auto try_parse_argument = [](std::string_view arg) -> std::optional<console_argument> {
if(arg == "true") {
return ConsoleArgument(true);
return console_argument(true);
} else if(arg == "false") {
return ConsoleArgument(false);
return console_argument(false);
} else if(is_numeric(arg)) {
return ConsoleArgument(std::stoi(arg.data()));
return console_argument(std::stoi(arg.data()));
}
return std::nullopt;
};
std::vector<ConsoleArgument> arguments;
std::vector<console_argument> arguments;
for(int i = 1; i < tokens.size(); i++) {
if(auto arg = try_parse_argument(tokens[i]); arg)
arguments.push_back(*arg);
}
console::Arguments args;
args.arguments = arguments;
console::invoke_command(tokens[0], args);
console::invoke_command(tokens[0], arguments);
}
void console::register_variable(const std::string_view name, bool& variable) {
void prism::console::register_variable(const std::string_view name, bool& variable) {
registered_variables.try_emplace(name.data(), RegisteredVariable(variable));
}

View file

@ -27,15 +27,15 @@
using prism::engine;
engine::engine(const int argc, char* argv[]) {
console::info(System::Core, "Prism Engine loading...");
log::info(System::Core, "Prism Engine loading...");
console::register_command("test_cmd", console::ArgumentFormat(0), [](const console::Arguments&) {
console::info(System::Core, "Test cmd!");
console::register_command("test_cmd", console::argument_format(0), [](const console::arguments&) {
log::info(System::Core, "Test cmd!");
});
console::register_variable("rs_dynamic_resolution", render_options.dynamic_resolution);
console::register_command("quit", console::ArgumentFormat(0), [this](const console::Arguments&) {
console::register_command("quit", console::argument_format(0), [this](const console::arguments&) {
quit();
});
@ -134,7 +134,7 @@ Scene* engine::load_scene(const file::Path& path) {
auto file = file::open(path);
if(!file.has_value()) {
console::error(System::Core, "Failed to load scene from {}!", path);
prism::log::error(System::Core, "Failed to load scene from {}!", path);
return nullptr;
}
@ -227,7 +227,7 @@ Animation engine::load_animation(const file::Path& path) {
auto file = file::open(path, true);
if(!file.has_value()) {
console::error(System::Core, "Failed to load animation from {}!", path);
prism::log::error(System::Core, "Failed to load animation from {}!", path);
return {};
}
@ -287,7 +287,7 @@ void engine::load_cutscene(const file::Path& path) {
auto file = file::open(path);
if(!file.has_value()) {
console::error(System::Core, "Failed to load cutscene from {}!", path);
prism::log::error(System::Core, "Failed to load cutscene from {}!", path);
return;
}
@ -362,7 +362,7 @@ Object engine::add_prefab(Scene& scene, const file::Path& path, const std::strin
auto file = file::open(path);
if(!file.has_value()) {
console::error(System::Core, "Failed to load prefab from {}!", path);
prism::log::error(System::Core, "Failed to load prefab from {}!", path);
return NullObject;
}

View file

@ -21,7 +21,7 @@ std::optional<file::File> file::open(const file::Path path, const bool binary_mo
auto str = get_file_path(path).string();
FILE* file = fopen(str.c_str(), binary_mode ? "rb" : "r");
if(file == nullptr) {
console::error(System::File, "Failed to open file handle from {}!", str);
prism::log::error(System::File, "Failed to open file handle from {}!", str);
return {};
}

View file

@ -97,7 +97,7 @@ UIElement* ui::Screen::find_element(const std::string& id) {
ui::Screen::Screen(const file::Path path) {
auto file = file::open(path);
if(!file.has_value()) {
console::error(System::Core, "Failed to load UI from {}!", path);
prism::log::error(System::Core, "Failed to load UI from {}!", path);
return;
}

View file

@ -165,14 +165,14 @@ VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
void *pUserData) {
console::debug(System::GFX, pCallbackData->pMessage);
prism::log::debug(System::GFX, pCallbackData->pMessage);
return VK_FALSE;
}
VkResult name_object(VkDevice device, VkObjectType type, uint64_t object, std::string_view name) {
if(object == 0x0) {
console::error(System::GFX, "Failed to name object {}", name);
prism::log::error(System::GFX, "Failed to name object {}", name);
return VK_ERROR_DEVICE_LOST;
}
@ -537,7 +537,7 @@ void GFXVulkan::copy_texture(GFXTexture* texture, void* data, GFXSize size) {
}
void GFXVulkan::copy_texture(GFXTexture* from, GFXTexture* to) {
console::error(System::GFX, "Copy Texture->Texture unimplemented!");
prism::log::error(System::GFX, "Copy Texture->Texture unimplemented!");
}
void GFXVulkan::copy_texture(GFXTexture* from, GFXBuffer* to) {
@ -1688,7 +1688,7 @@ void GFXVulkan::resetDescriptorState() {
void GFXVulkan::cacheDescriptorState(GFXVulkanPipeline* pipeline, VkDescriptorSetLayout layout) {
uint64_t hash = getDescriptorHash(pipeline);
console::debug(System::GFX, "Caching descriptor hash {}", std::to_string(hash));
prism::log::debug(System::GFX, "Caching descriptor hash {}", std::to_string(hash));
vkDeviceWaitIdle(device);
@ -1703,7 +1703,7 @@ void GFXVulkan::cacheDescriptorState(GFXVulkanPipeline* pipeline, VkDescriptorSe
VkResult error = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet);
if(error == VK_ERROR_OUT_OF_POOL_MEMORY) {
console::error(System::GFX, "ERROR: COULD NOT CACHE BECAUSE OUT OF DESCRIPTOR SETS.");
prism::log::error(System::GFX, "ERROR: COULD NOT CACHE BECAUSE OUT OF DESCRIPTOR SETS.");
}
if(error != VK_SUCCESS)

View file

@ -20,45 +20,45 @@ enum class Level {
Debug
};
namespace console {
inline void internal_format(std::string& msg, const std::string& arg) {
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) {
inline void internal_format(std::string &msg, const char *&arg) {
auto pos = msg.find_first_of("{}");
msg.replace(pos, 2, arg);
}
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) {
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) {
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) {
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) {
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) {
void debug(const System system, const std::string_view format, Args &&... args) {
internal_print(Level::Debug, system, format, args...);
}

View file

@ -6,7 +6,7 @@
#include "string_utils.hpp"
#include "utility.hpp"
void console::process_message(const Level level, const System system, const std::string_view message) {
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);

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));
ImGui::GetIO().FontGlobalScale = 1.0 / platform::get_window_dpi(0);
} else {
console::error(System::Renderer, "Failed to load font file for imgui!");
prism::log::error(System::Renderer, "Failed to load font file for imgui!");
return;
}
}

View file

@ -13,7 +13,7 @@
ShaderSource get_shader(std::string filename, bool skinned, bool cubemap) {
auto shader_file = file::open(file::internal_domain / filename);
if(!shader_file.has_value()) {
console::error(System::Renderer, "Failed to open shader file {}!", filename);
prism::log::error(System::Renderer, "Failed to open shader file {}!", filename);
return {};
}

View file

@ -887,7 +887,7 @@ void Renderer::createPostPipelines() {
void Renderer::createFontTexture() {
auto file = file::open(file::app_domain / "font.fp", true);
if(file == std::nullopt) {
console::error(System::Renderer, "Failed to load font file!");
prism::log::error(System::Renderer, "Failed to load font file!");
return;
}

View file

@ -54,7 +54,7 @@ const std::vector<uint32_t> compile_glsl_to_spv(const std::string_view source_st
includer.pushExternalLocalDirectory(path);
if (!Shader.parse(&Resources, 100, false, messages, includer)) {
console::error(System::Renderer, "{}", Shader.getInfoLog());
prism::log::error(System::Renderer, "{}", Shader.getInfoLog());
return {};
}
@ -63,7 +63,7 @@ const std::vector<uint32_t> compile_glsl_to_spv(const std::string_view source_st
Program.addShader(&Shader);
if(!Program.link(messages)) {
console::error(System::None, "Failed to link shader: {} {} {}", source_string.data(), Shader.getInfoLog(), Shader.getInfoDebugLog());
prism::log::error(System::None, "Failed to link shader: {} {} {}", source_string.data(), Shader.getInfoLog(), Shader.getInfoDebugLog());
return {};
}
@ -78,7 +78,7 @@ const std::vector<uint32_t> compile_glsl_to_spv(const std::string_view source_st
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) {
console::error(System::Renderer, "Non-supported input language!");
prism::log::error(System::Renderer, "Non-supported input language!");
return std::nullopt;
}
@ -97,7 +97,7 @@ std::optional<ShaderSource> ShaderCompiler::compile(const ShaderLanguage from_la
auto spirv = compile_glsl_to_spv(shader_source.as_string(), lang, options);
if(spirv.empty()) {
console::error(System::Renderer, "SPIRV generation failed!");
prism::log::error(System::Renderer, "SPIRV generation failed!");
return std::nullopt;
}

View file

@ -7,7 +7,7 @@ namespace file {
using Path = std::filesystem::path;
}
namespace console {
namespace prism::log {
inline void internal_format(std::string& msg, const file::Path& arg) {
auto pos = msg.find_first_of("{}");
msg.replace(pos, 2, arg.string());

View file

@ -751,7 +751,7 @@ void CommonEditor::set_undo_stack(UndoStack *stack) {
bool mesh_readable(const file::Path path) {
auto file = file::open(path);
if(!file.has_value()) {
console::error(System::Renderer, "Failed to load mesh from {}!", path);
prism::log::error(System::Renderer, "Failed to load mesh from {}!", path);
return false;
}
@ -764,7 +764,7 @@ bool mesh_readable(const file::Path path) {
bool material_readable(const file::Path path) {
auto file = file::open(path);
if(!file.has_value()) {
console::error(System::Core, "Failed to load material from {}!", path);
prism::log::error(System::Core, "Failed to load material from {}!", path);
return false;
}
@ -1057,13 +1057,13 @@ void CommonEditor::drawConsole() {
ImGui::SameLine();
if(ImGui::Button("Run")) {
console::parse_and_invoke_command(command_buffer);
prism::console::parse_and_invoke_command(command_buffer);
command_buffer.clear();
}
ImGui::BeginChild("console_output", ImVec2(-1, -1), true);
for(const auto& message : console::stored_output)
for(const auto& message : prism::log::stored_output)
ImGui::TextWrapped("%s", message.c_str());
ImGui::EndChild();
@ -1140,7 +1140,7 @@ void CommonEditor::save_thumbnail_cache() {
FILE* file = fopen("thumbnail-cache", "wb");
if(file == nullptr) {
console::error(System::Core, "Failed to write thumbnail cache!");
prism::log::error(System::Core, "Failed to write thumbnail cache!");
return;
}

View file

@ -13,7 +13,7 @@ bool has_extension(const std::filesystem::path path, const std::string_view exte
int main(int argc, char* argv[]) {
if(argc < 2) {
console::error(System::Core, "Not enough arguments!");
prism::log::error(System::Core, "Not enough arguments!");
return -1;
}
@ -33,8 +33,8 @@ int main(int argc, char* argv[]) {
std::ofstream out(outname + ".glsl");
out << buffer.rdbuf();
console::info(System::Core, "Successfully written {} to {}.", source_path, destination_path);
prism::log::info(System::Core, "Successfully written {} to {}.", source_path, destination_path);
return 0;
} else {
@ -60,7 +60,7 @@ int main(int argc, char* argv[]) {
const auto compiled_source = shader_compiler.compile(ShaderLanguage::GLSL, stage, buffer.str(), language, options);
if(!compiled_source.has_value()) {
console::error(System::Core, "Error when compiling {}!", source_path);
prism::log::error(System::Core, "Error when compiling {}!", source_path);
return -1;
}
@ -82,8 +82,8 @@ int main(int argc, char* argv[]) {
default:
break;
}
console::info(System::Core, "Successfully written shader from {} to {}.", source_path, destination_path);
prism::log::info(System::Core, "Successfully written shader from {} to {}.", source_path, destination_path);
return 0;
}