Skip to content

Commit e96efd6

Browse files
use rich_print when in-editor (#562)
* use rich_print when in-editor * add profile scaffolding impl. the usage of these into ModLoaderLog * improve `_print_rich` now accepts a Color over a String * populate all logging color variables * chore: use Godot's default color scheme * remove debug color config being a debug log it felt weird to have color * chore: exclude "ERROR" from prefix when in editor this removes the duplicate "ERROR: ERROR" at the start of error logs * chore: control debug log color and boldness
1 parent e39ba5d commit e96efd6

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

addons/mod_loader/api/log.gd

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ static var verbosity: VERBOSITY_LEVEL = VERBOSITY_LEVEL.DEBUG
4343
## Array of mods that should be ignored when logging messages (contains mod IDs as strings)
4444
static var ignored_mods: Array[String] = []
4545

46-
## Highlighting color for hint type log messages
47-
static var hint_color := Color("#70bafa")
46+
# NOTE: default values which get replaced later by `_configure_logger`
47+
static var warning_color := Color("#ffde66")
48+
static var success_color := Color("#5d8c3f")
49+
static var info_color := Color("#70bafa")
50+
static var hint_color := Color("#b293fa")
51+
static var debug_color := Color("#d4d4d4")
52+
static var debug_bold := true
4853

4954
## This Sub-Class represents a log entry in ModLoader.
5055
class ModLoaderLogEntry:
@@ -99,9 +104,15 @@ class ModLoaderLogEntry:
99104

100105
## Get the prefix string for the log entry, including the log type and mod name.[br]
101106
## [br]
107+
## [b]Parameters:[/b][br]
108+
## [param exclude_type] ([bool]): (Optional) If true, the log type (e.g., DEBUG, WARN) will be excluded from the prefix. Default is false.[br]
109+
## [br]
102110
## [b]Returns:[/b] [String]
103-
func get_prefix() -> String:
104-
return "%s %s: " % [type.to_upper(), mod_name]
111+
func get_prefix(exclude_type: bool = false) -> String:
112+
return "%s%s: " % [
113+
"" if exclude_type else type.to_upper() + " ",
114+
mod_name
115+
]
105116

106117

107118
## Generate an MD5 hash of the log entry (prefix + message).[br]
@@ -391,6 +402,17 @@ static func get_all_entries_as_string(log_entries: Array) -> Array:
391402
# Internal log functions
392403
# =============================================================================
393404

405+
static func _print_rich(prefix: String, message: String, color: Color, bold := true):
406+
if OS.has_feature("editor"):
407+
var prefix_text := "[b]%s[/b]" % prefix if bold else prefix
408+
print_rich("[color=%s]%s[/color]%s" % [
409+
color.to_html(false),
410+
prefix_text,
411+
message
412+
])
413+
else:
414+
print(prefix + message)
415+
394416
static func _log(message: String, mod_name: String, log_type: String = "info", only_once := false) -> void:
395417
if _is_mod_name_ignored(mod_name):
396418
return
@@ -422,25 +444,32 @@ static func _log(message: String, mod_name: String, log_type: String = "info", o
422444
_write_to_log_file(JSON.stringify(get_stack(), " "))
423445
assert(false, message)
424446
"error":
425-
printerr(log_entry.get_prefix() + message)
447+
if OS.has_feature("editor"):
448+
printerr(log_entry.get_prefix(true) + message)
449+
else:
450+
printerr(log_entry.get_prefix() + message)
426451
push_error(message)
427452
_write_to_log_file(log_entry.get_entry())
428453
"warning":
429454
if verbosity >= VERBOSITY_LEVEL.WARNING:
430-
print(log_entry.get_prefix() + message)
455+
_print_rich(log_entry.get_prefix(), message, warning_color)
431456
push_warning(message)
432457
_write_to_log_file(log_entry.get_entry())
433-
"info", "success":
458+
"success":
459+
if verbosity >= VERBOSITY_LEVEL.INFO:
460+
_print_rich(log_entry.get_prefix(), message, success_color)
461+
_write_to_log_file(log_entry.get_entry())
462+
"info":
434463
if verbosity >= VERBOSITY_LEVEL.INFO:
435-
print(log_entry.get_prefix() + message)
464+
_print_rich(log_entry.get_prefix(), message, info_color)
436465
_write_to_log_file(log_entry.get_entry())
437466
"debug":
438467
if verbosity >= VERBOSITY_LEVEL.DEBUG:
439-
print(log_entry.get_prefix() + message)
468+
_print_rich(log_entry.get_prefix(), message, debug_color, debug_bold)
440469
_write_to_log_file(log_entry.get_entry())
441470
"hint":
442471
if OS.has_feature("editor") and verbosity >= VERBOSITY_LEVEL.DEBUG:
443-
print_rich("[color=%s]%s[/color]" % [hint_color.to_html(false), log_entry.get_prefix() + message])
472+
_print_rich(log_entry.get_prefix(), message, hint_color)
444473

445474

446475
static func _is_mod_name_ignored(mod_name: String) -> bool:

addons/mod_loader/mod_loader_store.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,9 @@ func _update_ml_options_from_cli_args() -> void:
227227
func _configure_logger() -> void:
228228
ModLoaderLog.verbosity = ml_options.log_level
229229
ModLoaderLog.ignored_mods = ml_options.ignored_mod_names_in_log
230+
ModLoaderLog.warning_color = ml_options.warning_color
231+
ModLoaderLog.success_color = ml_options.success_color
232+
ModLoaderLog.info_color = ml_options.info_color
230233
ModLoaderLog.hint_color = ml_options.hint_color
234+
ModLoaderLog.debug_color = ml_options.debug_color
235+
ModLoaderLog.debug_bold = ml_options.debug_bold

addons/mod_loader/resources/options_profile.gd

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,18 @@ enum VERSION_VALIDATION {
5555
## [code]ModLoader:Dependency[/code] - ignore the exact name [br]
5656
## [code]ModLoader:*[/code] - ignore all beginning with this name [br]
5757
@export var ignored_mod_names_in_log: Array[String] = []
58-
@export var hint_color := Color("#70bafa")
58+
## Highlighting color for warning type log messages
59+
@export var warning_color := Color("#ffde66")
60+
## Highlighting color for success type log messages
61+
@export var success_color := Color("#5d8c3f")
62+
## Highlighting color for info type log messages
63+
@export var info_color := Color("#70bafa")
64+
## Highlighting color for hint type log messages
65+
@export var hint_color := Color("#b293fa")
66+
## Highlighting color for debug type log messages
67+
@export var debug_color := Color("#d4d4d4")
68+
## Highlight debug log prefixes with bold formatting
69+
@export var debug_bold := true
5970

6071
@export_group("Game Data")
6172
## Steam app id, can be found in the steam page url

0 commit comments

Comments
 (0)