From cf1ea46757fd062c50cc5fa1b5fc5c23f3baafa8 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Sat, 9 Oct 2021 09:15:14 +0200 Subject: [PATCH] Add `print_verbose()` built-in function to print in verbose mode only 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. --- doc/classes/OS.xml | 2 +- doc/classes/ProjectSettings.xml | 2 +- modules/gdscript/doc_classes/@GDScript.xml | 6 +++++ modules/gdscript/gdscript_functions.cpp | 18 +++++++++++++++ modules/gdscript/gdscript_functions.h | 1 + .../glue/GodotSharp/GodotSharp/Core/GD.cs | 17 ++++++++++++++ modules/mono/glue/gd_glue.cpp | 22 +++++++++++++++++++ modules/mono/glue/gd_glue.h | 2 ++ .../doc_classes/VisualScriptBuiltinFunc.xml | 5 ++++- .../visual_script_builtin_funcs.cpp | 16 +++++++++++++- .../visual_script_builtin_funcs.h | 1 + 11 files changed, 88 insertions(+), 4 deletions(-) diff --git a/doc/classes/OS.xml b/doc/classes/OS.xml index e20f6c0c1a7c..a1ff204ae8ef 100644 --- a/doc/classes/OS.xml +++ b/doc/classes/OS.xml @@ -722,7 +722,7 @@ - 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]. diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml index b1f26cec6604..75e4a06bceaf 100644 --- a/doc/classes/ProjectSettings.xml +++ b/doc/classes/ProjectSettings.xml @@ -442,7 +442,7 @@ Print frames per second to standard output every second. - 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]. Maximum call stack in visual scripting, to avoid infinite recursion. diff --git a/modules/gdscript/doc_classes/@GDScript.xml b/modules/gdscript/doc_classes/@GDScript.xml index 2d7aed1f8fd4..af7f9926fd76 100644 --- a/modules/gdscript/doc_classes/@GDScript.xml +++ b/modules/gdscript/doc_classes/@GDScript.xml @@ -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. + + + + 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. + + diff --git a/modules/gdscript/gdscript_functions.cpp b/modules/gdscript/gdscript_functions.cpp index d677299cbb8c..05516357ee9d 100644 --- a/modules/gdscript/gdscript_functions.cpp +++ b/modules/gdscript/gdscript_functions.cpp @@ -113,6 +113,7 @@ const char *GDScriptFunctions::get_func_name(Function p_func) { "printerr", "printraw", "print_debug", + "print_verbose", "push_error", "push_warning", "var2str", @@ -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) { @@ -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"))); diff --git a/modules/gdscript/gdscript_functions.h b/modules/gdscript/gdscript_functions.h index 7343bd5463df..0f7a1bda86d3 100644 --- a/modules/gdscript/gdscript_functions.h +++ b/modules/gdscript/gdscript_functions.h @@ -105,6 +105,7 @@ class GDScriptFunctions { TEXT_PRINTERR, TEXT_PRINTRAW, TEXT_PRINT_DEBUG, + TEXT_PRINT_VERBOSE, PUSH_ERROR, PUSH_WARNING, VAR_TO_STR, diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs index 39ad33c9d638..7d11a90a0cad 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs @@ -323,6 +323,23 @@ public static void PrintRaw(params object[] what) godot_icall_GD_printraw(GetPrintParams(what)); } + /// + /// If verbose mode is enabled ( returning ), + /// converts one or more arguments of any type to string in the best way possible + /// and prints them to the console. + /// + /// + /// + /// var a = new int[] { 1, 2, 3 }; + /// GD.PrintVerbose("a", "b", a); // Prints ab[1, 2, 3] only if verbose output is enabled + /// + /// + /// Arguments that will be printed. + public static void PrintVerbose(params object[] what) + { + godot_icall_GD_print_verbose(GetPrintParams(what)); + } + /// /// Prints one or more arguments to the console with a space between each argument. /// diff --git a/modules/mono/glue/gd_glue.cpp b/modules/mono/glue/gd_glue.cpp index beabc6aea7ed..b881315a121e 100644 --- a/modules/mono/glue/gd_glue.cpp +++ b/modules/mono/glue/gd_glue.cpp @@ -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); @@ -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); diff --git a/modules/mono/glue/gd_glue.h b/modules/mono/glue/gd_glue.h index 06a201a84850..bc26a61bdd1d 100644 --- a/modules/mono/glue/gd_glue.h +++ b/modules/mono/glue/gd_glue.h @@ -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); diff --git a/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml b/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml index 5800902abfef..d1b5fcbb2327 100644 --- a/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml +++ b/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml @@ -219,7 +219,10 @@ - + + 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. + + Represents the size of the [enum BuiltinFunc] enum. diff --git a/modules/visual_script/visual_script_builtin_funcs.cpp b/modules/visual_script/visual_script_builtin_funcs.cpp index d3b6fba05cd8..63befe11a883 100644 --- a/modules/visual_script/visual_script_builtin_funcs.cpp +++ b/modules/visual_script/visual_script_builtin_funcs.cpp @@ -98,6 +98,7 @@ const char *VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX "print", "printerr", "printraw", + "print_verbose", "var2str", "str2var", "var2bytes", @@ -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; @@ -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: @@ -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; @@ -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: { @@ -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: { } } @@ -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: { } @@ -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); } @@ -1399,4 +1412,5 @@ void register_visual_script_builtin_func_node() { VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2bytes", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/bytes2var", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/color_named", create_builtin_func_node); + VisualScriptLanguage::singleton->add_register_func("functions/built_in/print_verbose", create_builtin_func_node); } diff --git a/modules/visual_script/visual_script_builtin_funcs.h b/modules/visual_script/visual_script_builtin_funcs.h index fac2f216f9cf..a8853e2debd9 100644 --- a/modules/visual_script/visual_script_builtin_funcs.h +++ b/modules/visual_script/visual_script_builtin_funcs.h @@ -106,6 +106,7 @@ class VisualScriptBuiltinFunc : public VisualScriptNode { MATH_POSMOD, MATH_LERP_ANGLE, TEXT_ORD, + TEXT_PRINT_VERBOSE, FUNC_MAX };