I am one of those developers, whose main tool for debugging is print messages sprinkled all over the code, so wrapping GameMaker’s show_debug_message()
in something short and sweet like Log()
is a must. However, I started running into situations where Log(variable)
would not be informative enough for me to quickly figure out where it was called from, so I decided to buff my main debugging function with options to print out call stack and context.
In the end, it looked something like this…
/// @function Log(_msg, _debug, _index, _depth = 3)
/// @description Print a nice and informative debug message with callstack
/// @param _msg Message to print
/// @param [_debug] Print callstack or not
/// @param [_index] Print object index
/// @param [_depth] Callstack depth
function Log(_msg, _debug = false, _index = false, _depth = 3) {
var _string = string(_msg);
if (_debug) {
var _stack = debug_get_callstack(_depth);
for (var i = 1; i < len(_stack) - 1; i++) {
_string = string_replace(_stack[i], "gml_Script_", "") + " -> " + _string;
}
}
if (_index && !is_struct(self)) {
_string = object_get_name(object_index) + ": " + _string;
}
show_debug_message(_string);
}
Which would result in a pretty informative console output as shown below.
Config: gml_Object_Config_Other_62:8 -> TranslationParseKeyData:98 -> Lines: 6
Readability could be improved by placing call stack function names in separate lines, but personally I prefer to see more information without scrolling.
Hope it will be useful to you and let me know if you have any ideas on how I could improve it further!