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

Add new get_stack function to get GDScript stack trace as array #18976

Merged
merged 1 commit into from
Jul 3, 2018

Conversation

chanon
Copy link
Contributor

@chanon chanon commented May 17, 2018

As an alternative or addition to #18966, this adds a new function get_stack() that returns the stack trace as an array that looks like:

[
	{ "source": "res://path/to/file.gd", "function": "_name_of_function_a", "line": 34 },
	{ "source": "res://path/to/file.gd", "function": "_name_of_function_that_called_function_a", "line": 45 }
]

This allows creation of custom print/debugging/tracing/logging methods with custom formats in GDScript.

Implements #13335

@chanon
Copy link
Contributor Author

chanon commented May 17, 2018

Just to show how I've used the get_stack method in this pull request to create a customized print method:

With a print method that looks like this:

func debug(text):
	var frame = get_stack()[1]
	print( "%30s:%-4d %s" % [frame.source.get_file(), frame.line, text] )

My Output window logs now look like this:
image

With ability to customize and get additional debug info, I think this will greatly help with productivity.

@chanon
Copy link
Contributor Author

chanon commented May 17, 2018

Actually, another option (or another thing that could be added) is to expose the debug_get_stack_level_xxx methods directly to GDScript. But it isn't clear how/where they should be exposed as I don't think GDScriptLanguage::get_singleton is exposed to GDScript.

This get_stack method is still convenient though and is a good alternative to print_stack

@akien-mga akien-mga added this to the 3.1 milestone May 17, 2018
@mhilbrunner mhilbrunner closed this Jul 3, 2018
@mhilbrunner mhilbrunner reopened this Jul 3, 2018
@mhilbrunner mhilbrunner merged commit 5885e1c into godotengine:master Jul 3, 2018
@chanon chanon mentioned this pull request Jul 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants