Browse Source

Add auto newline feature to logger

master
C. J. Howard 3 years ago
parent
commit
351d65dcbc
5 changed files with 95 additions and 48 deletions
  1. +24
    -13
      src/application.cpp
  2. +1
    -1
      src/application.hpp
  3. +39
    -17
      src/debug/logger.cpp
  4. +30
    -16
      src/debug/logger.hpp
  5. +1
    -1
      src/resources/resource-manager.hpp

+ 24
- 13
src/application.cpp View File

@ -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::string()>(std::bind(&cc::scrot, this)));
cli.register_command("cue", std::function<std::string(float, std::string)>(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<model>("larva.obj"));
//underworld_scene.add_object(larva);
quaternion<float> flashlight_rotation = vmq::angle_axis(vmq::half_pi<float>, {0.0f, 1.0f, 0.0f});
model_instance* flashlight = new model_instance(resource_manager->load<model>("flashlight.obj"));
flashlight->set_rotation(flashlight_rotation);
underworld_scene.add_object(flashlight);
model_instance* flashlight_light_cone = new model_instance(resource_manager->load<model>("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)

+ 1
- 1
src/application.hpp View File

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

+ 39
- 17
src/debug/logger.cpp View File

@ -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;
}

+ 30
- 16
src/debug/logger.hpp View File

@ -25,10 +25,16 @@
#include <stack>
#include <string>
/**
* 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<std::string> tasks;
std::string indent;
bool timestamp_enabled;
};
#endif // ANTKEEPER_LOGGER_HPP

+ 1
- 1
src/resources/resource-manager.hpp View File

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

Loading…
Cancel
Save