From 76221e5a604a02404e2ac0966e078b962dde2854 Mon Sep 17 00:00:00 2001 From: nikitalita <69168929+nikitalita@users.noreply.github.com> Date: Fri, 12 Jan 2024 19:39:13 -0800 Subject: [PATCH] Fix logger causing crash at exit --- utility/gdre_logger.cpp | 9 +++++++-- utility/gdre_logger.h | 2 ++ utility/gdre_settings.cpp | 6 +++++- utility/gdre_settings.h | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/utility/gdre_logger.cpp b/utility/gdre_logger.cpp index 0d3a7b91..dae1666e 100644 --- a/utility/gdre_logger.cpp +++ b/utility/gdre_logger.cpp @@ -6,14 +6,14 @@ bool inGuiMode() { //check if we are in GUI mode - if (GodotREEditor::get_singleton() && GDRESettings::get_singleton() && !GDRESettings::get_singleton()->is_headless()) { + if (GDRESettings::get_singleton() && !GDRESettings::get_singleton()->is_headless()) { return true; } return false; } void GDRELogger::logv(const char *p_format, va_list p_list, bool p_err) { - if (!should_log(p_err)) { + if (disabled || !should_log(p_err)) { return; } if (file.is_valid() || inGuiMode()) { @@ -30,6 +30,7 @@ void GDRELogger::logv(const char *p_format, va_list p_list, bool p_err) { va_end(list_copy); if (inGuiMode()) { + // TODO: route this through GDRE Settings rather than GDRE Editor GodotREEditor::get_singleton()->call_deferred(SNAME("emit_signal"), "write_log_message", String(buf)); } if (file.is_valid()) { @@ -70,6 +71,10 @@ void GDRELogger::close_file() { } } +void GDRELogger::_disable() { + disabled = true; +} + GDRELogger::GDRELogger() {} GDRELogger::~GDRELogger() { close_file(); diff --git a/utility/gdre_logger.h b/utility/gdre_logger.h index 23143e18..750450b7 100644 --- a/utility/gdre_logger.h +++ b/utility/gdre_logger.h @@ -5,12 +5,14 @@ class GDRELogger : public Logger { Ref file; String base_path; + bool disabled = false; public: String get_path() { return base_path; }; GDRELogger(); Error open_file(const String &p_base_path); void close_file(); + void _disable(); // only used for during cleanup, because we can't remove the logger virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0; virtual ~GDRELogger(); diff --git a/utility/gdre_settings.cpp b/utility/gdre_settings.cpp index 5b6bb05c..a5717b54 100644 --- a/utility/gdre_settings.cpp +++ b/utility/gdre_settings.cpp @@ -159,6 +159,7 @@ GDRESettings::GDRESettings() { addCompatibilityClasses(); gdre_resource_path = ProjectSettings::get_singleton()->get_resource_path(); logger = memnew(GDRELogger); + headless = !RenderingServer::get_singleton() || RenderingServer::get_singleton()->get_video_adapter_name().is_empty(); add_logger(); } @@ -168,6 +169,7 @@ GDRESettings::~GDRESettings() { memdelete(new_singleton); } singleton = nullptr; + logger->_disable(); // logger doesn't get memdeleted because the OS singleton will do so } String GDRESettings::get_cwd() { @@ -928,7 +930,7 @@ String GDRESettings::get_log_file_path() { } bool GDRESettings::is_headless() const { - return RenderingServer::get_singleton()->get_video_adapter_name().is_empty(); + return headless; } String GDRESettings::get_sys_info_string() const { @@ -1183,6 +1185,8 @@ void GDRESettings::_bind_methods() { ClassDB::bind_method(D_METHOD("pack_has_project_config"), &GDRESettings::pack_has_project_config); ClassDB::bind_method(D_METHOD("get_gdre_version"), &GDRESettings::get_gdre_version); // ClassDB::bind_method(D_METHOD("get_auto_display_scale"), &GDRESettings::get_auto_display_scale); + // TODO: route this through GDRE Settings rather than GDRE Editor + //ADD_SIGNAL(MethodInfo("write_log_message", PropertyInfo(Variant::STRING, "message"))); } // This is at the bottom to account for the platform header files pulling in their respective OS headers and creating all sorts of issues diff --git a/utility/gdre_settings.h b/utility/gdre_settings.h index 372a555d..d3199415 100644 --- a/utility/gdre_settings.h +++ b/utility/gdre_settings.h @@ -107,6 +107,7 @@ class GDRESettings : public Object { String project_path = ""; static GDRESettings *singleton; static String exec_dir; + bool headless = false; void remove_current_pack(); Vector> files; String _get_res_path(const String &p_path, const String &resource_dir, const bool suppress_errors);