From d10bc6ac67c8d4e22d68d6f7e8210ba47130af6e Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Sat, 19 May 2018 13:32:22 +1000 Subject: [PATCH] log message output now also shown in console --- README.md | 8 +++++--- src/common/log.c | 24 ++++++++++++++++++++---- src/common/log.h | 3 +++ src/libsymmetry/console.c | 29 ++++++++++++++++++++--------- src/libsymmetry/console.h | 11 +++++++---- src/libsymmetry/game.c | 8 ++++++++ 6 files changed, 63 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index d227b07..909ae50 100644 --- a/README.md +++ b/README.md @@ -155,8 +155,7 @@ - ## TODO - - Console log output - - Console coloured output + - Console error/warning output - Console commands - Console fix bug when enabled in editor mode - Console command history @@ -166,6 +165,8 @@ - Move Gui_State and Editor_State into game_state and modify usage as needed - Remove model and replace all usages with static mesh - Get editor camera speed and other settings from config file + - Recompile Soloud on windows to use static sdl2 backend + - Figure out a way to reduce of remove snprintf calls from render code - Re-Implement player logic - Re-Implement saving/loading scene to/from files - Bring back functionality and complete overhaul @@ -408,4 +409,5 @@ * Re-implemented showing all the entities in the editor * Player init, update, visual representation and movement * Switching between editor and game mode/cameras - * In-game basis for scrollable console/log-viewer \ No newline at end of file + * In-game basis for scrollable console/log-viewer + * Console log output \ No newline at end of file diff --git a/src/common/log.c b/src/common/log.c index 38ded42..7fd7fa2 100644 --- a/src/common/log.c +++ b/src/common/log.c @@ -29,12 +29,17 @@ #endif -static FILE* log_file = NULL; +static void log_message_callback_stub(const char* message, va_list args); + +static FILE* log_file = NULL; +static Log_Message_CB message_callback = log_message_callback_stub; + +#define MAX_LOG_FILE_PATH_LEN 512 void log_init(const char* log_file_name, const char* user_directory) { - char log_file_path[512] = {'\0'}; - snprintf(log_file_path, 512, "%s/%s", user_directory, log_file_name); + char log_file_path[MAX_LOG_FILE_PATH_LEN] = {'\0'}; + snprintf(log_file_path, MAX_LOG_FILE_PATH_LEN, "%s/%s", user_directory, log_file_name); log_file = fopen(log_file_path, "w"); if(!log_file) { @@ -63,7 +68,6 @@ void log_cleanup(void) } } - void log_to_stdout(const char* message, ...) { printf("%sMSG : ", COL_CYAN); @@ -95,6 +99,7 @@ void log_message(const char* message, ...) va_copy(file_list, console_list); vfprintf(log_file, message, file_list); vprintf(message, console_list); + message_callback(message, console_list); va_end(console_list); va_end(file_list); printf("\n%s", COL_RESET); @@ -143,3 +148,14 @@ void log_file_handle_set(FILE* file) { log_file = file; } + +void log_message_callback_set(Log_Message_CB callback) +{ + if(callback) + message_callback = callback; +} + +void log_message_callback_stub(const char* message, va_list args) +{ + // This is just a stub in-case no callback has been set +} \ No newline at end of file diff --git a/src/common/log.h b/src/common/log.h index 99980b5..2ca5614 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -3,6 +3,8 @@ #include +typedef void (*Log_Message_CB)(const char* message, va_list args); + void log_init(const char* log_file_name, const char* user_directory); void log_cleanup(void); void log_message(const char* message, ...); @@ -12,5 +14,6 @@ void log_to_stdout(const char* message, ...); /* Only use when logging is not i void log_raw(const char* str, ...); FILE* log_file_handle_get(void); void log_file_handle_set(FILE* file); +void log_message_callback_set(Log_Message_CB callback); #endif diff --git a/src/libsymmetry/console.c b/src/libsymmetry/console.c index 698d640..67d6f60 100644 --- a/src/libsymmetry/console.c +++ b/src/libsymmetry/console.c @@ -15,13 +15,17 @@ static int console_filter(const struct nk_text_edit *box, nk_rune unicode); void console_init(struct Console* console) { assert(console); - - console->visible = false; - console->text_region_height = 22.f; - console->line_height = 20.f; color_normal = nk_rgb(255, 255, 255); - memset(console->console_command_text, '\0', MAX_CONSOLE_COMMAND_LEN); + + console->visible = false; + console->text_region_height = 22.f; + console->line_height = 20.f; + console->current_message_index = -1; + + memset(console->console_command_text, '\0', MAX_CONSOLE_MESSAGE_LEN); + for(int i = 0; i < MAX_CONSOLE_MESSAGES; i++) + memset(console->console_messages[i], '\0', MAX_CONSOLE_MESSAGE_LEN); } void console_toggle(struct Console* console) @@ -45,10 +49,10 @@ void console_update(struct Console* console, struct Gui_State* gui_state, float nk_layout_row_dynamic(context, nk_window_get_height(context) - console->text_region_height * 2, 1); if(nk_group_begin(context, "Log", NK_WINDOW_SCROLL_AUTO_HIDE)) { - for(int i = 0; i < MAX_CONSOLE_LINES / 2; i++) + for(int i = 0; i <= console->current_message_index; i++) { nk_layout_row_dynamic(context, console->line_height, 1); - nk_labelf_colored(context, NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE, color_normal, "This is a test log line. Do not panic!"); + nk_labelf_colored(context, NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE, color_normal, console->console_messages[i]); } nk_group_end(context); } @@ -56,11 +60,11 @@ void console_update(struct Console* console, struct Gui_State* gui_state, float nk_layout_row_dynamic(context, console->text_region_height, 1); int edit_flags = NK_EDIT_GOTO_END_ON_ACTIVATE | NK_EDIT_FIELD | NK_EDIT_SIG_ENTER; nk_edit_focus(context, edit_flags); - int edit_state = nk_edit_string_zero_terminated(context, edit_flags, console->console_command_text, MAX_CONSOLE_COMMAND_LEN, console_filter); + int edit_state = nk_edit_string_zero_terminated(context, edit_flags, console->console_command_text, MAX_CONSOLE_MESSAGE_LEN, console_filter); if(edit_state & NK_EDIT_COMMITED) { log_message("New message entered : %s", console->console_command_text); - memset(console->console_command_text, '\0', MAX_CONSOLE_COMMAND_LEN); + memset(console->console_command_text, '\0', MAX_CONSOLE_MESSAGE_LEN); } } nk_end(context); @@ -78,4 +82,11 @@ int console_filter(const struct nk_text_edit *box, nk_rune unicode) return nk_false; else return nk_true; +} + +void console_on_log_message(struct Console* console, const char* message, va_list args) +{ + if(++console->current_message_index >= MAX_CONSOLE_MESSAGES) + console->current_message_index = 0; + vsnprintf(console->console_messages[console->current_message_index], MAX_CONSOLE_MESSAGE_LEN, message, args); } \ No newline at end of file diff --git a/src/libsymmetry/console.h b/src/libsymmetry/console.h index dfb4d96..5658fd5 100644 --- a/src/libsymmetry/console.h +++ b/src/libsymmetry/console.h @@ -2,23 +2,26 @@ #define CONSOLE_H #include +#include -#define MAX_CONSOLE_COMMAND_LEN 128 -#define MAX_CONSOLE_LINES 1024 -#define MAX_CONSOLE_LINE_LEN 256 +#define MAX_CONSOLE_MESSAGE_LEN 256 +#define MAX_CONSOLE_MESSAGES 1024 struct Console { bool visible; float text_region_height; float line_height; - char console_command_text[MAX_CONSOLE_COMMAND_LEN]; + int current_message_index; + char console_command_text[MAX_CONSOLE_MESSAGE_LEN]; + char console_messages[MAX_CONSOLE_MESSAGES][MAX_CONSOLE_MESSAGE_LEN]; }; void console_init(struct Console* console); void console_toggle(struct Console* console); void console_update(struct Console* console, struct Gui_State* gui_state, float dt); void console_destroy(struct Console* console); +void console_on_log_message(struct Console* console, const char* message, va_list args); #endif \ No newline at end of file diff --git a/src/libsymmetry/game.c b/src/libsymmetry/game.c index 9a3b4aa..57dd873 100644 --- a/src/libsymmetry/game.c +++ b/src/libsymmetry/game.c @@ -48,6 +48,7 @@ static void game_render(void); static void game_debug(float dt); static void game_debug_gui(float dt); static void game_scene_setup(void); +static void game_on_log_message(const char* message, va_list args); static void on_box_move(Rigidbody body); static void on_collision_test(struct Entity* this_ent, struct Entity* other_ent, Rigidbody body, Rigidbody body2); @@ -80,6 +81,8 @@ bool game_init(struct Window* window, struct Platform_Api* platform_api) game_state->console = calloc(1, sizeof(*game_state->console)); log_file_handle_set(platform->log.file_handle_get()); + log_message_callback_set(game_on_log_message); + if(!gl_load_extentions()) { log_error("game:init", "Failed to load GL extentions"); @@ -1789,4 +1792,9 @@ void on_collision_test(struct Entity* this_ent, struct Entity* other_ent, Rigidb //platform->physics.body_force_add(body, 0.f, -100.f, 0.f); } //platform->physics.body_force_add(body, 0.f, 500.f, 0.f); +} + +void game_on_log_message(const char* message, va_list args) +{ + console_on_log_message(game_state->console, message, args); } \ No newline at end of file