From 47cdf3e048bb3cb3c989af7600e1a8f8a62077dc Mon Sep 17 00:00:00 2001 From: Balloonpopper Date: Sun, 3 Apr 2022 10:55:05 +1000 Subject: [PATCH 1/2] feat: Added ability to print globals as part of debug messages --- .../game/core-scripts/log/esc_logger.gd | 46 ++++++++++++++++++- game/rooms/room03/esc/button.esc | 2 + 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/addons/escoria-core/game/core-scripts/log/esc_logger.gd b/addons/escoria-core/game/core-scripts/log/esc_logger.gd index d860121e1..749c8bfa9 100644 --- a/addons/escoria-core/game/core-scripts/log/esc_logger.gd +++ b/addons/escoria-core/game/core-scripts/log/esc_logger.gd @@ -19,6 +19,8 @@ var crash_savegame_filename # Did we crash already? onready var crashed = false +# Regex to match globals in debug strings +var globals_regex: RegEx # Valid log levels enum { LOG_ERROR, LOG_WARNING, LOG_INFO, LOG_DEBUG, LOG_TRACE } @@ -54,9 +56,31 @@ func _init(): File.WRITE ) + # Use look-ahead/behind to capture the term in braces + globals_regex = RegEx.new() + globals_regex.compile("(?<=\\{)(.*)(?=\\})") + +# Look to see if any globals (names in braces) should be interpreted +# +# #### Parameters +# +# * string: Text to log +func _replace_globals(string: String): + for result in globals_regex.search_all(string): + var globresult = escoria.globals_manager.get_global( + str(result.get_string()) + ) + string = string.replace( + "{" + result.get_string() + "}", str(globresult) + ) + return string # Log a trace message # +# Global variables can be substituted into the text by wrapping the global +# name in braces. +# e.g. trace "There are {coin_count} coins remaining". +# # #### Parameters # # * string: Text to log @@ -64,23 +88,32 @@ func _init(): func trace(string: String, args = []): if _get_log_level() >= LOG_TRACE and !crashed: var argsstr = str(args) if !args.empty() else "" + string = _replace_globals(string) _log("(T)\t" + string + " \t" + argsstr) # Log a debug message # +# Global variables can be substituted into the text by wrapping the global +# name in braces. +# e.g. debug "There are {coin_count} coins remaining". +# # #### Parameters # # * string: Text to log # * args: Additional information func debug(string: String, args = []): if _get_log_level() >= LOG_DEBUG and !crashed: + string = _replace_globals(string) var argsstr = str(args) if !args.empty() else "" _log("(D)\t" + string + " \t" + argsstr) - # Log an info message # +# Global variables can be substituted into the text by wrapping the global +# name in braces. +# e.g. info "There are {coin_count} coins remaining". +# # #### Parameters # # * string: Text to log @@ -95,11 +128,16 @@ func info(string: String, args = []): argsstr.append(p.global_id) else: argsstr.append(str(arg)) + string = _replace_globals(string) _log("(I)\t" + string + " \t" + str(argsstr)) # Log a warning message # +# Global variables can be substituted into the text by wrapping the global +# name in braces. +# e.g. warning "There are {coin_count} coins remaining". +# # #### Parameters # # * string: Text to log @@ -107,6 +145,7 @@ func info(string: String, args = []): func warning(string: String, args = []): if _get_log_level() >= LOG_WARNING and !crashed: var argsstr = str(args) if !args.empty() else "" + string = _replace_globals(string) _log("(W)\t" + string + " \t" + argsstr, true) if escoria.project_settings_manager.get_setting( @@ -130,6 +169,10 @@ func warning(string: String, args = []): # Log an error message # +# Global variables can be substituted into the text by wrapping the global +# name in braces. +# e.g. error "There are {coin_count} coins remaining". +# # #### Parameters # # * string: Text to log @@ -137,6 +180,7 @@ func warning(string: String, args = []): func error(string: String, args = [], do_savegame: bool = true): if _get_log_level() >= LOG_ERROR and !crashed: var argsstr = str(args) if !args.empty() else "" + string = _replace_globals(string) _log("(E)\t" + string + " \t" + argsstr, true) if escoria.project_settings_manager.get_setting( diff --git a/game/rooms/room03/esc/button.esc b/game/rooms/room03/esc/button.esc index f04a0a9b8..f1887d672 100755 --- a/game/rooms/room03/esc/button.esc +++ b/game/rooms/room03/esc/button.esc @@ -1,6 +1,8 @@ :look say player "That button must activate the bridge, but it is broken." [r3_button_broken] say player "It should work now." [!r3_button_broken] +# Demonstrate printing globals as part of debug messages +debug "r3_button_broken is currently {r3_button_broken}" :push say player "I must USE this." From 3e126901f60f864dba8904b29677217f94326985 Mon Sep 17 00:00:00 2001 From: balloonpopper <5151242+balloonpopper@users.noreply.github.com> Date: Sun, 3 Apr 2022 20:09:43 +1000 Subject: [PATCH 2/2] Update addons/escoria-core/game/core-scripts/log/esc_logger.gd Co-authored-by: Julian Murgia --- addons/escoria-core/game/core-scripts/log/esc_logger.gd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/escoria-core/game/core-scripts/log/esc_logger.gd b/addons/escoria-core/game/core-scripts/log/esc_logger.gd index 749c8bfa9..52b6b26f8 100644 --- a/addons/escoria-core/game/core-scripts/log/esc_logger.gd +++ b/addons/escoria-core/game/core-scripts/log/esc_logger.gd @@ -65,7 +65,7 @@ func _init(): # #### Parameters # # * string: Text to log -func _replace_globals(string: String): +func _replace_globals(string: String) -> String: for result in globals_regex.search_all(string): var globresult = escoria.globals_manager.get_global( str(result.get_string())