add support for verbose log priority

This commit is contained in:
ohsqueezy 2023-07-01 19:21:06 -04:00
parent 88cf70b828
commit f7f6bee582
6 changed files with 72 additions and 90 deletions

View File

@ -84,6 +84,7 @@ void Configuration::set_defaults()
config["log"] = {
{"enabled", false},
{"debug-to-stdout", true},
{"verbose to stdout", false},
{"debug-to-file", false},
{"output-directory", "."},
{"info-file-name", "space_box_log.txt"},

View File

@ -42,7 +42,7 @@ void GLObject::generate(generator_function generator)
this->id(id);
std::ostringstream message;
message << "Generated ID " << this->id() << " for GL object";
sb::Log::log(message, sb::Log::DEBUG);
sb::Log::log(message, sb::Log::VERBOSE);
}
else
{

View File

@ -29,9 +29,13 @@ Game::Game()
_configuration.enable_auto_refresh(USER_CONFIG_PATH, configuration()["configuration"]["auto-refresh-interval"].get<float>());
}
/* Set the appropriate priority level for the default log category, changing it to INFO if the user does not want debug statements,
* or leaving it at DEBUG otherwise. */
if (!configuration()["log"]["debug-to-file"] && !configuration()["log"]["debug-to-stdout"])
/* Set the appropriate priority level for the default log category. Change it to VERBOSE if it is requested. Otherwise,
* change it to INFO if the user does not want debug statements, or leave it at DEBUG if not. */
if (_configuration("log", "verbose to stdout"))
{
SDL_LogSetPriority(sb::Log::DEFAULT_CATEGORY, SDL_LOG_PRIORITY_VERBOSE);
}
else if (!configuration()["log"]["debug-to-file"] && !configuration()["log"]["debug-to-stdout"])
{
SDL_LogSetPriority(sb::Log::DEFAULT_CATEGORY, SDL_LOG_PRIORITY_INFO);
}
@ -39,7 +43,10 @@ Game::Game()
/* If recording is enabled by configuration, activate it. */
if (configuration()["recording"]["enabled"])
{
// recorder.animation.play();
/* Recorder object disabled because of a memory leak
*
* recorder.animation.play()
*/
activate();
}
@ -606,7 +613,7 @@ void Game::frame(float timestamp)
frame_count_this_second = 0;
std::ostringstream message;
message << "Counted " << current_frames_per_second << " frames last second";
sb::Log::log(message, sb::Log::DEBUG);
sb::Log::log(message, sb::Log::VERBOSE);
}
}
}

View File

@ -10,22 +10,16 @@
#include "Log.hpp"
/* Send a message to SDL's log function, which currently gets overridden in the Game class.
* The default level is INFO. Category will default to SDL's custom category. Using the default
* category will ensure that debug level statements are handled according to the options in the
* global configuration. */
void sb::Log::log(const std::string& message, const Level level, const int category)
{
SDL_LogMessage(category, static_cast<SDL_LogPriority>(level), "%s", message.c_str());
}
/* Convert string stream to string and forward */
void sb::Log::log(const std::ostringstream& message, const Level level, const int category)
{
log(message.str(), level, category);
}
/* Log all GL errors accumulated since the last time this function was called */
bool sb::Log::gl_errors(const std::string& heading)
{
GLenum error;
@ -78,51 +72,9 @@ bool sb::Log::gl_errors(const std::string& heading)
return error_logged;
}
void sb::Log::sdl_error(const std::string& original_message)
void sb::Log::sdl_error(const std::string& message)
{
std::ostringstream message;
message << original_message << " " << SDL_GetError();
log(message, Level::ERROR);
std::ostringstream full_message;
full_message << message << " " << SDL_GetError();
log(full_message, Level::ERROR);
}
/* Overrides SDL's default log function to log a message to stdout/stderr and, if log is enabled in the
* global configuration, to a file. Debug level statements may be suppressed, printed to stdout, or printed to
* both stdout and file, depending on the global configuration.
*/
// void sb::Log::record(void* userdata, int category, SDL_LogPriority priority, const char* message)
// {
// Game* game = static_cast<Game*>(userdata);
// std::ostream& out = (priority > SDL_LOG_PRIORITY_WARN) ? std::cerr : std::cout;
// /* print to stdout/stderr if priority is higher than debug or debug statements are enabled */
// if (priority > SDL_LOG_PRIORITY_DEBUG /* || game->configuration()["log"]["debug-to-stdout"] */)
// {
// out << message << std::endl;
// }
// /* handle writing to log file */
// if (game->configuration()["log"]["enabled"])
// {
// fs::path path = game->configuration()["log"]["output-directory"];
// if (!fs::exists(path))
// {
// fs::create_directories(path);
// }
// /* prepend a timestamp to the message */
// std::time_t now = std::time(nullptr);
// std::stringstream stamped_message;
// stamped_message << std::put_time(std::localtime(&now), "%F %T ") << message;
// /* if debug is enabled, append message to debug log file */
// if (game->configuration()["log"]["debug-to-file"])
// {
// fs::path debug_path = path / game->configuration()["log"]["debug-file-name"];
// std::ofstream debug_stream(debug_path, std::ios_base::app);
// debug_stream << stamped_message.str() << std::endl;
// }
// /* only append messages to the info log that are higher than debug priority */
// if (priority > SDL_LOG_PRIORITY_DEBUG)
// {
// fs::path info_path = path / game->configuration()["log"]["info-file-name"];
// std::ofstream info_stream(info_path, std::ios_base::app);
// info_stream << stamped_message.str() << std::endl;
// }
// }
// }

