From 650b1db4b823155925877930d7800ac7f59cc334 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Mon, 20 Sep 2021 19:14:27 +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. --- core/variant/variant_utility.cpp | 43 ++++++++++--------- doc/classes/@GlobalScope.xml | 5 +++ doc/classes/OS.xml | 2 +- doc/classes/ProjectSettings.xml | 2 +- .../doc_classes/VisualScriptBuiltinFunc.xml | 20 +++++---- .../visual_script_builtin_funcs.cpp | 15 ++++++- .../visual_script_builtin_funcs.h | 1 + 7 files changed, 56 insertions(+), 32 deletions(-) diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp index 232054d0caf..55c13760317 100644 --- a/core/variant/variant_utility.cpp +++ b/core/variant/variant_utility.cpp @@ -487,10 +487,6 @@ struct VariantUtilityFunctions { } static inline void print(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } String str; for (int i = 0; i < p_arg_count; i++) { String os = p_args[i]->operator String(); @@ -506,11 +502,29 @@ struct VariantUtilityFunctions { r_error.error = Callable::CallError::CALL_OK; } - static inline void printerr(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; + static inline void print_verbose(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (OS::get_singleton()->is_stdout_verbose()) { + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + // No need to use `print_verbose()` as this call already only happens + // when verbose mode is enabled. This avoids performing string argument concatenation + // when not needed. + print_line(str); } + + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void printerr(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { String str; for (int i = 0; i < p_arg_count; i++) { String os = p_args[i]->operator String(); @@ -527,10 +541,6 @@ struct VariantUtilityFunctions { } static inline void printt(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } String str; for (int i = 0; i < p_arg_count; i++) { if (i) { @@ -544,10 +554,6 @@ struct VariantUtilityFunctions { } static inline void prints(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } String str; for (int i = 0; i < p_arg_count; i++) { if (i) { @@ -561,10 +567,6 @@ struct VariantUtilityFunctions { } static inline void printraw(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } String str; for (int i = 0; i < p_arg_count; i++) { String os = p_args[i]->operator String(); @@ -1246,6 +1248,7 @@ void Variant::_register_variant_utility_functions() { FUNCBINDVARARGV(printt, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); FUNCBINDVARARGV(prints, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); FUNCBINDVARARGV(printraw, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(print_verbose, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); FUNCBINDVARARGV(push_error, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); FUNCBINDVARARGV(push_warning, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index c47ce816514..0334bab32a7 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -556,6 +556,11 @@ [b]Note:[/b] Consider using [method push_error] and [method push_warning] to print error and warning messages instead of [method print]. This distinguishes them from print messages used for debugging purposes, while also displaying a stack trace when an error or warning is printed. + + + 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. + + Prints one or more arguments to strings in the best way possible to standard error line. diff --git a/doc/classes/OS.xml b/doc/classes/OS.xml index 305258c8c52..8954469b4df 100644 --- a/doc/classes/OS.xml +++ b/doc/classes/OS.xml @@ -338,7 +338,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 @GlobalScope.print_verbose]. diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml index 22ed14743ae..3476d79559b 100644 --- a/doc/classes/ProjectSettings.xml +++ b/doc/classes/ProjectSettings.xml @@ -432,7 +432,7 @@ - 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 @GlobalScope.print_verbose]. Maximum call stack in visual scripting, to avoid infinite recursion. diff --git a/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml b/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml index 55d0b392fa6..ac6d8e79ec9 100644 --- a/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml +++ b/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml @@ -181,32 +181,34 @@ Print the given string to the standard output, without adding a newline. - + + + Serialize a [Variant] to a string. - + Deserialize a [Variant] from a string serialized using [constant VAR_TO_STR]. - + Serialize a [Variant] to a [PackedByteArray]. - + Deserialize a [Variant] from a [PackedByteArray] serialized using [constant VAR_TO_BYTES]. - + Return a number smoothly interpolated between the first two inputs, based on the third input. Similar to [constant MATH_LERP], but interpolates faster at the beginning and slower at the end. Using Hermite interpolation formula: [codeblock] var t = clamp((weight - from) / (to - from), 0.0, 1.0) return t * t * (3.0 - 2.0 * t) [/codeblock] - + - + - + - + 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 2bd7220d15b..7e01031128f 100644 --- a/modules/visual_script/visual_script_builtin_funcs.cpp +++ b/modules/visual_script/visual_script_builtin_funcs.cpp @@ -94,6 +94,7 @@ const char *VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX "print", "printerr", "printraw", + "print_verbose", "var2str", "str2var", "var2bytes", @@ -129,6 +130,7 @@ bool VisualScriptBuiltinFunc::has_input_sequence_port() const { case TEXT_PRINT: case TEXT_PRINTERR: case TEXT_PRINTRAW: + case TEXT_PRINT_VERBOSE: case MATH_SEED: return true; default: @@ -177,6 +179,7 @@ int VisualScriptBuiltinFunc::get_func_argument_count(BuiltinFunc p_func) { case TEXT_PRINT: case TEXT_PRINTERR: case TEXT_PRINTRAW: + case TEXT_PRINT_VERBOSE: case VAR_TO_STR: case STR_TO_VAR: case TYPE_EXISTS: @@ -223,6 +226,7 @@ int VisualScriptBuiltinFunc::get_output_value_port_count() const { case TEXT_PRINT: case TEXT_PRINTERR: case TEXT_PRINTRAW: + case TEXT_PRINT_VERBOSE: case MATH_SEED: return 0; case MATH_RANDSEED: @@ -424,7 +428,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: { @@ -572,6 +577,8 @@ PropertyInfo VisualScriptBuiltinFunc::get_output_value_port_info(int p_idx) cons } break; case TEXT_PRINTRAW: { } break; + case TEXT_PRINT_VERBOSE: { + } break; case VAR_TO_STR: { t = Variant::STRING; } break; @@ -1020,6 +1027,10 @@ void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func, const Variant **p_in OS::get_singleton()->print("%s", str.utf8().get_data()); } break; + case VisualScriptBuiltinFunc::TEXT_PRINT_VERBOSE: { + String str = *p_inputs[0]; + print_verbose(str); + } break; case VisualScriptBuiltinFunc::VAR_TO_STR: { String vars; VariantWriter::write_to_string(*p_inputs[0], vars); @@ -1208,6 +1219,7 @@ void VisualScriptBuiltinFunc::_bind_methods() { BIND_ENUM_CONSTANT(TEXT_PRINT); BIND_ENUM_CONSTANT(TEXT_PRINTERR); BIND_ENUM_CONSTANT(TEXT_PRINTRAW); + BIND_ENUM_CONSTANT(TEXT_PRINT_VERBOSE); BIND_ENUM_CONSTANT(VAR_TO_STR); BIND_ENUM_CONSTANT(STR_TO_VAR); BIND_ENUM_CONSTANT(VAR_TO_BYTES); @@ -1300,6 +1312,7 @@ void register_visual_script_builtin_func_node() { VisualScriptLanguage::singleton->add_register_func("functions/built_in/print", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/printerr", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/printraw", create_builtin_func_node); + VisualScriptLanguage::singleton->add_register_func("functions/built_in/print_verbose", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2str", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/str2var", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2bytes", 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 26abc1e479a..f9eb7e983fc 100644 --- a/modules/visual_script/visual_script_builtin_funcs.h +++ b/modules/visual_script/visual_script_builtin_funcs.h @@ -94,6 +94,7 @@ public: TEXT_PRINT, TEXT_PRINTERR, TEXT_PRINTRAW, + TEXT_PRINT_VERBOSE, VAR_TO_STR, STR_TO_VAR, VAR_TO_BYTES,