Fix logger output when using verbose stdout mode (fix #23)

This commit is contained in:
JeremyStar™ 2024-04-14 14:42:46 +02:00
parent 56c96ee102
commit 0479635f95
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
5 changed files with 29 additions and 2 deletions

View file

@ -25,6 +25,13 @@ If `quit_safely` (and by extension `Core.cleanup`) should be called when pressin
The minimum log level you want to be displayed.
### *bool* <u>logger_colored</u> = *true*
Determines if the logger's output should be colored.
### *bool* <u>logger_detect_verbose_mode</u> = *false*
:::warning
Updating this during runtime does nothing.
:::
Determines if the logger should check if running in verbose mode (see [`OS.is_stdout_verbose`](https://docs.godotengine.org/en/4.2/classes/class_os.html#class-os-method-is-stdout-verbose)). \
Comes with a huge performance penalty on startup, delaying startup by about one to two seconds. \
Update `verbose_mode` accordingly yourself if you've disabled this, or diagnostic log messages might appear messed up. \
### *String* <u>logger_format</u> = *"%color%[%time%] [%level% %source%:%line%] %message%"*
The template for all log messages
Available placeholders are: `%time%`, `%time_ms%`, `%level%`, `%color%`, `%message%`, `%source%`, `%source_raw%`, `%function%` and `%line%`

View file

@ -21,6 +21,9 @@ Do not call this.
:::
Keeps track of all logger instances.
Unused instances will be cleaned periodically.
### *bool* <u>verbose_mode</u> = *false*
Used to determine if running in verbose/command line mode.
Makes diagnostic log messages display correctly (workaround for Godot's ANSI true color limitation).
## Functions
### *void* <u>_log</u>(*CoreTypes.LoggerLevel* <u>level</u>, *String* <u>origin</u>, *String* <u>message</u>)

View file

@ -19,11 +19,17 @@ class_name CoreConfiguration
@export var custom_modules: bool
## If [method Core.quit_safely] (and by extension [method Core.cleanup]) should be called when pressing the X.
@export var automatic_shutdown: bool
@export_category("Logger")
## The minimum log level you want to be displayed.
@export var logger_level: CoreTypes.LoggerLevel
## Determines if the logger's output should be colored.
@export var logger_colored: bool
## Determines if the logger should check if running in verbose mode (see [method OS.is_stdout_verbose]).[br]
## Comes with a huge performance penalty on startup, delaying startup by about one to two seconds.[br]
## Update [code]verbose_mode[/code] accordingly yourself if you've disabled this, or diagnostic log messages might appear messed up.[br]
## [b]Warning: [i]Updating this during runtime does nothing.[/i][/b]
@export var logger_detect_verbose_mode: bool
## The template for all log messages.[br]
## Available placeholders are: [code]%time%[/code], [code]%time_ms%[/code], [code]%level%[/code], [code]%color%[/code], [code]%message%[/code], [code]%source%[/code], [code]%source_raw%[/code], [code]%function%[/code] and [code]%line%[/code]
@export var logger_format: String
@ -31,6 +37,7 @@ class_name CoreConfiguration
@export var logger_newlines_override: bool
## The maximum amount of characters excluding the message before newlines are no longer idented. Set to [code]-1[/code] to disable this behaviour.
@export var logger_newlines_sizelimit: int
@export_category("Log UI")
## Enables or disables Log UI.
@export var logui_enabled: bool
@ -53,6 +60,7 @@ func _init() -> void:
# Logger
logger_level = CoreTypes.LoggerLevel.INFO
logger_colored = true
logger_detect_verbose_mode = false
logger_format = "%color%[%time%] [%level% %origin%] %message%"
logger_newlines_override = true
logger_newlines_sizelimit = 40

View file

@ -143,7 +143,7 @@ func complete_init(no_success_message: bool = false) -> void:
# Print and sleep
if modsinit_builtin.size() != 0 or modsinit_custom.size() != 0:
print("Waiting for modules to finish initialization:")
if modsinit_builtin.size() != 0: print(" Builtin: " + str(modsinit_builtin))
if modsinit_builtin.size() != 0: print(" Built-in: " + str(modsinit_builtin))
if modsinit_custom.size() != 0: print(" Custom: " + str(modsinit_custom))
await get_tree().create_timer(1).timeout

View file

@ -28,6 +28,10 @@ signal log_event
## [b]Danger: [i]Don't modify this.[/i][/b]
var instances: Array[CoreLoggerInstance] = []
## Used to determine if running in verbose/command line mode.[br]
## Makes diagnostic log messages display correctly (workaround for Godot's ANSI true color limitation).
var verbose_mode: bool = false
## The minimum log level you want to be displayed.
var config_level: CoreTypes.LoggerLevel
## Determines if the logger's output should be colored.
@ -41,6 +45,10 @@ var config_newlines_override: bool
var config_newlines_sizelimit: int
# +++ module +++
func _initialize() -> void:
if core.config.logger_detect_verbose_mode and OS.is_stdout_verbose():
verbose_mode = true
func _cleanup() -> void:
_schedule()
await get_tree().process_frame
@ -77,7 +85,8 @@ func _log(level: CoreTypes.LoggerLevel, origin: String, message: String) -> void
CoreTypes.LoggerLevel.DIAG:
format = format.replace("%level%", "DIAG")
format_newline = format_newline.replace("%level%", "DIAG")
format = format.replace("%color%", "[color=dark_gray]")
if verbose_mode: format = format.replace("%color%", "[color=gray]")
else: format = format.replace("%color%", "[color=dark_gray]")
CoreTypes.LoggerLevel.VERB:
format = format.replace("%level%", "VERB")
format_newline = format_newline.replace("%level%", "VERB")