View File

@ -1,21 +1,19 @@
/*!<pre>
* /\ +------------------------------------------------------+
* ____/ \____ /| - Open source game framework licensed to freely use, |
* \ / / | copy, modify and sell without restriction |
* +--\ ^__^ /--+ | |
* | ~/ \~ | | - created for <https://foam.shampoo.ooo> |
* | ~~~~~~~~~~~~ | +------------------------------------------------------+
* | SPACE ~~~~~ | /
* | ~~~~~~~ BOX |/
* +--------------+ </pre>
*
* Log
* ===
*
* Log messages of specified priority through the SDL logging method, which is overridden in
* the Game class to write to stdout, file, or both, depending on the user's configuration
* settings.
*/
/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
/*! @file
Log messages of specified priority through the SDL logging method, which is overridden in
sb::Game to write to stdout, file, or both, depending on the user's configuration settings.
*/
#pragma once
@ -33,7 +31,6 @@
#include <sstream>
#include <functional>
#include <SDL.h>
/* #include "filesystem.hpp" */
namespace sb
{
@ -43,7 +40,7 @@ namespace sb
public:
/* These definitions are equivalent to SDL's SDL_LOG_PRIORITY_* values */
/* Equivalent of SDL's SDL_LOG_PRIORITY_* values */
enum Level
{
VERBOSE = 1,
@ -55,16 +52,41 @@ namespace sb
};
static const int DEFAULT_CATEGORY = SDL_LOG_CATEGORY_CUSTOM;
Log(std::function<void(void*, int, SDL_LogPriority, const char*)>);
static void log(const std::string&, const Level = INFO, const int = DEFAULT_CATEGORY);
static void log(const std::ostringstream&, const Level = INFO, const int = DEFAULT_CATEGORY);
static bool gl_errors(const std::string& = "");
static void sdl_error(const std::string&);
/* static void record(void*, int, SDL_LogPriority, const char*); */
/*!
* Send a message to SDL's log function, which currently gets overridden in sb::Game. The default level is Level::INFO.
*
* Category will default to SDL's custom category. Using the default category will ensure that debug level statements are
* handled according to the options in the global configuration.
*
* @param message text to add to the log
* @param level message priority level
* @param category a category that matches SDL_LOG_CATEGORY_*
*/
static void log(const std::string& message, const Level level = INFO, const int category = DEFAULT_CATEGORY);
/*!
* Send a log statement using a stringstream.
*
* @see Log::log(const std::string&, const Level, const int)
*/
static void log(const std::ostringstream& message, const Level level = INFO, const int category = DEFAULT_CATEGORY);
/*!
* Log all GL errors accumulated since the last time this function was called.
*
* @param heading optional text to prepend to the error message
*/
static bool gl_errors(const std::string& heading = "");
/*!
* Log a message, adding the results of `SDL_GetError` to the end of the message. Should be used to add more information to
* an error statement when the error happened in SDL. The priority level will always be `Level::ERROR`
*
* @param message text to log before the SDL error is appended
*/
static void sdl_error(const std::string& message);
};
/* Log log = Log(&Log::record); */
}

View File

@ -89,7 +89,7 @@ void Texture::load(SDL_Surface* surface)
if (surface->w > 0 && surface->h > 0)
{
message << "Loading image from SDL surface (" << surface->w << "×" << surface->h << ", " << SDL_GetPixelFormatName(surface->format->format) << ")";
sb::Log::log(message, sb::Log::DEBUG);
sb::Log::log(message, sb::Log::VERBOSE);
load(surface->pixels, {surface->w, surface->h}, GL_RGBA, GL_UNSIGNED_BYTE);
}
else
@ -112,7 +112,7 @@ void Texture::load(void* pixels, glm::vec2 size, GLenum format, GLenum type)
bind();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size.x, size.y, format, type, pixels);
message << "Loaded " << size.x << "×" << size.y << " image into texture ID " << id();
message_level = sb::Log::DEBUG;
message_level = sb::Log::VERBOSE;
}
else
{
@ -178,7 +178,7 @@ bool Texture::operator==(const Texture& texture) const
void sb::texture_deleter(GLuint* id)
{
/* not sure why SDL_Log works here at program end but SDL_LogDebug and SDL_LogInfo don't */
SDL_Log("destroying texture ID %i", *id);
SDL_LogVerbose(sb::Log::DEFAULT_CATEGORY, "destroying texture ID %i", *id);
glDeleteTextures(1, id);
delete id;
}