11extends RefCounted
2- class_name _NetfoxLogger
2+ class_name NetfoxLogger
3+
4+ ## Logger implementation for use with netfox
5+ ##
6+ ## NetfoxLoggers implement distinct log levels. These can be used to filter
7+ ## which messages are actually emitted. All messages are output using [method
8+ ## @GlobalScope.print]. Warnings and errors are also pushed to the debug panel,
9+ ## using [method @GlobalScope.push_warning] and [method @GlobalScope.push_error]
10+ ## respectively, if [member push_to_debugger] is enabled.
11+ ## [br][br]
12+ ## Every logger has a name, and belongs to a module. Logging level can be
13+ ## overridden per module, using [member module_log_level].
14+ ## [br][br]
15+ ## Loggers also support tags. Tags can be used to provide extra pieces of
16+ ## information that are logged with each message. Some tags are provided by
17+ ## netfox. Additional tags can be added using [method register_tag].
18+ ##
19+ ## @tutorial(Logging Guide): https://foxssake.github.io/netfox/latest/netfox/guides/logging/
20+
321
422enum {
5- LOG_MIN ,
6- LOG_TRACE ,
7- LOG_DEBUG ,
8- LOG_INFO ,
9- LOG_WARN ,
10- LOG_ERROR ,
11- LOG_MAX
23+ LOG_ALL , ## Filter level to log every message
24+ LOG_TRACE , ## Trace logs, the most verbose level
25+ LOG_DEBUG , ## Debug logs
26+ LOG_INFO , ## Info logs
27+ LOG_WARN , ## Warnings
28+ LOG_ERROR , ## Errors
29+ LOG_NONE ## Filter level to log no messages
1230}
1331
32+ ## Default log level to fall back on, if not configured
1433const DEFAULT_LOG_LEVEL := LOG_DEBUG
1534
16- static var log_level : int
17- static var module_log_level : Dictionary
18-
19- static var _tags : Dictionary = {}
20- static var _ordered_tags : Array [Callable ] = []
21-
22- var module : String
23- var name : String
24-
25- const level_prefixes : Array [String ] = [
35+ const _LEVEL_PREFIXES : Array [String ] = [
2636 "" ,
2737 "TRC" ,
2838 "DBG" ,
@@ -32,24 +42,32 @@ const level_prefixes: Array[String] = [
3242 ""
3343]
3444
35- static func for_netfox ( p_name : String ) -> _NetfoxLogger:
36- return _NetfoxLogger . new ( "netfox" , p_name )
45+ ## Global logging level, used by all loggers
46+ static var log_level : int
3747
38- static func for_noray (p_name : String ) -> _NetfoxLogger:
39- return _NetfoxLogger .new ("netfox.noray" , p_name )
48+ ## Per-module logging level, used only by loggers belonging to the given module
49+ ## [br][br]
50+ ## This is a dictionary that associates module names ( strings ) to log levels
51+ ## ( int, e.g. [constant LOG_DEBUG] ).
52+ static var module_log_level : Dictionary
4053
41- static func for_extras (p_name : String ) -> _NetfoxLogger:
42- return _NetfoxLogger .new ("netfox.extras" , p_name )
54+ ## Set to true to enable calling [@GlobalScope.push_warning] and
55+ ## [@GlobalScope.push_error]
56+ static var push_to_debugger := true
4357
44- static func make_setting (name : String ) -> Dictionary :
45- return {
46- "name" : name ,
47- "value" : DEFAULT_LOG_LEVEL ,
48- "type" : TYPE_INT ,
49- "hint" : PROPERTY_HINT_ENUM ,
50- "hint_string" : "All,Trace,Debug,Info,Warning,Error,None"
51- }
58+ static var _tags : Dictionary = {}
59+ static var _ordered_tags : Array [Callable ] = []
5260
61+ ## Logger module
62+ var module : String
63+ ## Logger name
64+ var name : String
65+
66+
67+ ## Register a tag
68+ ## [br][br]
69+ ## Tags are callables that provide pieces of context, included in all log
70+ ## messges. The [param tag] callable must return a string.
5371static func register_tag (tag : Callable , priority : int = 0 ) -> void :
5472 # Save tag
5573 if not _tags .has (priority ):
@@ -67,6 +85,7 @@ static func register_tag(tag: Callable, priority: int = 0) -> void:
6785 var tag_group = _tags [prio_group ]
6886 _ordered_tags .append_array (tag_group )
6987
88+ ## Free an already registered tag
7089static func free_tag (tag : Callable ) -> void :
7190 for priority in _tags .keys ():
7291 var priority_group := _tags [priority ] as Array
@@ -86,10 +105,76 @@ static func _static_init():
86105 "netfox.extras" : ProjectSettings .get_setting (& "netfox/logging/netfox_extras_log_level" , DEFAULT_LOG_LEVEL )
87106 }
88107
108+ static func _for_netfox (p_name : String ) -> NetfoxLogger :
109+ return NetfoxLogger .new ("netfox" , p_name )
110+
111+ static func _for_noray (p_name : String ) -> NetfoxLogger :
112+ return NetfoxLogger .new ("netfox.noray" , p_name )
113+
114+ static func _for_extras (p_name : String ) -> NetfoxLogger :
115+ return NetfoxLogger .new ("netfox.extras" , p_name )
116+
117+ static func _make_setting (name : String ) -> Dictionary :
118+ return {
119+ "name" : name ,
120+ "value" : DEFAULT_LOG_LEVEL ,
121+ "type" : TYPE_INT ,
122+ "hint" : PROPERTY_HINT_ENUM ,
123+ "hint_string" : "All,Trace,Debug,Info,Warning,Error,None"
124+ }
125+
126+
89127func _init (p_module : String , p_name : String ):
90128 module = p_module
91129 name = p_name
92130
131+ ## Log a trace message
132+ ## [br][br]
133+ ## Traces are the most verbose, usually used for drilling down into very niche
134+ ## bugs.
135+ func trace (text : String , values : Array = []):
136+ _log_text (text , values , LOG_TRACE )
137+
138+ ## Log a debug message
139+ ## [br][br]
140+ ## Debug messages are verbose, usually used to reconstruct and investigate bugs.
141+ func debug (text : String , values : Array = []):
142+ _log_text (text , values , LOG_DEBUG )
143+
144+ ## Log an info message
145+ ## [br][br]
146+ ## Info messages provide general notifications about application events.
147+ func info (text : String , values : Array = []):
148+ _log_text (text , values , LOG_INFO )
149+
150+ ## Log a warning message
151+ ## [br][br]
152+ ## This is also forwarded to [method @GlobalScope.push_warning], if enabled with
153+ ## [member push_to_debugger]. Warning messages usually indicate that something
154+ ## has gone wrong, but is recoverable.
155+ func warning (text : String , values : Array = []):
156+ if _check_log_level (LOG_WARN ):
157+ var formatted_text = _format_text (text , values , LOG_WARN )
158+ if push_to_debugger :
159+ push_warning (formatted_text )
160+
161+ # Print so it shows up in the Output panel too
162+ print (formatted_text )
163+
164+ ## Log an error message
165+ ## [br][br]
166+ ## This is also forwarded to [method @GlobalScope.push_error], if enabled with
167+ ## [member push_to_debugger]. Error messages usually indicate an issue that
168+ ## can't be recovered from.
169+ func error (text : String , values : Array = []):
170+ if _check_log_level (LOG_ERROR ):
171+ var formatted_text = _format_text (text , values , LOG_ERROR )
172+ if push_to_debugger :
173+ push_error (formatted_text )
174+
175+ # Print so it shows up in the Output panel too
176+ print (formatted_text )
177+
93178func _check_log_level (level : int ) -> bool :
94179 var cmp_level = log_level
95180 if level < cmp_level :
@@ -102,11 +187,11 @@ func _check_log_level(level: int) -> bool:
102187 return true
103188
104189func _format_text (text : String , values : Array , level : int ) -> String :
105- level = clampi (level , LOG_MIN , LOG_MAX )
190+ level = clampi (level , LOG_TRACE , LOG_ERROR )
106191
107192 var result := PackedStringArray ()
108193
109- result .append ("[%s ]" % [level_prefixes [level ]])
194+ result .append ("[%s ]" % [_LEVEL_PREFIXES [level ]])
110195 for tag in _ordered_tags :
111196 result .append ("[%s ]" % [tag .call ()])
112197 result .append ("[%s ::%s ] " % [module , name ])
@@ -121,26 +206,3 @@ func _format_text(text: String, values: Array, level: int) -> String:
121206func _log_text (text : String , values : Array , level : int ):
122207 if _check_log_level (level ):
123208 print (_format_text (text , values , level ))
124-
125- func trace (text : String , values : Array = []):
126- _log_text (text , values , LOG_TRACE )
127-
128- func debug (text : String , values : Array = []):
129- _log_text (text , values , LOG_DEBUG )
130-
131- func info (text : String , values : Array = []):
132- _log_text (text , values , LOG_INFO )
133-
134- func warning (text : String , values : Array = []):
135- if _check_log_level (LOG_WARN ):
136- var formatted_text = _format_text (text , values , LOG_WARN )
137- push_warning (formatted_text )
138- # Print so it shows up in the Output panel too
139- print (formatted_text )
140-
141- func error (text : String , values : Array = []):
142- if _check_log_level (LOG_ERROR ):
143- var formatted_text = _format_text (text , values , LOG_ERROR )
144- push_error (formatted_text )
145- # Print so it shows up in the Output panel too
146- print (formatted_text )
0 commit comments