diff options
author | Frederico Linhares <fred@linhares.blue> | 2022-08-03 15:25:29 -0300 |
---|---|---|
committer | Frederico Linhares <fred@linhares.blue> | 2022-08-03 15:25:29 -0300 |
commit | 60b38f632e4d6e9416630de6120dafcac613a698 (patch) | |
tree | 458c35df273166b68a3fd26095226b88bba9371e | |
parent | f88712a929ee3543f8e1d45c6071f676df339cdb (diff) |
feat Create a log system
-rw-r--r-- | src/candy_gear.cpp | 29 | ||||
-rw-r--r-- | src/core.cpp | 77 | ||||
-rw-r--r-- | src/core.hpp | 9 | ||||
-rw-r--r-- | src/log.cpp | 43 | ||||
-rw-r--r-- | src/log.hpp | 35 | ||||
-rw-r--r-- | src/main.cpp | 67 | ||||
-rw-r--r-- | src/vk/device.cpp | 18 | ||||
-rw-r--r-- | src/vk/queue_family.cpp | 16 | ||||
-rw-r--r-- | src/vk/swapchain.cpp | 1 |
9 files changed, 202 insertions, 93 deletions
diff --git a/src/candy_gear.cpp b/src/candy_gear.cpp index b10ed6e..2575e83 100644 --- a/src/candy_gear.cpp +++ b/src/candy_gear.cpp @@ -69,6 +69,33 @@ cg_mCandyGear_load_yaml(mrb_state *mrb, mrb_value self) } static mrb_value +cg_mCandyGear_log(mrb_state *mrb, mrb_value self) +{ + const char *message; + mrb_sym sym_log_level; + Log::Level log_lvl; + + mrb_get_args(mrb, "nz", &sym_log_level, &message); + + if(sym_log_level == cg_core.sym_trace) + log_lvl = Log::Level::Trace; + else if(sym_log_level == cg_core.sym_debug) + log_lvl = Log::Level::Debug; + else if(sym_log_level == cg_core.sym_information) + log_lvl = Log::Level::Information; + else if(sym_log_level == cg_core.sym_warning) + log_lvl = Log::Level::Warning; + else if(sym_log_level == cg_core.sym_error) + log_lvl = Log::Level::Error; + else + log_lvl = Log::Level::Fatal; + + cg_core.log.message(log_lvl, message); + + return self; +} + +static mrb_value cg_mCandyGear_quit(mrb_state *mrb, mrb_value self) { cg_core.quit_game = true; @@ -85,5 +112,7 @@ cg_candy_gear_init(mrb_state *mrb) mrb_define_class_method( mrb, cg_m, "load_yaml", cg_mCandyGear_load_yaml, MRB_ARGS_REQ(1)); mrb_define_class_method( + mrb, cg_m, "log", cg_mCandyGear_log, MRB_ARGS_REQ(2)); + mrb_define_class_method( mrb, cg_m, "quit", cg_mCandyGear_quit, MRB_ARGS_NONE()); } diff --git a/src/core.cpp b/src/core.cpp index 7e13f75..30acbf9 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -16,7 +16,9 @@ #include "core.hpp" -#include <iostream> +#ifdef DEBUG +#include <sstream> +#endif #include <yaml-cpp/yaml.h> @@ -31,21 +33,19 @@ vk_debug_callback( const VkDebugUtilsMessengerCallbackDataEXT* callback_data, void* _obj) { - // Print message severy code. - std::cout << "[\e[1;0mVK\e[0;0m "; + // Set level. + Log::Level log_level; if(message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) - std::cout << "\e[1;32mV\e[0;0m"; + log_level = Log::Level::Trace; else if(message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) - std::cout << "\e[1;34mI\e[0;0m"; + log_level = Log::Level::Information; else if(message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) - std::cout << "\e[1;33mW\e[0;0m"; + log_level = Log::Level::Warning; else if(message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) - std::cout << "\e[1;31mE\e[0;0m"; - - std::cout << "]"; + log_level = Log::Level::Error; // Log message. - std::cout << callback_data->pMessage << std::endl; + cg_core.log.message(log_level, callback_data->pMessage); return VK_FALSE; } @@ -226,9 +226,13 @@ load_vk_instance(void *obj) } #ifdef DEBUG - std::cout << "Enabled VK extensions." << std::endl; + cg_core.log.message(Log::Level::Trace, "Enabled VK extensions."); for(auto vk_extension: vk_extensions) - std::cout << "Extension name: " << vk_extension << std::endl; + { + std::string message{"Extension name: "}; + message += vk_extension; + cg_core.log.message(Log::Level::Trace, message); + } #endif } @@ -244,19 +248,20 @@ load_vk_instance(void *obj) vk_available_layers.data()); vk_available_layers_names.resize(vk_available_layers_count); #ifdef DEBUG - std::cout << "Available VK instance layers." << std::endl; + cg_core.log.message(Log::Level::Trace, "Available VK instance layers."); #endif for(uint32_t i = 0; i < vk_available_layers_count; i++) { #ifdef DEBUG - std::cout << "\nname: " << vk_available_layers[i].layerName << - std::endl; - std::cout << "Description: " << vk_available_layers[i].description << - std::endl; - std::cout << "Spec version: " << vk_available_layers[i].specVersion << - std::endl; - std::cout << "Implementation version: " << - vk_available_layers[i].implementationVersion << std::endl; + std::stringstream message{}; + message << "\nname: " << vk_available_layers[i].layerName << std::endl; + message << "Description: " << vk_available_layers[i].description << + std::endl; + message << "Spec version: " << vk_available_layers[i].specVersion << + std::endl; + message << "Implementation version: " << + vk_available_layers[i].implementationVersion << std::endl; + cg_core.log.message(Log::Level::Trace, message.str()); #endif vk_available_layers_names[i] = vk_available_layers[i].layerName; @@ -402,7 +407,7 @@ load_vk_devices(void *obj) } #ifdef DEBUG - std::cout << "Physical devices properties" << std::endl; + cg_core.log.message(Log::Level::Trace, "Physical devices properties"); #endif cg_core.vk_devices.reserve(devices_count); @@ -462,6 +467,30 @@ unload_vk_graphics_pipeline(void *obj) delete cg_core.vk_graphics_pipeline; } +static void +load_mruby(void *obj) +{ + cg_core.mrb = mrb_open(); + if (!cg_core.mrb) throw CommandError{"Failed to initialize mruby."}; +} + +static void +unload_mruby(void *obj) +{ + mrb_close(cg_core.mrb); +} + +static void +load_mruby_symbols(void *obj) +{ + cg_core.sym_fatal = mrb_intern_cstr(cg_core.mrb, "fatal"); + cg_core.sym_error = mrb_intern_cstr(cg_core.mrb, "error"); + cg_core.sym_warning = mrb_intern_cstr(cg_core.mrb, "warning"); + cg_core.sym_information = mrb_intern_cstr(cg_core.mrb, "information"); + cg_core.sym_debug = mrb_intern_cstr(cg_core.mrb, "debug"); + cg_core.sym_trace = mrb_intern_cstr(cg_core.mrb, "trace"); +} + const CommandChain cg_sCore::loader{ {&load_variables, &unload_variables}, {&load_sdl, &unload_sdl}, @@ -476,5 +505,7 @@ const CommandChain cg_sCore::loader{ #endif {&load_vk_devices, &unload_vk_devices}, {&load_vk_swapchain, &unload_vk_swapchain}, - {&load_vk_graphics_pipeline, &unload_vk_graphics_pipeline} + {&load_vk_graphics_pipeline, &unload_vk_graphics_pipeline}, + {&load_mruby, &unload_mruby}, + {&load_mruby_symbols, nullptr} }; diff --git a/src/core.hpp b/src/core.hpp index 117a81f..a51d2b4 100644 --- a/src/core.hpp +++ b/src/core.hpp @@ -38,6 +38,7 @@ #include <SDL2/SDL_mixer.h> #include "command.hpp" +#include "log.hpp" #include "vk/device.hpp" #include "vk/graphics_pipeline.hpp" @@ -54,6 +55,14 @@ struct cg_sCore { static const CommandChain loader; + Log::Logger log; + + mrb_state *mrb; + + /// mruby symbols + mrb_sym sym_fatal, sym_error, sym_warning, sym_information, sym_debug, + sym_trace; + const char *config_file; /// Text displayed in the game window. diff --git a/src/log.cpp b/src/log.cpp index 201370c..78044c4 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -16,23 +16,38 @@ #include "log.hpp" -#include <stdio.h> +#include <iostream> -static mrb_value -cg_mLog_info(mrb_state *mrb, mrb_value self) +namespace Log { - const char *message; - mrb_get_args(mrb, "z", &message); - printf("%s\n", message); - - return self; -} void -cg_log_init(mrb_state *mrb) +Logger::message(Level lvl, const char* text) { - struct RClass *cg_m, *cg_mLog; - cg_m = mrb_module_get(mrb, "CandyGear"); - cg_mLog = mrb_define_module_under(mrb, cg_m, "Log"); - mrb_define_class_method(mrb, cg_mLog, "info", cg_mLog_info, MRB_ARGS_REQ(1)); + std::cout << "["; + switch(lvl) + { + case Level::Fatal: + std::cout << "\e[1;35mFatal\e[0;0m"; + break; + case Level::Error: + std::cout << "\e[1;31mError\e[0;0m"; + break; + case Level::Warning: + std::cout << "\e[1;33mWarning\e[0;0m"; + break; + case Level::Information: + std::cout << "\e[1;32mInformation\e[0;0m"; + break; + case Level::Debug: + std::cout << "\e[1;36mDebug\e[0;0m"; + break; + case Level::Trace: + std::cout << "\e[1;34mTrace\e[0;0m"; + break; + } + std::cout << "] "; + std::cout << text << std::endl; +} + } diff --git a/src/log.hpp b/src/log.hpp index 90e632c..2856f20 100644 --- a/src/log.hpp +++ b/src/log.hpp @@ -17,9 +17,38 @@ #ifndef CANDY_GEAR_LOG_H #define CANDY_GEAR_LOG_H 1 -#include "core.hpp" +#include <string> -void -cg_log_init(mrb_state *mrb); +#define CANDY_GEAR_LOG_LEVEL_FATAL 0 +#define CANDY_GEAR_LOG_LEVEL_ERROR 1 +#define CANDY_GEAR_LOG_LEVEL_WARNING 2 +#define CANDY_GEAR_LOG_LEVEL_INFORMATION 3 +#define CANDY_GEAR_LOG_LEVEL_DEBUG 4 +#define CANDY_GEAR_LOG_LEVEL_TRACE 5 + +namespace Log +{ + +enum class Level +{ + Fatal = CANDY_GEAR_LOG_LEVEL_FATAL, + Error = CANDY_GEAR_LOG_LEVEL_ERROR, + Warning = CANDY_GEAR_LOG_LEVEL_WARNING, + Information = CANDY_GEAR_LOG_LEVEL_INFORMATION, + Debug = CANDY_GEAR_LOG_LEVEL_DEBUG, + Trace = CANDY_GEAR_LOG_LEVEL_TRACE +}; + +struct Logger +{ + + void + message(Level lvl, const char* text); + + inline void + message(Level lvl, const std::string &text) {message(lvl, text.c_str());} +}; + +} #endif /* CANDY_GEAR_LOG_H */ diff --git a/src/main.cpp b/src/main.cpp index 3876db0..34cbcb4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,15 +14,10 @@ * limitations under the License. */ -#ifdef DEBUG -#include <iostream> -#endif - #include "camera.hpp" #include "candy_gear.hpp" #include "core.hpp" #include "key.hpp" -#include "log.hpp" #include "model.hpp" #include "model/instance.hpp" #include "sound.hpp" @@ -44,7 +39,6 @@ int main(int argc, char *argv[]) // Random numbers random_number_generator.seed(random_seed()); - mrb_state *mrb = mrb_open(); mrb_value main_obj; mrb_sym sym_init, sym_key_down, sym_key_up, sym_quit, sym_tick; FILE *fp; @@ -54,39 +48,35 @@ int main(int argc, char *argv[]) try{ cg_sCore::loader.execute(nullptr); } catch(const CommandError &error) { -#ifdef DEBUG - std::cout << error.what() << std::endl; -#endif + cg_core.log.message(Log::Level::Fatal, error.what()); return 1; } - if (!mrb) { /* handle error */ } - mrb_define_module(mrb, "CandyGear"); - cg_camera_init(mrb); - cg_candy_gear_init(mrb); - cg_key_init(mrb); - cg_log_init(mrb); - cg_model_init(mrb); - cg_model_instance_init(mrb); - cg_sound_init(mrb); - cg_texture_init(mrb); - - main_obj = mrb_obj_iv_inspect(mrb, mrb->top_self); - sym_init = mrb_intern_cstr(mrb, "init"); - sym_key_down = mrb_intern_cstr(mrb, "key_down"); - sym_key_up = mrb_intern_cstr(mrb, "key_up"); - sym_quit = mrb_intern_cstr(mrb, "quit"); - sym_tick = mrb_intern_cstr(mrb, "tick"); + mrb_define_module(cg_core.mrb, "CandyGear"); + cg_camera_init(cg_core.mrb); + cg_candy_gear_init(cg_core.mrb); + cg_key_init(cg_core.mrb); + cg_model_init(cg_core.mrb); + cg_model_instance_init(cg_core.mrb); + cg_sound_init(cg_core.mrb); + cg_texture_init(cg_core.mrb); + + main_obj = mrb_obj_iv_inspect(cg_core.mrb, cg_core.mrb->top_self); + sym_init = mrb_intern_cstr(cg_core.mrb, "init"); + sym_key_down = mrb_intern_cstr(cg_core.mrb, "key_down"); + sym_key_up = mrb_intern_cstr(cg_core.mrb, "key_up"); + sym_quit = mrb_intern_cstr(cg_core.mrb, "quit"); + sym_tick = mrb_intern_cstr(cg_core.mrb, "tick"); fp = fopen(argv[2], "r"); - mrb_load_irep_file(mrb, fp); + mrb_load_irep_file(cg_core.mrb, fp); fclose(fp); - if (mrb->exc) - handle_error(mrb); + if (cg_core.mrb->exc) + handle_error(cg_core.mrb); else { - mrb_funcall_id(mrb, main_obj, sym_init, 0); - if (mrb->exc) handle_error(mrb); + mrb_funcall_id(cg_core.mrb, main_obj, sym_init, 0); + if (cg_core.mrb->exc) handle_error(cg_core.mrb); } frame_start = SDL_GetTicks(); @@ -101,13 +91,13 @@ int main(int argc, char *argv[]) { case SDL_KEYDOWN: mrb_funcall_id( - mrb, main_obj, sym_key_down, 1, - mrb_int_value(mrb, event.key.keysym.sym)); + cg_core.mrb, main_obj, sym_key_down, 1, + mrb_int_value(cg_core.mrb, event.key.keysym.sym)); break; case SDL_KEYUP: mrb_funcall_id( - mrb, main_obj, sym_key_up, 1, - mrb_int_value(mrb, event.key.keysym.sym)); + cg_core.mrb, main_obj, sym_key_up, 1, + mrb_int_value(cg_core.mrb, event.key.keysym.sym)); break; case SDL_MOUSEMOTION: break; @@ -116,13 +106,13 @@ int main(int argc, char *argv[]) case SDL_MOUSEBUTTONUP: break; case SDL_QUIT: - mrb_funcall_id(mrb, main_obj, sym_quit, 0); + mrb_funcall_id(cg_core.mrb, main_obj, sym_quit, 0); break; } } - mrb_funcall_id(mrb, main_obj, sym_tick, 0); - if (mrb->exc) handle_error(mrb); + mrb_funcall_id(cg_core.mrb, main_obj, sym_tick, 0); + if (cg_core.mrb->exc) handle_error(cg_core.mrb); cg_core.vk_graphics_pipeline->draw(); @@ -141,7 +131,6 @@ int main(int argc, char *argv[]) } } - mrb_close(mrb); cg_sCore::loader.revert(nullptr); return 0; diff --git a/src/vk/device.cpp b/src/vk/device.cpp index 2434283..b4b47fb 100644 --- a/src/vk/device.cpp +++ b/src/vk/device.cpp @@ -17,9 +17,11 @@ #include "device.hpp" #include <fstream> -#include <iostream> #include <new> #include <vector> +#ifdef DEBUG +#include <sstream> +#endif #include "../core.hpp" @@ -86,15 +88,17 @@ Device::Device(VkPhysicalDevice vk_physical_device, bool with_swapchain) vkGetPhysicalDeviceFeatures(vk_physical_device, &supported_features); #ifdef DEBUG - std::cout << "Name: " << physical_properties.deviceName << std::endl; - std::cout << "API version: " << physical_properties.apiVersion << + std::stringstream message{}; + message << "Name: " << physical_properties.deviceName << std::endl; + message << "API version: " << physical_properties.apiVersion << std::endl; - std::cout << "Driver version: " << physical_properties.driverVersion << + message << "Driver version: " << physical_properties.driverVersion << std::endl; - std::cout << "Vendor ID: " << physical_properties.vendorID << std::endl; - std::cout << "Device ID: " << physical_properties.deviceID << std::endl; - std::cout << "Device type: " << physical_properties.deviceType << + message << "Vendor ID: " << physical_properties.vendorID << std::endl; + message << "Device ID: " << physical_properties.deviceID << std::endl; + message << "Device type: " << physical_properties.deviceType << std::endl; + cg_core.log.message(Log::Level::Trace, message.str()); #endif std::vector<VkDeviceQueueCreateInfo> device_queue_create_infos; diff --git a/src/vk/queue_family.cpp b/src/vk/queue_family.cpp index 7c91ba2..d917c13 100644 --- a/src/vk/queue_family.cpp +++ b/src/vk/queue_family.cpp @@ -16,7 +16,9 @@ #include "queue_family.hpp" -#include <iostream> +#ifdef DEBUG +#include <sstream> +#endif #include "../core.hpp" @@ -30,20 +32,22 @@ QueueFamily::QueueFamily( { #ifdef DEBUG - std::cout << "Queue quantity: " << queue_family_properties.queueCount << + std::stringstream message{}; + message << "Queue quantity: " << queue_family_properties.queueCount << std::endl; - std::cout << "Graphics: " << + message << "Graphics: " << (queue_family_properties.queueFlags & VK_QUEUE_GRAPHICS_BIT ? "true" : "false") << std::endl; - std::cout << "Compute: " << + message << "Compute: " << (queue_family_properties.queueFlags & VK_QUEUE_COMPUTE_BIT ? "true" : "false") << std::endl; - std::cout << "Transfer: " << + message << "Transfer: " << (queue_family_properties.queueFlags & VK_QUEUE_TRANSFER_BIT ? "true" : "false") << std::endl; - std::cout << "Sparse Binding: " << + message << "Sparse Binding: " << (queue_family_properties.queueFlags & VK_QUEUE_SPARSE_BINDING_BIT ? "true" : "false") << std::endl; + cg_core.log.message(Log::Level::Trace, message.str()); #endif this->device = device; diff --git a/src/vk/swapchain.cpp b/src/vk/swapchain.cpp index 3b26b00..678279e 100644 --- a/src/vk/swapchain.cpp +++ b/src/vk/swapchain.cpp @@ -19,7 +19,6 @@ #include "../core.hpp" #include <vector> -#include <iostream> namespace { |