From 4ece64442fc12be150be217d71dab9afd2d2f8e4 Mon Sep 17 00:00:00 2001 From: frank <420@shampoo.ooo> Date: Sat, 8 May 2021 20:01:53 -0400 Subject: [PATCH] custom logging function --- demo/config.json | 10 ++-------- src/Configuration.cpp | 13 +++++++++---- src/Game.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- src/Game.hpp | 3 +++ src/Recorder.cpp | 4 ++-- 5 files changed, 57 insertions(+), 15 deletions(-) diff --git a/demo/config.json b/demo/config.json index 4f72cc3..ec5d21a 100644 --- a/demo/config.json +++ b/demo/config.json @@ -4,14 +4,6 @@ "dimensions": [864, 486], "fps": 60 }, - "path": - { - "screenshots": "local/screenshots", - "video": "local/video" - }, - "gamepad": - { - }, "keys": { "context": " ", @@ -20,6 +12,8 @@ }, "recording": { + "screenshot-directory": "local/screenshots", + "video-directory": "local/video" "enabled": true, "write-mp4": true, "video-frame-length": 16.667, diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 8769ce4..fe755b7 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -38,10 +38,6 @@ void Configuration::set_defaults() {"default-unsuppress-delay", 700}, {"ignore-repeat-keypress", true} }; - sys_config["path"] = { - {"screenshots", "."}, - {"video", "."} - }; sys_config["display"] = { {"dimensions", {640, 480}}, {"framerate", 60}, @@ -60,7 +56,9 @@ void Configuration::set_defaults() {"screenshot-prefix", "screenshot-"}, {"screenshot-extension", ".png"}, {"screenshot-zfill", 5}, + {"screenshot-directory", "."}, {"gif-frame-length", 100}, + {"video-directory", "."}, {"write-mp4", false}, {"max-stash-length", 5000}, {"max-in-game-stashes", 3}, @@ -77,6 +75,13 @@ void Configuration::set_defaults() sys_config["animation"] = { {"all-frames-frameset-name", "all"} }; + sys_config["logging"] = { + {"enabled", false}, + {"debug", false}, + {"ouput-directory", "."}, + {"info-file-name", "log.txt"}, + {"debug-file-name", "debug_log.txt"} + }; } void Configuration::load() diff --git a/src/Game.cpp b/src/Game.cpp index 8cf7a88..79be51d 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -39,9 +39,49 @@ void FramerateIndicator::refresh() } } +void log(void* userdata, int category, SDL_LogPriority priority, const char* message) +{ + Game* game = static_cast(userdata); + std::ostream& out = (priority > SDL_LOG_PRIORITY_DEBUG) ? std::cerr : std::cout; + out << message << std::endl; + if (game->get_configuration()["logging"]["enabled"]) + { + fs::path path = game->get_configuration()["logging"]["output-directory"]; + if (!fs::exists(path)) + { + fs::create_directories(path); + } + std::time_t now = std::time(nullptr); + std::stringstream stamped_message; + stamped_message << std::put_time(std::localtime(&now), "%F %T ") << message; + if (game->get_configuration()["logging"]["debug"]) + { + fs::path debug_path = path / game->get_configuration()["logging"]["debug-file-name"]; + std::ofstream debug_stream(debug_path, std::ios_base::app); + debug_stream << stamped_message.str() << std::endl; + } + if (priority > SDL_LOG_PRIORITY_DEBUG) + { + fs::path info_path = path / game->get_configuration()["logging"]["info-file-name"]; + std::ofstream info_stream(info_path, std::ios_base::app); + info_stream << stamped_message.str() << std::endl; + } + } +} + Game::Game() { - std::cout << std::setw(4) << get_configuration() << std::endl; + // use config to determine if debug statements should be printed + if (get_configuration()["logging"]["debug"]) + { + SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG); + } + // set custom log function (prints to stdout/stderr and to file if enabled) + SDL_LogSetOutputFunction(&log, this); + // pretty print config to debug log + std::stringstream config_formatted; + config_formatted << std::setw(4) << get_configuration() << std::endl; + SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "%s", config_formatted.str().c_str()); SDL_SetHint(SDL_HINT_RENDER_DRIVER, get_configuration()["display"]["render driver"].get().c_str()); frame_length_history.reserve(5000); set_framerate(get_configuration()["display"]["framerate"]); diff --git a/src/Game.hpp b/src/Game.hpp index db90eac..f2abf37 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #define SDL_MAIN_HANDLED #include "SDL.h" @@ -41,6 +42,8 @@ struct FramerateIndicator : Sprite }; +void log(void*, int, SDL_LogPriority, const char*); + struct Game : Node { diff --git a/src/Recorder.cpp b/src/Recorder.cpp index e79a73d..5a48b86 100644 --- a/src/Recorder.cpp +++ b/src/Recorder.cpp @@ -55,7 +55,7 @@ void Recorder::capture_screen() { nlohmann::json config = get_configuration(); SDL_Surface* surface = get_display().get_screen_surface(); - fs::path directory = config["path"]["screenshots"]; + fs::path directory = config["recording"]["screenshot-directory"]; fs::create_directories(directory); std::string prefix = config["recording"]["screenshot-prefix"]. get(); @@ -222,7 +222,7 @@ int Recorder::get_memory_size() void Recorder::make_directory() { nlohmann::json config = get_configuration(); - fs::path root = config["path"]["video"]; + fs::path root = config["recording"]["video-directory"]; fs::create_directories(root); fs::path directory = sfw::get_next_file_name(root, 5, "video-"); fs::create_directories(directory);