diff --git a/core/io/logger.h b/core/io/logger.h index 0b871a13dec..ff5b8ce489b 100644 --- a/core/io/logger.h +++ b/core/io/logger.h @@ -49,11 +49,11 @@ public: ERR_SHADER }; - virtual void logv(const char *p_format, va_list p_list, bool p_err) = 0; + virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0 = 0; virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR); - void logf(const char *p_format, ...); - void logf_error(const char *p_format, ...); + void logf(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; + void logf_error(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; virtual ~Logger(); }; @@ -64,7 +64,7 @@ public: class StdLogger : public Logger { public: - virtual void logv(const char *p_format, va_list p_list, bool p_err); + virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0; virtual ~StdLogger(); }; @@ -88,7 +88,7 @@ class RotatedFileLogger : public Logger { public: RotatedFileLogger(const String &p_base_path, int p_max_files = 10); - virtual void logv(const char *p_format, va_list p_list, bool p_err); + virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0; virtual ~RotatedFileLogger(); }; @@ -99,7 +99,7 @@ class CompositeLogger : public Logger { public: CompositeLogger(Vector p_loggers); - virtual void logv(const char *p_format, va_list p_list, bool p_err); + virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0; virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR); void add_logger(Logger *p_logger); diff --git a/core/os/os.h b/core/os/os.h index f58d6079371..d6541034fdc 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -148,8 +148,8 @@ public: static OS *get_singleton(); void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, Logger::ErrorType p_type = Logger::ERR_ERROR); - void print(const char *p_format, ...); - void printerr(const char *p_format, ...); + void print(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; + void printerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; virtual void alert(const String &p_alert, const String &p_title = "ALERT!") = 0; virtual String get_stdin_string(bool p_block = true) = 0; diff --git a/core/typedefs.h b/core/typedefs.h index e01e1c00b91..966360d4f2b 100644 --- a/core/typedefs.h +++ b/core/typedefs.h @@ -307,4 +307,12 @@ struct _GlobalLock { #define unlikely(x) x #endif +#if defined(__GNUC__) +#define _PRINTF_FORMAT_ATTRIBUTE_2_0 __attribute__((format(printf, 2, 0))) +#define _PRINTF_FORMAT_ATTRIBUTE_2_3 __attribute__((format(printf, 2, 3))) +#else +#define _PRINTF_FORMAT_ATTRIBUTE_2_0 +#define _PRINTF_FORMAT_ATTRIBUTE_2_3 +#endif + #endif // TYPEDEFS_H diff --git a/drivers/unix/syslog_logger.h b/drivers/unix/syslog_logger.h index 5cfe3964f64..49a34dacc31 100644 --- a/drivers/unix/syslog_logger.h +++ b/drivers/unix/syslog_logger.h @@ -37,7 +37,7 @@ class SyslogLogger : public Logger { public: - virtual void logv(const char *p_format, va_list p_list, bool p_err); + virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0; virtual void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type); virtual ~SyslogLogger(); diff --git a/main/main.cpp b/main/main.cpp index 2ad59fd3637..9b062d39515 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -757,7 +757,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph editor = false; #else String error_msg = "Error: Could not load game data at path '" + project_path + "'. Is the .pck file missing?\n"; - OS::get_singleton()->print(error_msg.ascii().get_data()); + OS::get_singleton()->print("%s", error_msg.ascii().get_data()); OS::get_singleton()->alert(error_msg); goto error; diff --git a/main/tests/test_string.cpp b/main/tests/test_string.cpp index edc4fb0c979..3465fd783e6 100644 --- a/main/tests/test_string.cpp +++ b/main/tests/test_string.cpp @@ -457,7 +457,7 @@ bool test_27() { state = s.begins_with(sb) == tc[i].expected; } if (!state) { - OS::get_singleton()->print("\n\t Failure on:\n\t\tstring: ", tc[i].data, "\n\t\tbegin: ", tc[i].begin, "\n\t\texpected: ", tc[i].expected ? "true" : "false", "\n"); + OS::get_singleton()->print("\n\t Failure on:\n\t\tstring: %s\n\t\tbegin: %s\n\t\texpected: %s\n", tc[i].data, tc[i].begin, tc[i].expected ? "true" : "false"); break; } }; diff --git a/modules/mono/editor/bindings_generator.cpp b/modules/mono/editor/bindings_generator.cpp index cffccd5cb5f..890bea0d1d5 100644 --- a/modules/mono/editor/bindings_generator.cpp +++ b/modules/mono/editor/bindings_generator.cpp @@ -703,7 +703,7 @@ Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const Str List &custom_icalls = itype.api_type == ClassDB::API_EDITOR ? editor_custom_icalls : core_custom_icalls; if (verbose_output) - OS::get_singleton()->print(String("Generating " + itype.proxy_name + ".cs...\n").utf8()); + OS::get_singleton()->print("Generating %s.cs...\n", itype.proxy_name.utf8().get_data()); String ctor_method(ICALL_PREFIX + itype.proxy_name + "_Ctor"); // Used only for derived types @@ -1280,7 +1280,7 @@ Error BindingsGenerator::generate_glue(const String &p_output_dir) { List &custom_icalls = itype.api_type == ClassDB::API_EDITOR ? editor_custom_icalls : core_custom_icalls; - OS::get_singleton()->print(String("Generating " + itype.name + "...\n").utf8()); + OS::get_singleton()->print("Generating %s...\n", itype.name.utf8().get_data()); String ctor_method(ICALL_PREFIX + itype.proxy_name + "_Ctor"); // Used only for derived types diff --git a/modules/mono/editor/script_class_parser.cpp b/modules/mono/editor/script_class_parser.cpp index 53a043b6750..fcc58c22e80 100644 --- a/modules/mono/editor/script_class_parser.cpp +++ b/modules/mono/editor/script_class_parser.cpp @@ -567,7 +567,7 @@ Error ScriptClassParser::parse(const String &p_code) { if (full_name.length()) full_name += "."; full_name += class_decl.name; - OS::get_singleton()->print(String("Ignoring generic class declaration: " + class_decl.name).utf8()); + OS::get_singleton()->print("%s", String("Ignoring generic class declaration: " + class_decl.name).utf8().get_data()); } } } else if (tk == TK_IDENTIFIER && String(value) == "struct") {