feat: Added ability to print globals as part of debug messages (#561)
Co-authored-by: Balloonpopper <balloonpopper@git.com>
This commit is contained in:
@@ -19,6 +19,8 @@ var crash_savegame_filename
|
|||||||
# Did we crash already?
|
# Did we crash already?
|
||||||
onready var crashed = false
|
onready var crashed = false
|
||||||
|
|
||||||
|
# Regex to match globals in debug strings
|
||||||
|
var globals_regex: RegEx
|
||||||
|
|
||||||
# Valid log levels
|
# Valid log levels
|
||||||
enum { LOG_ERROR, LOG_WARNING, LOG_INFO, LOG_DEBUG, LOG_TRACE }
|
enum { LOG_ERROR, LOG_WARNING, LOG_INFO, LOG_DEBUG, LOG_TRACE }
|
||||||
@@ -54,9 +56,31 @@ func _init():
|
|||||||
File.WRITE
|
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) -> 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
|
# 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
|
# #### Parameters
|
||||||
#
|
#
|
||||||
# * string: Text to log
|
# * string: Text to log
|
||||||
@@ -64,23 +88,32 @@ func _init():
|
|||||||
func trace(string: String, args = []):
|
func trace(string: String, args = []):
|
||||||
if _get_log_level() >= LOG_TRACE and !crashed:
|
if _get_log_level() >= LOG_TRACE and !crashed:
|
||||||
var argsstr = str(args) if !args.empty() else ""
|
var argsstr = str(args) if !args.empty() else ""
|
||||||
|
string = _replace_globals(string)
|
||||||
_log("(T)\t" + string + " \t" + argsstr)
|
_log("(T)\t" + string + " \t" + argsstr)
|
||||||
|
|
||||||
|
|
||||||
# Log a debug message
|
# 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
|
# #### Parameters
|
||||||
#
|
#
|
||||||
# * string: Text to log
|
# * string: Text to log
|
||||||
# * args: Additional information
|
# * args: Additional information
|
||||||
func debug(string: String, args = []):
|
func debug(string: String, args = []):
|
||||||
if _get_log_level() >= LOG_DEBUG and !crashed:
|
if _get_log_level() >= LOG_DEBUG and !crashed:
|
||||||
|
string = _replace_globals(string)
|
||||||
var argsstr = str(args) if !args.empty() else ""
|
var argsstr = str(args) if !args.empty() else ""
|
||||||
_log("(D)\t" + string + " \t" + argsstr)
|
_log("(D)\t" + string + " \t" + argsstr)
|
||||||
|
|
||||||
|
|
||||||
# Log an info message
|
# 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
|
# #### Parameters
|
||||||
#
|
#
|
||||||
# * string: Text to log
|
# * string: Text to log
|
||||||
@@ -95,11 +128,16 @@ func info(string: String, args = []):
|
|||||||
argsstr.append(p.global_id)
|
argsstr.append(p.global_id)
|
||||||
else:
|
else:
|
||||||
argsstr.append(str(arg))
|
argsstr.append(str(arg))
|
||||||
|
string = _replace_globals(string)
|
||||||
_log("(I)\t" + string + " \t" + str(argsstr))
|
_log("(I)\t" + string + " \t" + str(argsstr))
|
||||||
|
|
||||||
|
|
||||||
# Log a warning message
|
# 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
|
# #### Parameters
|
||||||
#
|
#
|
||||||
# * string: Text to log
|
# * string: Text to log
|
||||||
@@ -107,6 +145,7 @@ func info(string: String, args = []):
|
|||||||
func warning(string: String, args = []):
|
func warning(string: String, args = []):
|
||||||
if _get_log_level() >= LOG_WARNING and !crashed:
|
if _get_log_level() >= LOG_WARNING and !crashed:
|
||||||
var argsstr = str(args) if !args.empty() else ""
|
var argsstr = str(args) if !args.empty() else ""
|
||||||
|
string = _replace_globals(string)
|
||||||
_log("(W)\t" + string + " \t" + argsstr, true)
|
_log("(W)\t" + string + " \t" + argsstr, true)
|
||||||
|
|
||||||
if escoria.project_settings_manager.get_setting(
|
if escoria.project_settings_manager.get_setting(
|
||||||
@@ -130,6 +169,10 @@ func warning(string: String, args = []):
|
|||||||
|
|
||||||
# Log an error message
|
# 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
|
# #### Parameters
|
||||||
#
|
#
|
||||||
# * string: Text to log
|
# * string: Text to log
|
||||||
@@ -137,6 +180,7 @@ func warning(string: String, args = []):
|
|||||||
func error(string: String, args = [], do_savegame: bool = true):
|
func error(string: String, args = [], do_savegame: bool = true):
|
||||||
if _get_log_level() >= LOG_ERROR and !crashed:
|
if _get_log_level() >= LOG_ERROR and !crashed:
|
||||||
var argsstr = str(args) if !args.empty() else ""
|
var argsstr = str(args) if !args.empty() else ""
|
||||||
|
string = _replace_globals(string)
|
||||||
_log("(E)\t" + string + " \t" + argsstr, true)
|
_log("(E)\t" + string + " \t" + argsstr, true)
|
||||||
|
|
||||||
if escoria.project_settings_manager.get_setting(
|
if escoria.project_settings_manager.get_setting(
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
:look
|
:look
|
||||||
say player "That button must activate the bridge, but it is broken." [r3_button_broken]
|
say player "That button must activate the bridge, but it is broken." [r3_button_broken]
|
||||||
say player "It should work now." [!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
|
:push
|
||||||
say player "I must USE this."
|
say player "I must USE this."
|
||||||
|
|||||||
Reference in New Issue
Block a user