From e53f2b27ba9d74ceff06d82971e8d007e49a638f Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 16 Nov 2024 10:28:17 +0100 Subject: [PATCH 1/5] 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(); }; From 642db983282634689bf484cf12f0c06f5f4c31d6 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 16 Nov 2024 10:44:48 +0100 Subject: [PATCH 2/5] Added a new important log level to PLogger. --- core/bind/logger_bind.cpp | 6 ++++ core/bind/logger_bind.h | 2 ++ core/log/logger.cpp | 69 +++++++++++++++++++++++++++++++++++++ core/log/logger.h | 10 ++++++ core/log/logger_backend.cpp | 9 +++++ core/log/logger_backend.h | 2 ++ 6 files changed, 98 insertions(+) diff --git a/core/bind/logger_bind.cpp b/core/bind/logger_bind.cpp index 5b05b3e53..dc37b6419 100644 --- a/core/bind/logger_bind.cpp +++ b/core/bind/logger_bind.cpp @@ -56,6 +56,10 @@ void _PLogger::log_error(const String &str) { PLogger::log_error(str); } +void _PLogger::log_important(const String &str) { + PLogger::log_important(str); +} + _PLogger::LogLevel _PLogger::get_log_level() { return static_cast(static_cast(PLogger::get_log_level())); } @@ -83,6 +87,7 @@ void _PLogger::_bind_methods() { ClassDB::bind_method(D_METHOD("log_message", "str"), &_PLogger::log_message); ClassDB::bind_method(D_METHOD("log_warning", "str"), &_PLogger::log_warning); ClassDB::bind_method(D_METHOD("log_error", "str"), &_PLogger::log_error); + ClassDB::bind_method(D_METHOD("log_important", "str"), &_PLogger::log_important); ClassDB::bind_method(D_METHOD("get_log_level"), &_PLogger::get_log_level); ClassDB::bind_method(D_METHOD("set_log_level", "log_level"), &_PLogger::set_log_level); @@ -92,6 +97,7 @@ void _PLogger::_bind_methods() { BIND_ENUM_CONSTANT(LOG_LEVEL_MESSAGE); BIND_ENUM_CONSTANT(LOG_LEVEL_WARNING); BIND_ENUM_CONSTANT(LOG_LEVEL_ERROR); + BIND_ENUM_CONSTANT(LOG_LEVEL_IMPORTANT); BIND_ENUM_CONSTANT(LOG_LEVEL_NONE); } diff --git a/core/bind/logger_bind.h b/core/bind/logger_bind.h index a58c762b3..5815b1bbb 100644 --- a/core/bind/logger_bind.h +++ b/core/bind/logger_bind.h @@ -48,6 +48,7 @@ public: LOG_LEVEL_MESSAGE, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR, + LOG_LEVEL_IMPORTANT, LOG_LEVEL_NONE, }; @@ -58,6 +59,7 @@ public: void log_message(const String &str); void log_warning(const String &str); void log_error(const String &str); + void log_important(const String &str); LogLevel get_log_level(); void set_log_level(const LogLevel p_log_level); diff --git a/core/log/logger.cpp b/core/log/logger.cpp index 7830c51ac..4d8f55c49 100644 --- a/core/log/logger.cpp +++ b/core/log/logger.cpp @@ -278,6 +278,67 @@ void PLogger::log_error(const char *p_function, const char *p_file, int p_line, do_log_error(s); } +void PLogger::log_important(const String &str) { + if (_log_level > LOG_LEVEL_IMPORTANT) { + return; + } + + String s; + s += "I "; + s += str; + //s += "\n"; + + do_log_important(s); +} +void PLogger::log_important(const char *str) { + if (_log_level > LOG_LEVEL_IMPORTANT) { + return; + } + + String s; + s += "I "; + s += str; + //s += "\n"; + + do_log_important(s); +} +void PLogger::log_important(const char *p_function, const char *p_file, int p_line, const char *str) { + if (_log_level > LOG_LEVEL_IMPORTANT) { + return; + } + + String s; + s += "I | "; + s += p_file; + s += "::"; + s += p_function; + s += ":"; + s += String::num(p_line); + s += " | "; + s += str; + //s += "\n"; + + do_log_important(s); +} +void PLogger::log_important(const char *p_function, const char *p_file, int p_line, const String &str) { + if (_log_level > LOG_LEVEL_IMPORTANT) { + return; + } + + String s; + s += "I | "; + s += p_file; + s += "::"; + s += p_function; + s += ":"; + s += String::num(p_line); + s += " | "; + s += str; + //s += "\n"; + + do_log_important(s); +} + void PLogger::do_log_trace(const String &str) { if (_backend.is_valid()) { _backend->log_trace(str); @@ -310,6 +371,14 @@ void PLogger::do_log_error(const String &str) { } } +void PLogger::do_log_important(const String &str) { + if (_backend.is_valid()) { + _backend->log_important(str); + } else { + force_print_error(str); + } +} + PLogger::LogLevel PLogger::get_log_level() { return _log_level; } diff --git a/core/log/logger.h b/core/log/logger.h index 494cddf8e..ef24fb796 100644 --- a/core/log/logger.h +++ b/core/log/logger.h @@ -51,6 +51,9 @@ class String; #define PLOG_ERR(str) \ PLogger::log_error(__FUNCTION__, __FILE__, __LINE__, str); +#define PLOG_IMPORTANT(str) \ + PLogger::log_important(__FUNCTION__, __FILE__, __LINE__, str); + class PLogger : public Object { public: enum LogLevel { @@ -58,6 +61,7 @@ public: LOG_LEVEL_MESSAGE, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR, + LOG_LEVEL_IMPORTANT, LOG_LEVEL_NONE, }; @@ -81,10 +85,16 @@ public: static void log_error(const char *p_function, const char *p_file, int p_line, const char *str); static void log_error(const char *p_function, const char *p_file, int p_line, const String &str); + static void log_important(const String &str); + static void log_important(const char *str); + static void log_important(const char *p_function, const char *p_file, int p_line, const char *str); + static void log_important(const char *p_function, const char *p_file, int p_line, const String &str); + static void do_log_trace(const String &str); static void do_log_message(const String &str); static void do_log_warning(const String &str); static void do_log_error(const String &str); + static void do_log_important(const String &str); static LogLevel get_log_level(); static void set_log_level(const LogLevel p_log_level); diff --git a/core/log/logger_backend.cpp b/core/log/logger_backend.cpp index 74f17fb9c..fee4bb57e 100644 --- a/core/log/logger_backend.cpp +++ b/core/log/logger_backend.cpp @@ -45,6 +45,9 @@ void LoggerBackend::log_warning(const String &str) { void LoggerBackend::log_error(const String &str) { call("_log_error", str); } +void LoggerBackend::log_important(const String &str) { + call("_log_important", str); +} void LoggerBackend::_log_trace(const String &str) { print_line(str); @@ -58,6 +61,9 @@ void LoggerBackend::_log_warning(const String &str) { void LoggerBackend::_log_error(const String &str) { print_error(str); } +void LoggerBackend::_log_important(const String &str) { + print_error(str); +} LoggerBackend::LoggerBackend() { } @@ -70,14 +76,17 @@ void LoggerBackend::_bind_methods() { BIND_VMETHOD(MethodInfo("_log_message", PropertyInfo(Variant::STRING, "str"))); BIND_VMETHOD(MethodInfo("_log_warning", PropertyInfo(Variant::STRING, "str"))); BIND_VMETHOD(MethodInfo("_log_error", PropertyInfo(Variant::STRING, "str"))); + BIND_VMETHOD(MethodInfo("_log_important", PropertyInfo(Variant::STRING, "str"))); ClassDB::bind_method(D_METHOD("log_trace", "str"), &LoggerBackend::log_trace); ClassDB::bind_method(D_METHOD("log_message", "str"), &LoggerBackend::log_message); ClassDB::bind_method(D_METHOD("log_warning", "str"), &LoggerBackend::log_warning); ClassDB::bind_method(D_METHOD("log_error", "str"), &LoggerBackend::log_error); + ClassDB::bind_method(D_METHOD("log_important", "str"), &LoggerBackend::log_important); ClassDB::bind_method(D_METHOD("_log_trace", "str"), &LoggerBackend::_log_trace); ClassDB::bind_method(D_METHOD("_log_message", "str"), &LoggerBackend::_log_message); ClassDB::bind_method(D_METHOD("_log_warning", "str"), &LoggerBackend::_log_warning); ClassDB::bind_method(D_METHOD("_log_error", "str"), &LoggerBackend::_log_error); + ClassDB::bind_method(D_METHOD("_log_important", "str"), &LoggerBackend::_log_important); } diff --git a/core/log/logger_backend.h b/core/log/logger_backend.h index c0fb69fa8..7a5eb38e4 100644 --- a/core/log/logger_backend.h +++ b/core/log/logger_backend.h @@ -45,11 +45,13 @@ public: virtual void log_message(const String &str); virtual void log_warning(const String &str); virtual void log_error(const String &str); + virtual void log_important(const String &str); virtual void _log_trace(const String &str); virtual void _log_message(const String &str); virtual void _log_warning(const String &str); virtual void _log_error(const String &str); + virtual void _log_important(const String &str); LoggerBackend(); ~LoggerBackend(); From f4917d1ec49d34bf17152bad43d342072d7aea2e Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 16 Nov 2024 10:46:53 +0100 Subject: [PATCH 3/5] Fix osx compile. --- platform/osx/os_osx.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index ccd3514a5..7a25f1ee4 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -1942,7 +1942,7 @@ 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, bool p_force) { + 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) { if (!p_force && !should_log(true)) { return; } From 618e22306a4e323100c90e83fb3aecb1dd7038ec Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 17 Nov 2024 14:51:44 +0100 Subject: [PATCH 4/5] Added log_custom method to PLogger. It's a lot more customizable than the others by design. Also small cleanups. --- core/bind/logger_bind.cpp | 5 ++ core/bind/logger_bind.h | 1 + core/log/logger.cpp | 145 +++++++++++++++++++++++++++++++----- core/log/logger.h | 8 ++ core/log/logger_backend.cpp | 35 +++++++-- core/log/logger_backend.h | 2 + 6 files changed, 171 insertions(+), 25 deletions(-) diff --git a/core/bind/logger_bind.cpp b/core/bind/logger_bind.cpp index dc37b6419..a4f8d832a 100644 --- a/core/bind/logger_bind.cpp +++ b/core/bind/logger_bind.cpp @@ -60,6 +60,10 @@ void _PLogger::log_important(const String &str) { PLogger::log_important(str); } +void _PLogger::log_custom(const StringName &p_category, const int p_level, const String &str) { + PLogger::log_custom(p_category, p_level, str); +} + _PLogger::LogLevel _PLogger::get_log_level() { return static_cast(static_cast(PLogger::get_log_level())); } @@ -88,6 +92,7 @@ void _PLogger::_bind_methods() { ClassDB::bind_method(D_METHOD("log_warning", "str"), &_PLogger::log_warning); ClassDB::bind_method(D_METHOD("log_error", "str"), &_PLogger::log_error); ClassDB::bind_method(D_METHOD("log_important", "str"), &_PLogger::log_important); + ClassDB::bind_method(D_METHOD("log_custom", "category", "level", "str"), &_PLogger::log_custom); ClassDB::bind_method(D_METHOD("get_log_level"), &_PLogger::get_log_level); ClassDB::bind_method(D_METHOD("set_log_level", "log_level"), &_PLogger::set_log_level); diff --git a/core/bind/logger_bind.h b/core/bind/logger_bind.h index 5815b1bbb..dff108c0d 100644 --- a/core/bind/logger_bind.h +++ b/core/bind/logger_bind.h @@ -60,6 +60,7 @@ public: void log_warning(const String &str); void log_error(const String &str); void log_important(const String &str); + void log_custom(const StringName &p_category, const int p_level, const String &str); LogLevel get_log_level(); void set_log_level(const LogLevel p_log_level); diff --git a/core/log/logger.cpp b/core/log/logger.cpp index 4d8f55c49..f3d8dfdf4 100644 --- a/core/log/logger.cpp +++ b/core/log/logger.cpp @@ -42,7 +42,6 @@ void PLogger::log_trace(const String &str) { String s; s += "T "; s += str; - //s += "\n"; do_log_trace(s); } @@ -54,7 +53,6 @@ void PLogger::log_trace(const char *str) { String s; s += "T "; s += str; - //s += "\n"; do_log_trace(s); } @@ -72,7 +70,6 @@ void PLogger::log_trace(const char *p_function, const char *p_file, int p_line, s += String::num(p_line); s += " | "; s += str; - //s += "\n"; do_log_trace(s); } @@ -90,7 +87,6 @@ void PLogger::log_trace(const char *p_function, const char *p_file, int p_line, s += String::num(p_line); s += " | "; s += str; - //s += "\n"; do_log_trace(s); } @@ -103,7 +99,6 @@ void PLogger::log_message(const String &str) { String s; s += "M "; s += str; - //s += "\n"; do_log_message(s); } @@ -115,7 +110,6 @@ void PLogger::log_message(const char *str) { String s; s += "M "; s += str; - //s += "\n"; do_log_message(s); } @@ -133,7 +127,6 @@ void PLogger::log_message(const char *p_function, const char *p_file, int p_line s += String::num(p_line); s += " | "; s += str; - //s += "\n"; do_log_message(s); } @@ -151,7 +144,6 @@ void PLogger::log_message(const char *p_function, const char *p_file, int p_line s += String::num(p_line); s += " | "; s += str; - //s += "\n"; do_log_message(s); } @@ -164,7 +156,6 @@ void PLogger::log_warning(const String &str) { String s; s += "W "; s += str; - //s += "\n"; do_log_warning(s); } @@ -176,7 +167,6 @@ void PLogger::log_warning(const char *str) { String s; s += "W "; s += str; - //s += "\n"; do_log_warning(s); } @@ -194,7 +184,6 @@ void PLogger::log_warning(const char *p_function, const char *p_file, int p_line s += String::num(p_line); s += " | "; s += str; - //s += "\n"; do_log_warning(s); } @@ -212,7 +201,6 @@ void PLogger::log_warning(const char *p_function, const char *p_file, int p_line s += String::num(p_line); s += " | "; s += str; - //s += "\n"; do_log_warning(s); } @@ -225,7 +213,6 @@ void PLogger::log_error(const String &str) { String s; s += "E "; s += str; - //s += "\n"; do_log_error(s); } @@ -237,7 +224,6 @@ void PLogger::log_error(const char *str) { String s; s += "E "; s += str; - //s += "\n"; do_log_error(s); } @@ -255,7 +241,6 @@ void PLogger::log_error(const char *p_function, const char *p_file, int p_line, s += String::num(p_line); s += " | "; s += str; - //s += "\n"; do_log_error(s); } @@ -273,7 +258,6 @@ void PLogger::log_error(const char *p_function, const char *p_file, int p_line, s += String::num(p_line); s += " | "; s += str; - //s += "\n"; do_log_error(s); } @@ -286,7 +270,6 @@ void PLogger::log_important(const String &str) { String s; s += "I "; s += str; - //s += "\n"; do_log_important(s); } @@ -298,7 +281,6 @@ void PLogger::log_important(const char *str) { String s; s += "I "; s += str; - //s += "\n"; do_log_important(s); } @@ -316,7 +298,6 @@ void PLogger::log_important(const char *p_function, const char *p_file, int p_li s += String::num(p_line); s += " | "; s += str; - //s += "\n"; do_log_important(s); } @@ -334,11 +315,135 @@ void PLogger::log_important(const char *p_function, const char *p_file, int p_li s += String::num(p_line); s += " | "; s += str; - //s += "\n"; do_log_important(s); } +void PLogger::log_custom(const StringName &p_category, const int p_level, const String &str) { + if (_backend.is_valid()) { + _backend->log_custom(p_category, p_level, str); + return; + } + + if (_log_level > p_level) { + return; + } + + String s; + s += p_category; + s += " : "; + s += String::num(p_level); + s += " | "; + s += str; + + if (p_level < LOG_LEVEL_ERROR) { + force_print_line(s); + } else { + force_print_error(s); + } +} +void PLogger::log_custom(const StringName &p_category, const int p_level, const char *str) { + if (_backend.is_valid()) { + _backend->log_custom(p_category, p_level, str); + return; + } + + if (_log_level > p_level) { + return; + } + + String s; + s += p_category; + s += " : "; + s += String::num(p_level); + s += " | "; + s += str; + + if (p_level < LOG_LEVEL_ERROR) { + force_print_line(s); + } else { + force_print_error(s); + } +} +void PLogger::log_custom(const StringName &p_category, const int p_level, const char *p_function, const char *p_file, int p_line, const char *str) { + if (_backend.is_valid()) { + + String s; + s += p_file; + s += "::"; + s += p_function; + s += ":"; + s += String::num(p_line); + s += " | "; + s += str; + + _backend->log_custom(p_category, p_level, s); + return; + } + + if (_log_level > p_level) { + return; + } + + String s; + s += p_category; + s += " : "; + s += String::num(p_level); + s += " | "; + s += p_file; + s += "::"; + s += p_function; + s += ":"; + s += String::num(p_line); + s += " | "; + s += str; + + if (p_level < LOG_LEVEL_ERROR) { + force_print_line(s); + } else { + force_print_error(s); + } +} +void PLogger::log_custom(const StringName &p_category, const int p_level, const char *p_function, const char *p_file, int p_line, const String &str) { + if (_backend.is_valid()) { + + String s; + s += p_file; + s += "::"; + s += p_function; + s += ":"; + s += String::num(p_line); + s += " | "; + s += str; + + _backend->log_custom(p_category, p_level, s); + return; + } + + if (_log_level > p_level) { + return; + } + + String s; + s += p_category; + s += " : "; + s += String::num(p_level); + s += " | "; + s += p_file; + s += "::"; + s += p_function; + s += ":"; + s += String::num(p_line); + s += " | "; + s += str; + + if (p_level < LOG_LEVEL_ERROR) { + force_print_line(s); + } else { + force_print_error(s); + } +} + void PLogger::do_log_trace(const String &str) { if (_backend.is_valid()) { _backend->log_trace(str); diff --git a/core/log/logger.h b/core/log/logger.h index ef24fb796..071b4031c 100644 --- a/core/log/logger.h +++ b/core/log/logger.h @@ -54,6 +54,9 @@ class String; #define PLOG_IMPORTANT(str) \ PLogger::log_important(__FUNCTION__, __FILE__, __LINE__, str); +#define PLOG_CUSTOM(category, level, str) \ + PLogger::log_important(category, level, __FUNCTION__, __FILE__, __LINE__, str); + class PLogger : public Object { public: enum LogLevel { @@ -90,6 +93,11 @@ public: static void log_important(const char *p_function, const char *p_file, int p_line, const char *str); static void log_important(const char *p_function, const char *p_file, int p_line, const String &str); + static void log_custom(const StringName &p_category, const int p_level, const String &p_str); + static void log_custom(const StringName &p_category, const int p_level, const char *str); + static void log_custom(const StringName &p_category, const int p_level, const char *p_function, const char *p_file, int p_line, const char *str); + static void log_custom(const StringName &p_category, const int p_level, const char *p_function, const char *p_file, int p_line, const String &str); + static void do_log_trace(const String &str); static void do_log_message(const String &str); static void do_log_warning(const String &str); diff --git a/core/log/logger_backend.cpp b/core/log/logger_backend.cpp index fee4bb57e..f0033b291 100644 --- a/core/log/logger_backend.cpp +++ b/core/log/logger_backend.cpp @@ -31,6 +31,8 @@ #include "logger_backend.h" +#include "core/io/logger.h" +#include "core/log/logger.h" #include "core/string/print_string.h" void LoggerBackend::log_trace(const String &str) { @@ -48,21 +50,38 @@ void LoggerBackend::log_error(const String &str) { void LoggerBackend::log_important(const String &str) { call("_log_important", str); } +void LoggerBackend::log_custom(const StringName &p_category, const int p_level, const String &str) { + call("_log_custom", p_category, p_level, str); +} void LoggerBackend::_log_trace(const String &str) { - print_line(str); + force_print_line(str); } void LoggerBackend::_log_message(const String &str) { - print_line(str); + force_print_line(str); } void LoggerBackend::_log_warning(const String &str) { - print_line(str); + force_print_line(str); } void LoggerBackend::_log_error(const String &str) { - print_error(str); + force_print_error(str); } void LoggerBackend::_log_important(const String &str) { - print_error(str); + force_print_error(str); +} +void LoggerBackend::_log_custom(const StringName &p_category, const int p_level, const String &str) { + String s; + s += p_category; + s += " : "; + s += String::num(p_level); + s += " | "; + s += str; + + if (p_level < PLogger::LOG_LEVEL_ERROR) { + force_print_line(s); + } else { + force_print_error(s); + } } LoggerBackend::LoggerBackend() { @@ -77,16 +96,22 @@ void LoggerBackend::_bind_methods() { BIND_VMETHOD(MethodInfo("_log_warning", PropertyInfo(Variant::STRING, "str"))); BIND_VMETHOD(MethodInfo("_log_error", PropertyInfo(Variant::STRING, "str"))); BIND_VMETHOD(MethodInfo("_log_important", PropertyInfo(Variant::STRING, "str"))); + BIND_VMETHOD(MethodInfo("_log_custom", + PropertyInfo(Variant::STRING_NAME, "category"), + PropertyInfo(Variant::INT, "level"), + PropertyInfo(Variant::STRING, "str"))); ClassDB::bind_method(D_METHOD("log_trace", "str"), &LoggerBackend::log_trace); ClassDB::bind_method(D_METHOD("log_message", "str"), &LoggerBackend::log_message); ClassDB::bind_method(D_METHOD("log_warning", "str"), &LoggerBackend::log_warning); ClassDB::bind_method(D_METHOD("log_error", "str"), &LoggerBackend::log_error); ClassDB::bind_method(D_METHOD("log_important", "str"), &LoggerBackend::log_important); + ClassDB::bind_method(D_METHOD("log_custom", "category", "level", "str"), &LoggerBackend::log_custom); ClassDB::bind_method(D_METHOD("_log_trace", "str"), &LoggerBackend::_log_trace); ClassDB::bind_method(D_METHOD("_log_message", "str"), &LoggerBackend::_log_message); ClassDB::bind_method(D_METHOD("_log_warning", "str"), &LoggerBackend::_log_warning); ClassDB::bind_method(D_METHOD("_log_error", "str"), &LoggerBackend::_log_error); ClassDB::bind_method(D_METHOD("_log_important", "str"), &LoggerBackend::_log_important); + ClassDB::bind_method(D_METHOD("_log_custom", "category", "level", "str"), &LoggerBackend::log_custom); } diff --git a/core/log/logger_backend.h b/core/log/logger_backend.h index 7a5eb38e4..52cc30d71 100644 --- a/core/log/logger_backend.h +++ b/core/log/logger_backend.h @@ -46,12 +46,14 @@ public: virtual void log_warning(const String &str); virtual void log_error(const String &str); virtual void log_important(const String &str); + virtual void log_custom(const StringName &p_category, const int p_level, const String &str); virtual void _log_trace(const String &str); virtual void _log_message(const String &str); virtual void _log_warning(const String &str); virtual void _log_error(const String &str); virtual void _log_important(const String &str); + virtual void _log_custom(const StringName &p_category, const int p_level, const String &str); LoggerBackend(); ~LoggerBackend(); From e7e91f7aa258d91b3e16a8db9850c4682cafcc86 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 17 Nov 2024 16:34:22 +0100 Subject: [PATCH 5/5] Fix typo. --- core/log/logger.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/log/logger.h b/core/log/logger.h index 071b4031c..c9cd76edc 100644 --- a/core/log/logger.h +++ b/core/log/logger.h @@ -55,7 +55,7 @@ class String; PLogger::log_important(__FUNCTION__, __FILE__, __LINE__, str); #define PLOG_CUSTOM(category, level, str) \ - PLogger::log_important(category, level, __FUNCTION__, __FILE__, __LINE__, str); + PLogger::log_custom(category, level, __FUNCTION__, __FILE__, __LINE__, str); class PLogger : public Object { public: