From 351d65dcbc6a71c993db08a0d8862f6646469503 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Fri, 7 Aug 2020 09:56:02 -0700 Subject: [PATCH] Add auto newline feature to logger --- src/application.cpp | 37 +++++++++++++------- src/application.hpp | 2 +- src/debug/logger.cpp | 56 +++++++++++++++++++++--------- src/debug/logger.hpp | 46 +++++++++++++++--------- src/resources/resource-manager.hpp | 2 +- 5 files changed, 95 insertions(+), 48 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index f44143f..2664c0b 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -134,8 +134,8 @@ application::application(int argc, char** argv): // Form resource paths data_path = get_data_path(application_name) + "data/"; config_path = get_config_path(application_name); - logger.log("Detected data path as \"" + data_path + "\"\n"); - logger.log("Detected config path as \"" + config_path + "\"\n"); + logger.log("Detected data path as \"" + data_path + "\""); + logger.log("Detected config path as \"" + config_path + "\""); screenshots_path = config_path + "screenshots/"; @@ -165,8 +165,8 @@ application::application(int argc, char** argv): cli.register_command("scrot", std::function(std::bind(&cc::scrot, this))); cli.register_command("cue", std::function(std::bind(&cc::cue, this, std::placeholders::_1, std::placeholders::_2))); //std::string cmd = "cue 20 exit"; - //logger.log(cmd + "\n"); - //logger.log(cli.interpret(cmd) + "\n"); + //logger.log(cmd); + //logger.log(cli.interpret(cmd)); // Setup resource manager resource_manager = new ::resource_manager(); @@ -188,13 +188,13 @@ application::application(int argc, char** argv): SDL_version sdl_compiled_version; SDL_VERSION(&sdl_compiled_version); std::string sdl_compiled_version_string = std::to_string(sdl_compiled_version.major) + "." + std::to_string(sdl_compiled_version.minor) + "." + std::to_string(sdl_compiled_version.patch); - logger.log("Compiled against SDL " + sdl_compiled_version_string + "\n"); + logger.log("Compiled against SDL " + sdl_compiled_version_string); // Get SDL linked version SDL_version sdl_linked_version; SDL_GetVersion(&sdl_linked_version); std::string sdl_linked_version_string = std::to_string(sdl_linked_version.major) + "." + std::to_string(sdl_linked_version.minor) + "." + std::to_string(sdl_linked_version.patch); - logger.log("Linking against SDL " + sdl_linked_version_string + "\n"); + logger.log("Linking against SDL " + sdl_linked_version_string); // Init SDL logger.push_task("Initializing SDL"); @@ -229,11 +229,11 @@ application::application(int argc, char** argv): SDL_DisplayMode sdl_display_mode; if (SDL_GetDesktopDisplayMode(0, &sdl_display_mode) != 0) { - logger.error("Failed to get desktop display mode: \"" + std::string(SDL_GetError()) + "\"\n"); + logger.error("Failed to get desktop display mode: \"" + std::string(SDL_GetError()) + "\""); } else { - logger.log("Detected " + std::to_string(sdl_display_mode.w) + "x" + std::to_string(sdl_display_mode.h) + " display\n"); + logger.log("Detected " + std::to_string(sdl_display_mode.w) + "x" + std::to_string(sdl_display_mode.h) + " display"); display_dimensions = {sdl_display_mode.w, sdl_display_mode.h}; } @@ -827,8 +827,14 @@ application::application(int argc, char** argv): underworld_scene.add_object(portal_billboard); //model_instance* larva = new model_instance(resource_manager->load("larva.obj")); //underworld_scene.add_object(larva); + + quaternion flashlight_rotation = vmq::angle_axis(vmq::half_pi, {0.0f, 1.0f, 0.0f}); model_instance* flashlight = new model_instance(resource_manager->load("flashlight.obj")); + flashlight->set_rotation(flashlight_rotation); underworld_scene.add_object(flashlight); + model_instance* flashlight_light_cone = new model_instance(resource_manager->load("flashlight-light-cone.obj")); + flashlight_light_cone->set_rotation(flashlight_rotation); + underworld_scene.add_object(flashlight_light_cone); // Set overworld as active scene active_scene = &overworld_scene; @@ -1045,11 +1051,11 @@ void application::translate_sdl_events() std::string controller_name = SDL_GameControllerNameForIndex(sdl_event.cdevice.which); if (sdl_controller) { - logger.log("Connected game controller \"" + controller_name + "\"\n"); + logger.log("Connected game controller \"" + controller_name + "\""); } else { - logger.error("Failed to connected game controller \"" + controller_name + "\"\n"); + logger.error("Failed to connected game controller \"" + controller_name + "\""); } } @@ -1063,7 +1069,7 @@ void application::translate_sdl_events() if (sdl_controller) { SDL_GameControllerClose(sdl_controller); - logger.log("Disconnected game controller\n"); + logger.log("Disconnected game controller"); } break; @@ -1144,8 +1150,12 @@ void application::window_resized() ui_system->set_viewport(viewport); } -void application::take_screenshot() const +void application::take_screenshot() { + std::string filename = screenshots_path + "antkeeper-" + timestamp() + ".png"; + + logger.push_task("Saving screenshot to \"" + filename + "\""); + int x = viewport[0]; int y = viewport[1]; int w = viewport[2]; @@ -1156,9 +1166,10 @@ void application::take_screenshot() const glReadBuffer(GL_BACK); glReadPixels(x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixels); - std::string filename = screenshots_path + "antkeeper-" + timestamp() + ".png"; std::thread screenshot_thread(application::save_image, filename, w, h, pixels); screenshot_thread.detach(); + + logger.pop_task(EXIT_SUCCESS); } void application::save_image(const std::string& filename, int w, int h, const unsigned char* pixels) diff --git a/src/application.hpp b/src/application.hpp index 172a3fe..fd9a668 100644 --- a/src/application.hpp +++ b/src/application.hpp @@ -173,7 +173,7 @@ public: entt::registry& get_ecs_registry(); scene& get_scene(); - void take_screenshot() const; + void take_screenshot(); // UI scene* get_ui_scene(); diff --git a/src/debug/logger.cpp b/src/debug/logger.cpp index c8a5207..23bfbbe 100644 --- a/src/debug/logger.cpp +++ b/src/debug/logger.cpp @@ -23,6 +23,9 @@ logger::logger(): os(&std::cout), + auto_newline(true), + timestamp_enabled(true), + indent("| "), log_prefix(std::string()), log_postfix(std::string()), warning_prefix(std::string()), @@ -30,9 +33,7 @@ logger::logger(): error_prefix(std::string()), error_postfix(std::string()), success_prefix(std::string()), - success_postfix(std::string()), - indent("| "), - timestamp_enabled(true) + success_postfix(std::string()) {} logger::~logger() @@ -47,15 +48,24 @@ void logger::log(const std::string& text) { if (os) { + // Prepend timestamp if (timestamp_enabled) { (*os) << timestamp() << ": "; } + // Prepend indentation for (std::size_t i = 0; i < tasks.size(); ++i) (*os) << indent; + // Output text (*os) << (log_prefix + text + log_postfix); + + // Append newline + if (auto_newline) + { + (*os) << "\n"; + } os->flush(); } @@ -76,6 +86,21 @@ void logger::success(const std::string& text) log(success_prefix + text + success_postfix); } +void logger::set_auto_newline(bool enabled) +{ + auto_newline = enabled; +} + +void logger::set_timestamp(bool enabled) +{ + timestamp_enabled = enabled; +} + +void logger::set_indent(const std::string& indent) +{ + this->indent = indent; +} + void logger::set_log_prefix(const std::string& prefix) { log_prefix = prefix; @@ -118,7 +143,9 @@ void logger::set_success_postfix(const std::string& postfix) void logger::push_task(const std::string& description) { - std::string message = description + "...\n"; + std::string message = description + "..."; + if (!auto_newline) + message += "\n"; log(message); @@ -138,23 +165,18 @@ void logger::pop_task(int status) if (status == EXIT_SUCCESS) { - message += "success\n"; + message += "success"; + if (!auto_newline) + message += "\n"; + log(message); } else { - message += "failed (" + std::to_string(status) + ")\n"; + message += "failed (" + std::to_string(status) + ")"; + if (!auto_newline) + message += "\n"; + error(message); } } - -void logger::set_indent(const std::string& indent) -{ - this->indent = indent; -} - -void logger::set_timestamp(bool enabled) -{ - timestamp_enabled = enabled; -} - diff --git a/src/debug/logger.hpp b/src/debug/logger.hpp index 06527b5..8fb7ef0 100644 --- a/src/debug/logger.hpp +++ b/src/debug/logger.hpp @@ -25,10 +25,16 @@ #include #include +/** + * Logs formatted debug messages to an output stream. + */ class logger { public: + /// Creates a logger. logger(); + + /// Destroys a logger. ~logger(); /** @@ -46,6 +52,27 @@ public: void error(const std::string& text); void success(const std::string& text); + /** + * Enables or disables automatic appending of newlines to log messages. + * + * @param enabled `true` if auto newline should be enabled, `false` otherwise. + */ + void set_auto_newline(bool enabled); + + /** + * Enables or disables prefixing log messages with a timestamp. + * + * @param enabled `true` if timestamps should be enabled, `false` otherwise. + */ + void set_timestamp(bool enabled); + + /** + * Sets the indent string which prefixes subtasks. This string will be repeated according to the level of indentation. + * + * @param indent Indent string. + */ + void set_indent(const std::string& indent); + void set_log_prefix(const std::string& prefix); void set_log_postfix(const std::string& postfix); void set_warning_prefix(const std::string& prefix); @@ -68,23 +95,12 @@ public: * @param status Exit status of the task. A value of `0` or `EXIT_SUCCESS` indicates the task exited successfully. A non-zero exit status indicates the task failed. */ void pop_task(int status); - - /** - * Sets the indent string which prefixes subtasks. This string will be repeated according to the level of indentation. - * - * @param indent Indent string. - */ - void set_indent(const std::string& indent); - - /** - * Enables or disables prefixing log messages with a timestamp. - * - * @param enabled `true` if timestamps should be enabled, `false` otherwise. - */ - void set_timestamp(bool enabled); private: std::ostream* os; + bool auto_newline; + bool timestamp_enabled; + std::string indent; std::string log_prefix; std::string log_postfix; std::string warning_prefix; @@ -94,8 +110,6 @@ private: std::string success_prefix; std::string success_postfix; std::stack tasks; - std::string indent; - bool timestamp_enabled; }; #endif // ANTKEEPER_LOGGER_HPP diff --git a/src/resources/resource-manager.hpp b/src/resources/resource-manager.hpp index 56b559e..6a98e23 100644 --- a/src/resources/resource-manager.hpp +++ b/src/resources/resource-manager.hpp @@ -100,7 +100,7 @@ T* resource_manager::load(const std::string& path) { if (logger) { - logger->log("Fetched resource \"" + path + "\"\n"); + logger->log("Fetched resource \"" + path + "\""); } // Resource found