Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added ability to print globals as part of debug messages #561

Merged
merged 2 commits into from
Apr 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Added ability to print globals as part of debug messages
Balloonpopper committed Apr 3, 2022

Verified

This commit was signed with the committer’s verified signature. The key has expired.
renovate-bot Mend Renovate
commit 47cdf3e048bb3cb3c989af7600e1a8f8a62077dc
46 changes: 45 additions & 1 deletion addons/escoria-core/game/core-scripts/log/esc_logger.gd
Original file line number Diff line number Diff line change
@@ -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,33 +56,64 @@ 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
# * args: Additional information
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,18 +128,24 @@ 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
# * args: Additional information
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,13 +169,18 @@ 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
# * args: Additional information
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(
2 changes: 2 additions & 0 deletions game/rooms/room03/esc/button.esc
Original file line number Diff line number Diff line change
@@ -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."