Merge pull request #52877 from Calinou/add-print-verbose

Add `print_verbose()` built-in function to print in verbose mode only
This commit is contained in:
Rémi Verschelde 2021-09-21 17:00:58 +02:00 committed by GitHub
commit 61ce8b206e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 32 deletions

View File

@ -487,10 +487,6 @@ struct VariantUtilityFunctions {
} }
static inline void print(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { 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; String str;
for (int i = 0; i < p_arg_count; i++) { for (int i = 0; i < p_arg_count; i++) {
String os = p_args[i]->operator String(); String os = p_args[i]->operator String();
@ -506,11 +502,29 @@ struct VariantUtilityFunctions {
r_error.error = Callable::CallError::CALL_OK; r_error.error = Callable::CallError::CALL_OK;
} }
static inline void printerr(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void print_verbose(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
if (p_arg_count < 1) { if (OS::get_singleton()->is_stdout_verbose()) {
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; String str;
r_error.argument = 1; 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; String str;
for (int i = 0; i < p_arg_count; i++) { for (int i = 0; i < p_arg_count; i++) {
String os = p_args[i]->operator String(); 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) { 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; String str;
for (int i = 0; i < p_arg_count; i++) { for (int i = 0; i < p_arg_count; i++) {
if (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) { 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; String str;
for (int i = 0; i < p_arg_count; i++) { for (int i = 0; i < p_arg_count; i++) {
if (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) { 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; String str;
for (int i = 0; i < p_arg_count; i++) { for (int i = 0; i < p_arg_count; i++) {
String os = p_args[i]->operator String(); 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(printt, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
FUNCBINDVARARGV(prints, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); FUNCBINDVARARGV(prints, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
FUNCBINDVARARGV(printraw, 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_error, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
FUNCBINDVARARGV(push_warning, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); FUNCBINDVARARGV(push_warning, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);

View File

@ -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. [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.
</description> </description>
</method> </method>
<method name="print_verbose" qualifiers="vararg">
<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"> <method name="printerr" qualifiers="vararg">
<description> <description>
Prints one or more arguments to strings in the best way possible to standard error line. Prints one or more arguments to strings in the best way possible to standard error line.

View File

@ -342,7 +342,7 @@
<method name="is_stdout_verbose" qualifiers="const"> <method name="is_stdout_verbose" qualifiers="const">
<return type="bool" /> <return type="bool" />
<description> <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 @GlobalScope.print_verbose].
</description> </description>
</method> </method>
<method name="is_userfs_persistent" qualifiers="const"> <method name="is_userfs_persistent" qualifiers="const">

View File

@ -432,7 +432,7 @@
<member name="debug/settings/stdout/print_gpu_profile" type="bool" setter="" getter="" default="false"> <member name="debug/settings/stdout/print_gpu_profile" type="bool" setter="" getter="" default="false">
</member> </member>
<member name="debug/settings/stdout/verbose_stdout" type="bool" setter="" getter="" default="false"> <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 @GlobalScope.print_verbose].
</member> </member>
<member name="debug/settings/visual_script/max_call_stack" type="int" setter="" getter="" default="1024"> <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. Maximum call stack in visual scripting, to avoid infinite recursion.

View File

@ -179,32 +179,34 @@
<constant name="TEXT_PRINTRAW" value="55" enum="BuiltinFunc"> <constant name="TEXT_PRINTRAW" value="55" enum="BuiltinFunc">
Print the given string to the standard output, without adding a newline. Print the given string to the standard output, without adding a newline.
</constant> </constant>
<constant name="VAR_TO_STR" value="56" enum="BuiltinFunc"> <constant name="TEXT_PRINT_VERBOSE" value="56" enum="BuiltinFunc">
</constant>
<constant name="VAR_TO_STR" value="57" enum="BuiltinFunc">
Serialize a [Variant] to a string. Serialize a [Variant] to a string.
</constant> </constant>
<constant name="STR_TO_VAR" value="57" enum="BuiltinFunc"> <constant name="STR_TO_VAR" value="58" enum="BuiltinFunc">
Deserialize a [Variant] from a string serialized using [constant VAR_TO_STR]. Deserialize a [Variant] from a string serialized using [constant VAR_TO_STR].
</constant> </constant>
<constant name="VAR_TO_BYTES" value="58" enum="BuiltinFunc"> <constant name="VAR_TO_BYTES" value="59" enum="BuiltinFunc">
Serialize a [Variant] to a [PackedByteArray]. Serialize a [Variant] to a [PackedByteArray].
</constant> </constant>
<constant name="BYTES_TO_VAR" value="59" enum="BuiltinFunc"> <constant name="BYTES_TO_VAR" value="60" enum="BuiltinFunc">
Deserialize a [Variant] from a [PackedByteArray] serialized using [constant VAR_TO_BYTES]. Deserialize a [Variant] from a [PackedByteArray] serialized using [constant VAR_TO_BYTES].
</constant> </constant>
<constant name="MATH_SMOOTHSTEP" value="60" enum="BuiltinFunc"> <constant name="MATH_SMOOTHSTEP" value="61" enum="BuiltinFunc">
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: 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] [codeblock]
var t = clamp((weight - from) / (to - from), 0.0, 1.0) var t = clamp((weight - from) / (to - from), 0.0, 1.0)
return t * t * (3.0 - 2.0 * t) return t * t * (3.0 - 2.0 * t)
[/codeblock] [/codeblock]
</constant> </constant>
<constant name="MATH_POSMOD" value="61" enum="BuiltinFunc"> <constant name="MATH_POSMOD" value="62" enum="BuiltinFunc">
</constant> </constant>
<constant name="MATH_LERP_ANGLE" value="62" enum="BuiltinFunc"> <constant name="MATH_LERP_ANGLE" value="63" enum="BuiltinFunc">
</constant> </constant>
<constant name="TEXT_ORD" value="63" enum="BuiltinFunc"> <constant name="TEXT_ORD" value="64" enum="BuiltinFunc">
</constant> </constant>
<constant name="FUNC_MAX" value="64" enum="BuiltinFunc"> <constant name="FUNC_MAX" value="65" enum="BuiltinFunc">
Represents the size of the [enum BuiltinFunc] enum. Represents the size of the [enum BuiltinFunc] enum.
</constant> </constant>
</constants> </constants>

View File

@ -94,6 +94,7 @@ const char *VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX
"print", "print",
"printerr", "printerr",
"printraw", "printraw",
"print_verbose",
"var2str", "var2str",
"str2var", "str2var",
"var2bytes", "var2bytes",
@ -129,6 +130,7 @@ bool VisualScriptBuiltinFunc::has_input_sequence_port() const {
case TEXT_PRINT: case TEXT_PRINT:
case TEXT_PRINTERR: case TEXT_PRINTERR:
case TEXT_PRINTRAW: case TEXT_PRINTRAW:
case TEXT_PRINT_VERBOSE:
case MATH_SEED: case MATH_SEED:
return true; return true;
default: default:
@ -177,6 +179,7 @@ int VisualScriptBuiltinFunc::get_func_argument_count(BuiltinFunc p_func) {
case TEXT_PRINT: case TEXT_PRINT:
case TEXT_PRINTERR: case TEXT_PRINTERR:
case TEXT_PRINTRAW: case TEXT_PRINTRAW:
case TEXT_PRINT_VERBOSE:
case VAR_TO_STR: case VAR_TO_STR:
case STR_TO_VAR: case STR_TO_VAR:
case TYPE_EXISTS: case TYPE_EXISTS:
@ -223,6 +226,7 @@ int VisualScriptBuiltinFunc::get_output_value_port_count() const {
case TEXT_PRINT: case TEXT_PRINT:
case TEXT_PRINTERR: case TEXT_PRINTERR:
case TEXT_PRINTRAW: case TEXT_PRINTRAW:
case TEXT_PRINT_VERBOSE:
case MATH_SEED: case MATH_SEED:
return 0; return 0;
case MATH_RANDSEED: case MATH_RANDSEED:
@ -424,7 +428,8 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
case TEXT_STR: case TEXT_STR:
case TEXT_PRINT: case TEXT_PRINT:
case TEXT_PRINTERR: case TEXT_PRINTERR:
case TEXT_PRINTRAW: { case TEXT_PRINTRAW:
case TEXT_PRINT_VERBOSE: {
return PropertyInfo(Variant::NIL, "value"); return PropertyInfo(Variant::NIL, "value");
} break; } break;
case STR_TO_VAR: { case STR_TO_VAR: {
@ -572,6 +577,8 @@ PropertyInfo VisualScriptBuiltinFunc::get_output_value_port_info(int p_idx) cons
} break; } break;
case TEXT_PRINTRAW: { case TEXT_PRINTRAW: {
} break; } break;
case TEXT_PRINT_VERBOSE: {
} break;
case VAR_TO_STR: { case VAR_TO_STR: {
t = Variant::STRING; t = Variant::STRING;
} break; } 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()); OS::get_singleton()->print("%s", str.utf8().get_data());
} break; } break;
case VisualScriptBuiltinFunc::TEXT_PRINT_VERBOSE: {
String str = *p_inputs[0];
print_verbose(str);
} break;
case VisualScriptBuiltinFunc::VAR_TO_STR: { case VisualScriptBuiltinFunc::VAR_TO_STR: {
String vars; String vars;
VariantWriter::write_to_string(*p_inputs[0], 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_PRINT);
BIND_ENUM_CONSTANT(TEXT_PRINTERR); BIND_ENUM_CONSTANT(TEXT_PRINTERR);
BIND_ENUM_CONSTANT(TEXT_PRINTRAW); BIND_ENUM_CONSTANT(TEXT_PRINTRAW);
BIND_ENUM_CONSTANT(TEXT_PRINT_VERBOSE);
BIND_ENUM_CONSTANT(VAR_TO_STR); BIND_ENUM_CONSTANT(VAR_TO_STR);
BIND_ENUM_CONSTANT(STR_TO_VAR); BIND_ENUM_CONSTANT(STR_TO_VAR);
BIND_ENUM_CONSTANT(VAR_TO_BYTES); 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<VisualScriptBuiltinFunc::TEXT_PRINT>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/print", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINT>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/printerr", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINTERR>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/printerr", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINTERR>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/printraw", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINTRAW>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/printraw", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINTRAW>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/print_verbose", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINT_VERBOSE>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2str", create_builtin_func_node<VisualScriptBuiltinFunc::VAR_TO_STR>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2str", create_builtin_func_node<VisualScriptBuiltinFunc::VAR_TO_STR>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/str2var", create_builtin_func_node<VisualScriptBuiltinFunc::STR_TO_VAR>); VisualScriptLanguage::singleton->add_register_func("functions/built_in/str2var", create_builtin_func_node<VisualScriptBuiltinFunc::STR_TO_VAR>);
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/var2bytes", create_builtin_func_node<VisualScriptBuiltinFunc::VAR_TO_BYTES>);

View File

@ -94,6 +94,7 @@ public:
TEXT_PRINT, TEXT_PRINT,
TEXT_PRINTERR, TEXT_PRINTERR,
TEXT_PRINTRAW, TEXT_PRINTRAW,
TEXT_PRINT_VERBOSE,
VAR_TO_STR, VAR_TO_STR,
STR_TO_VAR, STR_TO_VAR,
VAR_TO_BYTES, VAR_TO_BYTES,