Skip to content

Commit

Permalink
Fix logger causing crash at exit
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitalita committed Jan 13, 2024
1 parent 23e1c33 commit 76221e5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
9 changes: 7 additions & 2 deletions utility/gdre_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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()) {
Expand Down Expand Up @@ -70,6 +71,10 @@ void GDRELogger::close_file() {
}
}

void GDRELogger::_disable() {
disabled = true;
}

GDRELogger::GDRELogger() {}
GDRELogger::~GDRELogger() {
close_file();
Expand Down
2 changes: 2 additions & 0 deletions utility/gdre_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
class GDRELogger : public Logger {
Ref<FileAccess> 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();
Expand Down
6 changes: 5 additions & 1 deletion utility/gdre_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions utility/gdre_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ref<PackedFileInfo>> files;
String _get_res_path(const String &p_path, const String &resource_dir, const bool suppress_errors);
Expand Down

0 comments on commit 76221e5

Please sign in to comment.