Skip to content

Commit

Permalink
Add print_verbose() built-in function to print in verbose mode only
Browse files Browse the repository at this point in the history
This can be used as a shorthand for:

    if OS.is_stdout_verbose():
        print("...")

Unlike `print_debug()`, this works in release builds too and can
be toggled off in debug builds.
  • Loading branch information
Calinou committed Jun 27, 2023
1 parent 29eeb46 commit cf1ea46
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@
<method name="is_stdout_verbose" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if the engine was executed with [code]-v[/code] (verbose stdout).
Returns [code]true[/code] if the engine was executed with the [code]--verbose[/code] or [code]-v[/code] command line argument, or if [member ProjectSettings.debug/settings/stdout/verbose_stdout] is [code]true[/code]. See also [method @GDScript.print_verbose].
</description>
</method>
<method name="is_userfs_persistent" qualifiers="const">
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@
Print frames per second to standard output every second.
</member>
<member name="debug/settings/stdout/verbose_stdout" type="bool" setter="" getter="" default="false">
Print more information to standard output when running. It displays information such as memory leaks, which scenes and resources are being loaded, etc.
Print more information to standard output when running. It displays information such as memory leaks, which scenes and resources are being loaded, etc. This can also be enabled using the [code]--verbose[/code] or [code]-v[/code] command line argument, even on an exported project. See also [method OS.is_stdout_verbose] and [method @GDScript.print_verbose].
</member>
<member name="debug/settings/visual_script/max_call_stack" type="int" setter="" getter="" default="1024">
Maximum call stack in visual scripting, to avoid infinite recursion.
Expand Down
6 changes: 6 additions & 0 deletions modules/gdscript/doc_classes/@GDScript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,12 @@
[b]Note:[/b] [method print_stack] only works if the running instance is connected to a debugging server (i.e. an editor instance). [method print_stack] will not work in projects exported in release mode, or in projects exported in debug mode if not connected to a debugging server.
</description>
</method>
<method name="print_verbose" qualifiers="vararg">
<return type="void" />
<description>
If verbose mode is enabled ([method OS.is_stdout_verbose] returning [code]true[/code]), converts one or more arguments of any type to string in the best way possible and prints them to the console.
</description>
</method>
<method name="printerr" qualifiers="vararg">
<return type="void" />
<description>
Expand Down
18 changes: 18 additions & 0 deletions modules/gdscript/gdscript_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const char *GDScriptFunctions::get_func_name(Function p_func) {
"printerr",
"printraw",
"print_debug",
"print_verbose",
"push_error",
"push_warning",
"var2str",
Expand Down Expand Up @@ -761,6 +762,16 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
print_line(str);
r_ret = Variant();
} break;
case TEXT_PRINT_VERBOSE: {
String str;
for (int i = 0; i < p_arg_count; i++) {
str += p_args[i]->operator String();
}

print_verbose(str);
r_ret = Variant();

} break;
case PUSH_ERROR: {
VALIDATE_ARG_COUNT(1);
if (p_args[0]->get_type() != Variant::STRING) {
Expand Down Expand Up @@ -1843,6 +1854,13 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
mi.flags |= METHOD_FLAG_VARARG;
return mi;

} break;
case TEXT_PRINT_VERBOSE: {
MethodInfo mi("print_verbose");
mi.return_val.type = Variant::NIL;
mi.flags |= METHOD_FLAG_VARARG;
return mi;

} break;
case PUSH_ERROR: {
MethodInfo mi(Variant::NIL, "push_error", PropertyInfo(Variant::STRING, ARGNAME("message")));
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class GDScriptFunctions {
TEXT_PRINTERR,
TEXT_PRINTRAW,
TEXT_PRINT_DEBUG,
TEXT_PRINT_VERBOSE,
PUSH_ERROR,
PUSH_WARNING,
VAR_TO_STR,
Expand Down
17 changes: 17 additions & 0 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,23 @@ public static void PrintRaw(params object[] what)
godot_icall_GD_printraw(GetPrintParams(what));
}

/// <summary>
/// If verbose mode is enabled (<see cref="OS.IsStdoutVerbose"/> returning <see langword="true"/>),
/// converts one or more arguments of any type to string in the best way possible
/// and prints them to the console.
/// </summary>
/// <example>
/// <code>
/// var a = new int[] { 1, 2, 3 };
/// GD.PrintVerbose("a", "b", a); // Prints ab[1, 2, 3] only if verbose output is enabled
/// </code>
/// </example>
/// <param name="what">Arguments that will be printed.</param>
public static void PrintVerbose(params object[] what)
{
godot_icall_GD_print_verbose(GetPrintParams(what));
}

/// <summary>
/// Prints one or more arguments to the console with a space between each argument.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions modules/mono/glue/gd_glue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ void godot_icall_GD_printraw(MonoArray *p_what) {
OS::get_singleton()->print("%s", str.utf8().get_data());
}

void godot_icall_GD_print_verbose(MonoArray *p_what) {
String str;
int length = mono_array_length(p_what);

for (int i = 0; i < length; i++) {
MonoObject *elem = mono_array_get(p_what, MonoObject *, i);

MonoException *exc = NULL;
String elem_str = GDMonoMarshal::mono_object_to_variant_string(elem, &exc);

if (exc) {
GDMonoUtils::set_pending_exception(exc);
return;
}

str += elem_str;
}

print_verbose(str);
}

void godot_icall_GD_prints(MonoArray *p_what) {
String str;
int length = mono_array_length(p_what);
Expand Down Expand Up @@ -290,6 +311,7 @@ void godot_register_gd_icalls() {
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_print", godot_icall_GD_print);
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printerr", godot_icall_GD_printerr);
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printraw", godot_icall_GD_printraw);
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_print_verbose", godot_icall_GD_print_verbose);
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_prints", godot_icall_GD_prints);
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_printt", godot_icall_GD_printt);
GDMonoUtils::add_internal_call("Godot.GD::godot_icall_GD_randf", godot_icall_GD_randf);
Expand Down
2 changes: 2 additions & 0 deletions modules/mono/glue/gd_glue.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ void godot_icall_GD_printerr(MonoArray *p_what);

void godot_icall_GD_printraw(MonoArray *p_what);

void godot_icall_GD_print_verbose(MonoArray *p_what);

void godot_icall_GD_prints(MonoArray *p_what);

void godot_icall_GD_printt(MonoArray *p_what);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@
</constant>
<constant name="TEXT_ORD" value="67" enum="BuiltinFunc">
</constant>
<constant name="FUNC_MAX" value="68" enum="BuiltinFunc">
<constant name="TEXT_PRINT_VERBOSE" value="68" enum="BuiltinFunc">
If verbose mode is enabled ([method OS.is_stdout_verbose] returning [code]true[/code]), converts one or more arguments of any type to string in the best way possible and prints them to the console.
</constant>
<constant name="FUNC_MAX" value="69" enum="BuiltinFunc">
Represents the size of the [enum BuiltinFunc] enum.
</constant>
</constants>
Expand Down
16 changes: 15 additions & 1 deletion modules/visual_script/visual_script_builtin_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const char *VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX
"print",
"printerr",
"printraw",
"print_verbose",
"var2str",
"str2var",
"var2bytes",
Expand Down Expand Up @@ -135,6 +136,7 @@ bool VisualScriptBuiltinFunc::has_input_sequence_port() const {
case TEXT_PRINTERR:
case TEXT_PRINTRAW:
case MATH_SEED:
case TEXT_PRINT_VERBOSE:
return true;
default:
return false;
Expand Down Expand Up @@ -185,6 +187,7 @@ int VisualScriptBuiltinFunc::get_func_argument_count(BuiltinFunc p_func) {
case VAR_TO_STR:
case STR_TO_VAR:
case TYPE_EXISTS:
case TEXT_PRINT_VERBOSE:
return 1;
case VAR_TO_BYTES:
case BYTES_TO_VAR:
Expand Down Expand Up @@ -232,6 +235,7 @@ int VisualScriptBuiltinFunc::get_output_value_port_count() const {
case TEXT_PRINTERR:
case TEXT_PRINTRAW:
case MATH_SEED:
case TEXT_PRINT_VERBOSE:
return 0;
case MATH_RANDSEED:
return 2;
Expand Down Expand Up @@ -455,7 +459,8 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
case TEXT_STR:
case TEXT_PRINT:
case TEXT_PRINTERR:
case TEXT_PRINTRAW: {
case TEXT_PRINTRAW:
case TEXT_PRINT_VERBOSE: {
return PropertyInfo(Variant::NIL, "value");
} break;
case STR_TO_VAR: {
Expand Down Expand Up @@ -633,6 +638,8 @@ PropertyInfo VisualScriptBuiltinFunc::get_output_value_port_info(int p_idx) cons
case COLORN: {
t = Variant::COLOR;
} break;
case TEXT_PRINT_VERBOSE: {
} break;
case FUNC_MAX: {
}
}
Expand Down Expand Up @@ -1198,6 +1205,11 @@ void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func, const Variant **p_in

*r_return = String(color);

} break;
case VisualScriptBuiltinFunc::TEXT_PRINT_VERBOSE: {
String str = *p_inputs[0];
print_verbose(str);

} break;
default: {
}
Expand Down Expand Up @@ -1307,6 +1319,7 @@ void VisualScriptBuiltinFunc::_bind_methods() {
BIND_ENUM_CONSTANT(MATH_POSMOD);
BIND_ENUM_CONSTANT(MATH_LERP_ANGLE);
BIND_ENUM_CONSTANT(TEXT_ORD);
BIND_ENUM_CONSTANT(TEXT_PRINT_VERBOSE);
BIND_ENUM_CONSTANT(FUNC_MAX);
}

Expand Down Expand Up @@ -1399,4 +1412,5 @@ void register_visual_script_builtin_func_node() {
VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2bytes", create_builtin_func_node<VisualScriptBuiltinFunc::VAR_TO_BYTES>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/bytes2var", create_builtin_func_node<VisualScriptBuiltinFunc::BYTES_TO_VAR>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/color_named", create_builtin_func_node<VisualScriptBuiltinFunc::COLORN>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/print_verbose", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINT_VERBOSE>);
}
1 change: 1 addition & 0 deletions modules/visual_script/visual_script_builtin_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class VisualScriptBuiltinFunc : public VisualScriptNode {
MATH_POSMOD,
MATH_LERP_ANGLE,
TEXT_ORD,
TEXT_PRINT_VERBOSE,
FUNC_MAX
};

Expand Down

0 comments on commit cf1ea46

Please sign in to comment.