custom logging function

This commit is contained in:
frank 2021-05-08 20:01:53 -04:00
parent a8948bca73
commit 4ece64442f
5 changed files with 57 additions and 15 deletions

View File

@ -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,

View File

@ -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()

View File

@ -39,9 +39,49 @@ void FramerateIndicator::refresh()
}
}
void log(void* userdata, int category, SDL_LogPriority priority, const char* message)
{
Game* game = static_cast<Game*>(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<std::string>().c_str());
frame_length_history.reserve(5000);
set_framerate(get_configuration()["display"]["framerate"]);

View File

@ -5,6 +5,7 @@
#include <string>
#include <iostream>
#include <sstream>
#include <ctime>
#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
{

View File

@ -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<std::string>();
@ -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);