From e53f2b27ba9d74ceff06d82971e8d007e49a638f Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 16 Nov 2024 10:28:17 +0100 Subject: [PATCH] Added force printing / logging option to OS. Made PLogger use this internally. --- core/io/logger.cpp | 36 ++++++++------------ core/io/logger.h | 12 +++---- core/log/logger.cpp | 9 +++-- core/os/os.cpp | 22 ++++++++++++ core/os/os.h | 6 ++++ core/string/print_string.cpp | 31 +++++++++++++++++ core/string/print_string.h | 4 +++ drivers/unix/os_unix.cpp | 4 +-- drivers/unix/os_unix.h | 2 +- drivers/unix/syslog_logger.cpp | 8 ++--- drivers/unix/syslog_logger.h | 4 +-- platform/android/os_android.cpp | 2 +- platform/osx/os_osx.mm | 4 +-- platform/windows/windows_terminal_logger.cpp | 8 ++--- platform/windows/windows_terminal_logger.h | 4 +-- 15 files changed, 105 insertions(+), 51 deletions(-) diff --git a/core/io/logger.cpp b/core/io/logger.cpp index bc3fff213..ce5bd51ed 100644 --- a/core/io/logger.cpp +++ b/core/io/logger.cpp @@ -62,8 +62,8 @@ void Logger::set_flush_stdout_on_print(bool value) { _flush_stdout_on_print = value; } -void Logger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) { - if (!should_log(true)) { +void Logger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type, bool p_force) { + if (!p_force && !should_log(true)) { return; } @@ -98,27 +98,19 @@ void Logger::log_error(const char *p_function, const char *p_file, int p_line, c } void Logger::logf(const char *p_format, ...) { - if (!should_log(false)) { - return; - } - va_list argp; va_start(argp, p_format); - logv(p_format, argp, false); + logv(p_format, argp, false, false); va_end(argp); } void Logger::logf_error(const char *p_format, ...) { - if (!should_log(true)) { - return; - } - va_list argp; va_start(argp, p_format); - logv(p_format, argp, true); + logv(p_format, argp, true, false); va_end(argp); } @@ -202,8 +194,8 @@ RotatedFileLogger::RotatedFileLogger(const String &p_base_path, int p_max_files) rotate_file(); } -void RotatedFileLogger::logv(const char *p_format, va_list p_list, bool p_err) { - if (!should_log(p_err)) { +void RotatedFileLogger::logv(const char *p_format, va_list p_list, bool p_err, bool p_force) { + if (!p_force && !should_log(p_err)) { return; } @@ -237,8 +229,8 @@ RotatedFileLogger::~RotatedFileLogger() { close_file(); } -void StdLogger::logv(const char *p_format, va_list p_list, bool p_err) { - if (!should_log(p_err)) { +void StdLogger::logv(const char *p_format, va_list p_list, bool p_err, bool p_force) { + if (!p_force && !should_log(p_err)) { return; } @@ -260,26 +252,26 @@ CompositeLogger::CompositeLogger(Vector p_loggers) : loggers(p_loggers) { } -void CompositeLogger::logv(const char *p_format, va_list p_list, bool p_err) { - if (!should_log(p_err)) { +void CompositeLogger::logv(const char *p_format, va_list p_list, bool p_err, bool p_force) { + if (!p_force && !should_log(p_err)) { return; } for (int i = 0; i < loggers.size(); ++i) { va_list list_copy; va_copy(list_copy, p_list); - loggers[i]->logv(p_format, list_copy, p_err); + loggers[i]->logv(p_format, list_copy, p_err, p_force); va_end(list_copy); } } -void CompositeLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) { - if (!should_log(true)) { +void CompositeLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type, bool p_force) { + if (!p_force && !should_log(true)) { return; } for (int i = 0; i < loggers.size(); ++i) { - loggers[i]->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type); + loggers[i]->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type, p_force); } } diff --git a/core/io/logger.h b/core/io/logger.h index a59d75148..32d516f74 100644 --- a/core/io/logger.h +++ b/core/io/logger.h @@ -54,8 +54,8 @@ public: static void set_flush_stdout_on_print(bool value); - 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); + virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) _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, bool p_force = false); void logf(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; void logf_error(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; @@ -68,7 +68,7 @@ public: */ class StdLogger : public Logger { public: - virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0; + virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) _PRINTF_FORMAT_ATTRIBUTE_2_0; virtual ~StdLogger(); }; @@ -91,7 +91,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) _PRINTF_FORMAT_ATTRIBUTE_2_0; + virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) _PRINTF_FORMAT_ATTRIBUTE_2_0; virtual ~RotatedFileLogger(); }; @@ -102,8 +102,8 @@ class CompositeLogger : public Logger { public: CompositeLogger(Vector p_loggers); - 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); + virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) _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, bool p_force = false); void add_logger(Logger *p_logger); diff --git a/core/log/logger.cpp b/core/log/logger.cpp index 9b0f22168..7830c51ac 100644 --- a/core/log/logger.cpp +++ b/core/log/logger.cpp @@ -282,7 +282,7 @@ void PLogger::do_log_trace(const String &str) { if (_backend.is_valid()) { _backend->log_trace(str); } else { - print_line(str); + force_print_line(str); } } @@ -290,7 +290,7 @@ void PLogger::do_log_message(const String &str) { if (_backend.is_valid()) { _backend->log_message(str); } else { - print_line(str); + force_print_line(str); } } @@ -298,7 +298,7 @@ void PLogger::do_log_warning(const String &str) { if (_backend.is_valid()) { _backend->log_warning(str); } else { - print_line(str); + force_print_line(str); } } @@ -306,8 +306,7 @@ void PLogger::do_log_error(const String &str) { if (_backend.is_valid()) { _backend->log_error(str); } else { - ERR_PRINT(str); - //print_error(str); + force_print_error(str); } } diff --git a/core/os/os.cpp b/core/os/os.cpp index 859503ed8..c31d15873 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -246,6 +246,28 @@ void OS::printerr(const char *p_format, ...) { va_end(argp); }; +void OS::log_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->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type, true); +} + +void OS::log(const char *p_format, ...) { + va_list argp; + va_start(argp, p_format); + + _logger->logv(p_format, argp, false, true); + + va_end(argp); +}; + +void OS::logerr(const char *p_format, ...) { + va_list argp; + va_start(argp, p_format); + + _logger->logv(p_format, argp, true, true); + + va_end(argp); +}; + void OS::set_keep_screen_on(bool p_enabled) { _keep_screen_on = p_enabled; } diff --git a/core/os/os.h b/core/os/os.h index 31691fc6b..a1cbee9b2 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -186,10 +186,16 @@ public: virtual void global_menu_remove_item(const String &p_menu, int p_idx) {}; virtual void global_menu_clear(const String &p_menu) {}; + // Depens on settings if logged / printed 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, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; void printerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; + // Will always get logged / printed + void log_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 log(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; + void logerr(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() = 0; diff --git a/core/string/print_string.cpp b/core/string/print_string.cpp index dfac77825..c517e9931 100644 --- a/core/string/print_string.cpp +++ b/core/string/print_string.cpp @@ -78,6 +78,7 @@ void print_line(String p_string) { OS::get_singleton()->print("%s\n", p_string.utf8().get_data()); _global_lock(); + PrintHandlerList *l = print_handler_list; while (l) { l->printfunc(l->userdata, p_string, false); @@ -95,6 +96,7 @@ void print_error(String p_string) { OS::get_singleton()->printerr("%s\n", p_string.utf8().get_data()); _global_lock(); + PrintHandlerList *l = print_handler_list; while (l) { l->printfunc(l->userdata, p_string, true); @@ -109,3 +111,32 @@ void print_verbose(String p_string) { print_line(p_string); } } + +void force_print_line(String p_string) { + OS::get_singleton()->log("%s\n", p_string.utf8().get_data()); + + _global_lock(); + + PrintHandlerList *l = print_handler_list; + while (l) { + l->printfunc(l->userdata, p_string, false); + l = l->next; + } + + _global_unlock(); +} + +void force_print_error(String p_string) { + OS::get_singleton()->logerr("%s\n", p_string.utf8().get_data()); + + _global_lock(); + + PrintHandlerList *l = print_handler_list; + while (l) { + l->printfunc(l->userdata, p_string, true); + l = l->next; + } + + _global_unlock(); +} + diff --git a/core/string/print_string.h b/core/string/print_string.h index ee9126e2f..317df62b3 100644 --- a/core/string/print_string.h +++ b/core/string/print_string.h @@ -56,8 +56,12 @@ void remove_print_handler(PrintHandlerList *p_handler); extern bool _print_line_enabled; extern bool _print_error_enabled; + extern void print_line(String p_string); extern void print_error(String p_string); extern void print_verbose(String p_string); +extern void force_print_line(String p_string); +extern void force_print_error(String p_string); + #endif diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 7e72fbb3c..edf7c91e1 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -521,8 +521,8 @@ String OS_Unix::get_executable_path() const { #endif } -void UnixTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) { - if (!should_log(true)) { +void UnixTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type, bool p_force) { + if (!p_force && !should_log(true)) { return; } diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h index 4da8a0e68..654f345c3 100644 --- a/drivers/unix/os_unix.h +++ b/drivers/unix/os_unix.h @@ -103,7 +103,7 @@ public: class UnixTerminalLogger : public StdLogger { public: - 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); + 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, bool p_force = false); virtual ~UnixTerminalLogger(); }; diff --git a/drivers/unix/syslog_logger.cpp b/drivers/unix/syslog_logger.cpp index 3495d4af0..01ea519b5 100644 --- a/drivers/unix/syslog_logger.cpp +++ b/drivers/unix/syslog_logger.cpp @@ -35,16 +35,16 @@ #include "core/string/print_string.h" #include -void SyslogLogger::logv(const char *p_format, va_list p_list, bool p_err) { - if (!should_log(p_err)) { +void SyslogLogger::logv(const char *p_format, va_list p_list, bool p_err, bool p_force) { + if (!p_force && !should_log(p_err)) { return; } vsyslog(p_err ? LOG_ERR : LOG_INFO, p_format, p_list); } -void SyslogLogger::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) { - if (!should_log(true)) { +void SyslogLogger::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type, bool p_force) { + if (!p_force && !should_log(true)) { return; } diff --git a/drivers/unix/syslog_logger.h b/drivers/unix/syslog_logger.h index 5944a4518..d827605b6 100644 --- a/drivers/unix/syslog_logger.h +++ b/drivers/unix/syslog_logger.h @@ -38,8 +38,8 @@ class SyslogLogger : public Logger { public: - 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 void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) _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 = ERR_ERROR, bool p_force = false); virtual ~SyslogLogger(); }; diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index 9683ebf7c..c38ec44f3 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -80,7 +80,7 @@ String _remove_symlink(const String &dir) { class AndroidLogger : 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, bool p_force = false) { __android_log_vprint(p_err ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "pandemonium", p_format, p_list); } diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index 9dfdc4bb8..ccd3514a5 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -1942,8 +1942,8 @@ String OS_OSX::get_name() const { #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 class OSXTerminalLogger : public StdLogger { public: - 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) { - if (!should_log(true)) { + 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, bool p_force) { + if (!p_force && !should_log(true)) { return; } diff --git a/platform/windows/windows_terminal_logger.cpp b/platform/windows/windows_terminal_logger.cpp index 6fe2b6414..de3026597 100644 --- a/platform/windows/windows_terminal_logger.cpp +++ b/platform/windows/windows_terminal_logger.cpp @@ -37,8 +37,8 @@ #define WIN32_LEAN_AND_MEAN #include -void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err) { - if (!should_log(p_err)) { +void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err, bool p_force) { + if (!p_force && !should_log(p_err)) { return; } @@ -73,8 +73,8 @@ void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_er #endif } -void WindowsTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) { - if (!should_log(true)) { +void WindowsTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type, bool p_force) { + if (!p_force && !should_log(true)) { return; } diff --git a/platform/windows/windows_terminal_logger.h b/platform/windows/windows_terminal_logger.h index f333ad8db..b50828e35 100644 --- a/platform/windows/windows_terminal_logger.h +++ b/platform/windows/windows_terminal_logger.h @@ -38,8 +38,8 @@ class WindowsTerminalLogger : public StdLogger { public: - virtual void logv(const char *p_format, va_list p_list, bool p_err); - 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); + virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false); + 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, bool p_force = false); virtual ~WindowsTerminalLogger(); };