Skip to content

Commit

Permalink
Add a project setting to enable stdout flushing in release builds
Browse files Browse the repository at this point in the history
This can be used in server builds for journalctl compatibility.

(cherry picked from commit 341b9cf)

Fixes crash when exiting with --verbose with leaked resources

(cherry picked from commit 25c4dac)
  • Loading branch information
Calinou authored and akien-mga committed Feb 16, 2021
1 parent ece69f8 commit 15d9f77
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
20 changes: 11 additions & 9 deletions core/io/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "core/os/dir_access.h"
#include "core/os/os.h"
#include "core/print_string.h"
#include "core/project_settings.h"

// va_copy was defined in the C99, but not in C++ standards before C++11.
// When you compile C++ without --std=c++<XX> option, compilers still define
Expand Down Expand Up @@ -204,15 +205,14 @@ void RotatedFileLogger::logv(const char *p_format, va_list p_list, bool p_err) {
}
va_end(list_copy);
file->store_buffer((uint8_t *)buf, len);

if (len >= static_buf_size) {
Memory::free_static(buf);
}
#ifdef DEBUG_ENABLED
const bool need_flush = true;
#else
bool need_flush = p_err;
#endif
if (need_flush) {

if (p_err || !ProjectSettings::get_singleton() || GLOBAL_GET("application/run/flush_stdout_on_print")) {
// Don't always flush when printing stdout to avoid performance
// issues when `print()` is spammed in release builds.
file->flush();
}
}
Expand All @@ -231,9 +231,11 @@ void StdLogger::logv(const char *p_format, va_list p_list, bool p_err) {
vfprintf(stderr, p_format, p_list);
} else {
vprintf(p_format, p_list);
#ifdef DEBUG_ENABLED
fflush(stdout);
#endif
if (!ProjectSettings::get_singleton() || GLOBAL_GET("application/run/flush_stdout_on_print")) {
// Don't always flush when printing stdout to avoid performance
// issues when `print()` is spammed in release builds.
fflush(stdout);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@
<member name="application/run/disable_stdout" type="bool" setter="" getter="" default="false">
If [code]true[/code], disables printing to standard output in an exported build.
</member>
<member name="application/run/flush_stdout_on_print" type="bool" setter="" getter="" default="false">
If [code]true[/code], flushes the standard output stream every time a line is printed. This affects both terminal logging and file logging.
When running a project, this setting must be enabled if you want logs to be collected by service managers such as systemd/journalctl. This setting is disabled by default on release builds, since flushing on every printed line will negatively affect performance if lots of lines are printed in a rapid succession. Also, if this setting is enabled, logged files will still be written successfully if the application crashes or is otherwise killed by the user (without being closed "normally").
[b]Note:[/b] Regardless of this setting, the standard error stream ([code]stderr[/code]) is always flushed when a line is printed to it.
</member>
<member name="application/run/flush_stdout_on_print.debug" type="bool" setter="" getter="" default="true">
Debug build override for [member application/run/flush_stdout_on_print], as performance is less important during debugging.
</member>
<member name="application/run/frame_delay_msec" type="int" setter="" getter="" default="0">
Forces a delay between frames in the main loop (in milliseconds). This may be useful if you plan to disable vertical synchronization.
</member>
Expand Down
5 changes: 5 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,11 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
}
#endif

// Only flush stdout in debug builds by default, as spamming `print()` will
// decrease performance if this is enabled.
GLOBAL_DEF("application/run/flush_stdout_on_print", false);
GLOBAL_DEF("application/run/flush_stdout_on_print.debug", true);

GLOBAL_DEF("logging/file_logging/enable_file_logging", false);
// Only file logging by default on desktop platforms as logs can't be
// accessed easily on mobile/Web platforms (if at all).
Expand Down

0 comments on commit 15d9f77

Please sign in to comment.