Skip to content

Commit b746c88

Browse files
committed
improvement: add log singleton
1 parent 6aeac8a commit b746c88

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

autoload/Log.gd

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
extends Node
2+
3+
enum LEVEL {
4+
TRACE = 10,
5+
DEBUG = 20,
6+
INFO = 30,
7+
WARN = 40,
8+
ERROR = 50,
9+
FATAL = 60,
10+
};
11+
12+
var _js_available := OS.has_feature("JavaScript")
13+
var GDQUEST: JavaScriptObject
14+
15+
func write(level: int, properties: Dictionary, message: String) -> void:
16+
if not _js_available:
17+
prints({ "properties": properties, "message": message })
18+
return
19+
var props = JavaScript.create_object("Object", {})
20+
for key in properties:
21+
props[key] = properties[key]
22+
match(level):
23+
LEVEL.FATAL:
24+
# warning-ignore:unsafe_property_access
25+
GDQUEST.log.fatal(props, message)
26+
LEVEL.ERROR:
27+
# warning-ignore:unsafe_property_access
28+
GDQUEST.log.error(props, message)
29+
LEVEL.WARN:
30+
# warning-ignore:unsafe_property_access
31+
GDQUEST.log.warn(props, message)
32+
LEVEL.INFO:
33+
# warning-ignore:unsafe_property_access
34+
GDQUEST.log.info(props, message)
35+
LEVEL.DEBUG:
36+
# warning-ignore:unsafe_property_access
37+
GDQUEST.log.debug(props, message)
38+
_:
39+
# warning-ignore:unsafe_property_access
40+
GDQUEST.log.trace(props, message)
41+
42+
43+
func _init() -> void:
44+
if not _js_available:
45+
return
46+
GDQUEST = JavaScript.get_interface("GDQUEST")
47+
48+
49+
func trace(properties: Dictionary, message: String) -> void:
50+
write(LEVEL.TRACE, properties, message)
51+
52+
func debug(properties: Dictionary, message: String) -> void:
53+
write(LEVEL.DEBUG, properties, message)
54+
55+
func info(properties: Dictionary, message: String) -> void:
56+
write(LEVEL.INFO, properties, message)
57+
58+
func warn(properties: Dictionary, message: String) -> void:
59+
write(LEVEL.WARN, properties, message)
60+
61+
func error(properties: Dictionary, message: String) -> void:
62+
write(LEVEL.ERROR, properties, message)
63+
64+
func fatal(properties: Dictionary, message: String) -> void:
65+
write(LEVEL.FATAL, properties, message)

project.godot

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ Events="*res://autoload/Events.gd"
333333
NavigationManager="*res://autoload/NavigationManager.gd"
334334
TextUtils="*res://autoload/TextUtils.gd"
335335
GDScriptErrorDatabase="*res://autoload/GDScriptErrorDatabase.gd"
336+
Log="*res://autoload/Log.gd"
336337

337338
[debug]
338339

0 commit comments

Comments
 (0)