Skip to content

Commit 240d505

Browse files
committed
Add a custom logging demo
This demo showcases a custom logger implementation, which runs in parallel to the built-in logging facilities (including file logging). The custom logger displays all messages printed by the engine in an in-game console.
1 parent b45ef54 commit 240d505

14 files changed

+401
-0
lines changed
92.4 KB
Binary file not shown.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[remap]
2+
3+
importer="font_data_dynamic"
4+
type="FontFile"
5+
uid="uid://g800tr1mba1m"
6+
path="res://.godot/imported/JetBrainsMono-Bold.woff2-90b8bb02e1d6b1e6d33bf7df4e6cecb8.fontdata"
7+
8+
[deps]
9+
10+
source_file="res://JetBrainsMono-Bold.woff2"
11+
dest_files=["res://.godot/imported/JetBrainsMono-Bold.woff2-90b8bb02e1d6b1e6d33bf7df4e6cecb8.fontdata"]
12+
13+
[params]
14+
15+
Rendering=null
16+
antialiasing=1
17+
generate_mipmaps=false
18+
disable_embedded_bitmaps=true
19+
multichannel_signed_distance_field=false
20+
msdf_pixel_range=8
21+
msdf_size=48
22+
allow_system_fallback=true
23+
force_autohinter=false
24+
modulate_color_glyphs=false
25+
hinting=1
26+
subpixel_positioning=4
27+
keep_rounding_remainders=true
28+
oversampling=0.0
29+
Fallbacks=null
30+
fallbacks=[]
31+
Compress=null
32+
compress=true
33+
preload=[]
34+
language_support={}
35+
script_support={}
36+
opentype_features={}
90 KB
Binary file not shown.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[remap]
2+
3+
importer="font_data_dynamic"
4+
type="FontFile"
5+
uid="uid://jrduhl6723o1"
6+
path="res://.godot/imported/JetBrainsMono-Regular.woff2-7c9f8b67058abc1b94f68d1d046bf79f.fontdata"
7+
8+
[deps]
9+
10+
source_file="res://JetBrainsMono-Regular.woff2"
11+
dest_files=["res://.godot/imported/JetBrainsMono-Regular.woff2-7c9f8b67058abc1b94f68d1d046bf79f.fontdata"]
12+
13+
[params]
14+
15+
Rendering=null
16+
antialiasing=1
17+
generate_mipmaps=false
18+
disable_embedded_bitmaps=true
19+
multichannel_signed_distance_field=false
20+
msdf_pixel_range=8
21+
msdf_size=48
22+
allow_system_fallback=true
23+
force_autohinter=false
24+
modulate_color_glyphs=false
25+
hinting=1
26+
subpixel_positioning=4
27+
keep_rounding_remainders=true
28+
oversampling=0.0
29+
Fallbacks=null
30+
fallbacks=[]
31+
Compress=null
32+
compress=true
33+
preload=[]
34+
language_support={}
35+
script_support={}
36+
opentype_features={}

misc/custom_logging/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Custom Logging
2+
3+
This demo showcases a custom logger implementation, which runs in parallel
4+
to the built-in logging facilities (including file logging). The custom logger
5+
displays all messages printed by the engine in an in-game console.
6+
7+
See [Logging](https://docs.godotengine.org/en/latest/tutorials/scripting/logging.html)
8+
in the documentation for more information about configuring the engine's logging
9+
and writing custom loggers.
10+
11+
Language: GDScript
12+
13+
Renderer: Compatibility
14+
15+
## Screenshots
16+
17+
![Screenshot](screenshots/custom_logging.webp)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
extends RichTextLabel
2+
3+
4+
class CustomLogger extends Logger:
5+
func _log_message(message: String, _error: bool) -> void:
6+
CustomLoggerUI.get_node("Panel/RichTextLabel").text += message
7+
8+
9+
func _log_error(
10+
function: String,
11+
file: String,
12+
line: int,
13+
code: String,
14+
rationale: String,
15+
_editor_notify: bool,
16+
error_type: int,
17+
script_backtraces: Array[ScriptBacktrace]
18+
) -> void:
19+
var prefix := ""
20+
# The column at which to print the trace. Should match the length of the
21+
# unformatted text above it.
22+
var trace_indent := 0
23+
24+
match error_type:
25+
ERROR_TYPE_ERROR:
26+
prefix = "[color=#f54][b]ERROR:[/b]"
27+
trace_indent = 6
28+
ERROR_TYPE_WARNING:
29+
prefix = "[color=#fd4][b]WARNING:[/b]"
30+
trace_indent = 8
31+
ERROR_TYPE_SCRIPT:
32+
prefix = "[color=#f4f][b]SCRIPT ERROR:[/b]"
33+
trace_indent = 13
34+
ERROR_TYPE_SHADER:
35+
prefix = "[color=#4bf][b]SHADER ERROR:[/b]"
36+
trace_indent = 13
37+
38+
var trace := "%*s %s (%s:%s)" % [trace_indent, "at:", function, file, line]
39+
var script_backtraces_text := ""
40+
for backtrace in script_backtraces:
41+
script_backtraces_text += backtrace.format(trace_indent - 3) + "\n"
42+
43+
CustomLoggerUI.get_node("Panel/RichTextLabel").text += "%s %s %s[/color]\n[color=#999]%s[/color]\n[color=#999]%s[/color]" % [
44+
prefix,
45+
code,
46+
rationale,
47+
trace,
48+
script_backtraces_text,
49+
]
50+
51+
52+
# Use `_init()` to initialize the logger as early as possible, which ensures that messages
53+
# printed early are taken into account. However, even when using `_init()`, the engine's own
54+
# initialization messages are not accessible.
55+
func _init() -> void:
56+
OS.add_logger(CustomLogger.new())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://cgdbfnmbujg61
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[gd_scene load_steps=5 format=3 uid="uid://cpdcq55mdqx5o"]
2+
3+
[ext_resource type="FontFile" uid="uid://jrduhl6723o1" path="res://JetBrainsMono-Regular.woff2" id="1_c73ru"]
4+
[ext_resource type="FontFile" uid="uid://g800tr1mba1m" path="res://JetBrainsMono-Bold.woff2" id="2_nsaj1"]
5+
[ext_resource type="Script" uid="uid://cgdbfnmbujg61" path="res://custom_logger_ui.gd" id="3_5eal2"]
6+
7+
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_c73ru"]
8+
content_margin_left = 20.0
9+
content_margin_top = 0.0
10+
content_margin_right = 0.0
11+
content_margin_bottom = 10.0
12+
bg_color = Color(0.1, 0.1, 0.1, 0.6)
13+
corner_radius_top_left = 3
14+
corner_radius_top_right = 3
15+
corner_radius_bottom_right = 3
16+
corner_radius_bottom_left = 3
17+
corner_detail = 5
18+
19+
[node name="CanvasLayer" type="CanvasLayer"]
20+
layer = 1024
21+
22+
[node name="Panel" type="PanelContainer" parent="."]
23+
anchors_preset = 15
24+
anchor_right = 1.0
25+
anchor_bottom = 1.0
26+
offset_bottom = -194.0
27+
grow_horizontal = 2
28+
grow_vertical = 2
29+
theme_override_styles/panel = SubResource("StyleBoxFlat_c73ru")
30+
31+
[node name="RichTextLabel" type="RichTextLabel" parent="Panel"]
32+
layout_mode = 2
33+
theme_override_fonts/normal_font = ExtResource("1_c73ru")
34+
theme_override_fonts/bold_font = ExtResource("2_nsaj1")
35+
theme_override_fonts/mono_font = ExtResource("1_c73ru")
36+
bbcode_enabled = true
37+
scroll_following = true
38+
script = ExtResource("3_5eal2")

misc/custom_logging/main.gd

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
extends Control
2+
3+
var message_counter := 0
4+
var message_raw_counter := 0
5+
var message_stderr_counter := 0
6+
var warning_counter := 0
7+
var error_counter := 0
8+
9+
10+
func _ready() -> void:
11+
print("Normal message 1.")
12+
push_error("Error 1.")
13+
push_warning("Warning 1.")
14+
push_error("Error 2.")
15+
push_warning("Warning 2.")
16+
print("Normal message 2.")
17+
printerr("Normal message 1 (stderr).")
18+
printerr("Normal message 2 (stderr).")
19+
printraw("Normal message 1 (raw). ")
20+
printraw("Normal message 2 (raw).\n--------\n")
21+
22+
if bool(ProjectSettings.get_setting_with_override("application/run/flush_stdout_on_print")):
23+
$FlushStdoutOnPrint.text = "Flush stdout on print: Yes (?)"
24+
else:
25+
$FlushStdoutOnPrint.text = "Flush stdout on print: No (?)"
26+
27+
28+
func _on_print_message_pressed() -> void:
29+
message_counter += 1
30+
print("Printing message #%d." % message_counter)
31+
32+
33+
func _on_print_message_raw_pressed() -> void:
34+
message_raw_counter += 1
35+
printraw("Printing message #%d (raw). " % message_raw_counter)
36+
37+
38+
func _on_print_message_stderr_pressed() -> void:
39+
message_stderr_counter += 1
40+
printerr("Printing message #%d (stderr)." % message_stderr_counter)
41+
42+
43+
func _on_print_warning_pressed() -> void:
44+
warning_counter += 1
45+
push_warning("Printing warning #%d." % warning_counter)
46+
47+
48+
func _on_print_error_pressed() -> void:
49+
error_counter += 1
50+
push_error("Printing error #%d." % error_counter)
51+
52+
53+
func _on_open_logs_folder_pressed() -> void:
54+
OS.shell_open(ProjectSettings.globalize_path(String(ProjectSettings.get_setting_with_override("debug/file_logging/log_path")).get_base_dir()))
55+
56+
57+
func _on_crash_engine_pressed() -> void:
58+
OS.crash("Crashing the engine on user request (the Crash Engine button was pressed). Do not report this as a bug.")

misc/custom_logging/main.gd.uid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://715k1racyrfh

0 commit comments

Comments
 (0)