Skip to content

Commit b78e486

Browse files
committed
Update to 1.2
- Fixes #8 & #9 - Cleans up the _internal_log method.
1 parent 860845b commit b78e486

File tree

5 files changed

+105
-82
lines changed

5 files changed

+105
-82
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.godot/
33
icon.svg
44
**.import
5-
#project.godot
5+
#project.godot
6+
.vscode/

addons/logger/log-stream.gd

+86-73
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static func _static_init() -> void:
3232
_ensure_setting_exists(settings.LOG_MESSAGE_FORMAT_KEY, settings.LOG_MESSAGE_FORMAT_DEFAULT_VALUE)
3333
_ensure_setting_exists(settings.USE_UTC_TIME_FORMAT_KEY, settings.USE_UTC_TIME_FORMAT_DEFAULT_VALUE)
3434
_ensure_setting_exists(settings.BREAK_ON_ERROR_KEY, settings.BREAK_ON_ERROR_DEFAULT_VALUE)
35+
_ensure_setting_exists(settings.PRINT_TREE_ON_ERROR_KEY, settings.PRINT_TREE_ON_ERROR_DEFAULT_VALUE)
3536

3637
func _init(log_name:String, min_log_level:=LogLevel.DEFAULT, crash_behavior:Callable = default_crash_behavior):
3738
_log_name = log_name
@@ -42,6 +43,10 @@ func _init(log_name:String, min_log_level:=LogLevel.DEFAULT, crash_behavior:Call
4243
func debug(message, values={}):
4344
call_thread_safe("_internal_log", message, values, LogLevel.DEBUG)
4445

46+
##Shorthand for debug
47+
func dbg(message:String,values={}):
48+
call_thread_safe("_internal_log", message, values, LogLevel.DEBUG)
49+
4550
##prints a message to the log at the info level.
4651
func info(message:String,values={}):
4752
call_thread_safe("_internal_log", message, values)
@@ -54,19 +59,15 @@ func warn(message:String,values={}):
5459
func error(message:String,values={}):
5560
call_thread_safe("_internal_log", message, values, LogLevel.ERROR)
5661

62+
##Shorthand for error
63+
func err(message:String,values={}):
64+
call_thread_safe("_internal_log", message, values, LogLevel.ERROR)
65+
5766
##Prints a message to the log at the fatal level, exits the application
5867
##since there has been a fatal error.
5968
func fatal(message:String,values={}):
6069
call_thread_safe("_internal_log", message, values, LogLevel.FATAL)
6170

62-
##Shorthand for debug
63-
func dbg(message:String,values={}):
64-
call_thread_safe("_internal_log", message, values, LogLevel.DEBUG)
65-
66-
##Shorthand for error
67-
func err(message:String,values={}):
68-
call_thread_safe("_internal_log", message, values, LogLevel.ERROR)
69-
7071
##Throws an error if err_code is not of value "OK" and appends the error code string.
7172
func err_cond_not_ok(err_code:Error, message:String, fatal:=true, other_values_to_be_printed={}):
7273
if err_code != OK:
@@ -88,11 +89,66 @@ func err_cond_not_equal(arg1, arg2, message:String, fatal:=true, other_values_to
8889
if (arg1 is Color && arg2 is Color && !arg1.is_equal_approx(arg2)) || arg1 != arg2:
8990
call_thread_safe("_internal_log", str(arg1) + " != " + str(arg2) + ", not allowed. " + message, other_values_to_be_printed, LogLevel.FATAL if fatal else LogLevel.ERROR)
9091

91-
##Main internal logging method, please use the logger() instead since this is not thread safe.
92+
##Main internal logging method, please use the methods above instead, since this is not thread safe.
9293
func _internal_log(message:String, values, log_level := LogLevel.INFO):
9394
if current_log_level > log_level :
9495
return
96+
if log_level == LogLevel.DEFAULT:
97+
err("Can't log at 'default' level, this level is only used as filter")
98+
##Format message string
99+
var format_str:String = ProjectSettings.get_setting(settings.LOG_MESSAGE_FORMAT_KEY, settings.LOG_MESSAGE_FORMAT_DEFAULT_VALUE)
100+
message = format_str.format(_get_format_data(message, log_level))
101+
##Tac on passed values
102+
message += _stringify_values(values)
95103

104+
var stack = get_stack()
105+
emit_signal("log_message", log_level, message)
106+
if stack.is_empty():#Aka is connected to debug server -> print to the editor console in addition to pushing the warning.
107+
_log_mode_console(message, log_level)
108+
else:
109+
_log_mode_editor(message, log_level, stack)
110+
##AKA, level is error or fatal, the main tree is accessible and we want to print it.
111+
if log_level > 3 && Log.is_inside_tree() && ProjectSettings.get_setting(settings.PRINT_TREE_ON_ERROR_KEY, settings.PRINT_TREE_ON_ERROR_DEFAULT_VALUE):
112+
#We want to access the main scene tree since this may be a custom logger that isn't in the main tree.
113+
print("Main tree: ")
114+
Log.get_tree().root.print_tree_pretty()
115+
print("")#Print empty line to mark new message
116+
117+
if log_level == LogLevel.FATAL:
118+
_crash_behavior.call()
119+
120+
func _log_mode_editor(msg:String, lvl:LogLevel, stack:Array):
121+
match lvl:
122+
LogLevel.DEBUG:
123+
print_rich("[color=gray]"+msg+"[/color]")
124+
LogLevel.INFO:
125+
print_rich(msg)
126+
LogLevel.WARN:
127+
print_rich("[color=yellow]"+msg+"[/color]")
128+
push_warning(msg)
129+
print(_get_reduced_stack(stack) + "\n")
130+
_:#AKA error or fatal
131+
push_error(msg)
132+
msg = msg.replace("[lb]", "[").replace("[rb]", "]")
133+
printerr(msg)
134+
#Mimic the native godot behavior of halting execution upon error.
135+
if ProjectSettings.get_setting(settings.BREAK_ON_ERROR_KEY, settings.BREAK_ON_ERROR_DEFAULT_VALUE):
136+
##Please go a few steps down the stack to find the errorous code, since you are currently inside the error handler.
137+
breakpoint
138+
print(_get_reduced_stack(stack))
139+
140+
141+
func _log_mode_console(msg:String, lvl:LogLevel):
142+
##remove any BBCodes
143+
msg = msg.replace("[lb]", "[").replace("[rb]", "]")
144+
if lvl < 3:
145+
print(msg)
146+
elif lvl == LogLevel.WARN:
147+
push_warning(msg)
148+
else:
149+
push_error(msg)
150+
151+
func _get_format_data(msg:String, lvl:LogLevel)->Dictionary:
96152
var now = Time.get_datetime_dict_from_system(ProjectSettings.get_setting(settings.USE_UTC_TIME_FORMAT_KEY, settings.USE_UTC_TIME_FORMAT_DEFAULT_VALUE))
97153
now["second"] = "%02d"%now["second"]
98154
now["minute"] = "%02d"%now["minute"]
@@ -102,78 +158,35 @@ func _internal_log(message:String, values, log_level := LogLevel.INFO):
102158

103159
var format_data := {
104160
"log_name":_log_name,
105-
"message":message,
106-
"level":LogLevel.keys()[log_level]
161+
"message":msg,
162+
"level":LogLevel.keys()[lvl]
107163
}
108164
format_data.merge(now)
109-
var msg:String = ProjectSettings.get_setting(settings.LOG_MESSAGE_FORMAT_KEY, settings.LOG_MESSAGE_FORMAT_DEFAULT_VALUE).format(format_data)
110-
var stack = get_stack()
111-
165+
return format_data
166+
167+
func _stringify_values(values)->String:
112168
match typeof(values):
169+
TYPE_NIL:
170+
return ""
113171
TYPE_ARRAY:
114-
if values.size() > 0:
115-
msg += "["
116-
for k in values:
117-
msg += "{k},".format({"k":JSON.stringify(k)})
118-
msg = msg.left(msg.length()-1)+"]"
172+
var msg = "["
173+
for k in values:
174+
msg += "{k}, ".format({"k":JSON.stringify(k)})
175+
return msg + "]"
119176
TYPE_DICTIONARY:
120-
if values.size() > 0:
121-
msg += "{"
122-
for k in values:
123-
if typeof(values[k]) == TYPE_OBJECT && values[k] != null:
124-
msg += '"{k}":{v},'.format({"k":k,"v":JSON.stringify(JsonData.to_dict(values[k],false))})
125-
else:
126-
msg += '"{k}":{v},'.format({"k":k,"v":JSON.stringify(values[k])})
127-
msg = msg.left(msg.length()-1)+"}"
177+
var msg = "{"
178+
for k in values:
179+
if typeof(values[k]) == TYPE_OBJECT && values[k] != null:
180+
msg += '"{k}":{v},'.format({"k":k,"v":JSON.stringify(JsonData.to_dict(values[k],false))})
181+
else:
182+
msg += '"{k}":{v},'.format({"k":k,"v":JSON.stringify(values[k])})
183+
return msg+"}"
128184
TYPE_PACKED_BYTE_ARRAY:
129-
if values == null:
130-
msg += JSON.stringify(null)
131-
else:
132-
msg += JSON.stringify(JsonData.unmarshal_bytes_to_dict(values))
185+
return JSON.stringify(JsonData.unmarshal_bytes_to_dict(values))
133186
TYPE_OBJECT:
134-
if values == null:
135-
msg += JSON.stringify(null)
136-
else:
137-
msg += JSON.stringify(JsonData.to_dict(values,false))
138-
TYPE_NIL:
139-
msg += JSON.stringify(null)
187+
return JSON.stringify(JsonData.to_dict(values,false))
140188
_:
141-
msg += JSON.stringify(values)
142-
143-
emit_signal("log_message", log_level, msg)
144-
match log_level:
145-
LogLevel.DEBUG:
146-
print_rich("[color=gray]"+msg+"[/color]")
147-
LogLevel.INFO:
148-
print_rich(msg)
149-
LogLevel.WARN:
150-
if !stack.is_empty():#Aka is connected to debug server -> print to the editor console in addition to pushing the warning.
151-
print_rich("[color=yellow]"+msg+"[/color]")
152-
153-
push_warning(msg)
154-
print(_get_reduced_stack(stack) + "\n")
155-
LogLevel.DEFAULT:
156-
err("Can't log at 'default' level, this level is only used as filter")
157-
_:
158-
msg = msg.replace("[lb]", "[").replace("[rb]", "]")
159-
push_error(msg)
160-
if !stack.is_empty():#Aka is connected to debug server -> print to the editor console in addition to pushing the warning.
161-
printerr(msg)
162-
#Mimic the native godot behavior of halting execution upon error.
163-
if ProjectSettings.get_setting(settings.BREAK_ON_ERROR_KEY, settings.BREAK_ON_ERROR_DEFAULT_VALUE):
164-
##Please go a few steps down the stack to find the errorous code, since you are currently inside the error handler.
165-
breakpoint
166-
print(_get_reduced_stack(stack))
167-
168-
#We want to access the main scene tree since this may be a custom logger that isn't in the main tree.
169-
if Log.is_inside_tree():
170-
print("Main tree: ")
171-
Log.get_tree().root.print_tree_pretty()
172-
print("")#Print empty line to mark new message
173-
174-
if log_level == LogLevel.FATAL:
175-
_crash_behavior.call()
176-
189+
return JSON.stringify(values)
177190

178191
func _get_reduced_stack(stack:Array)->String:
179192
var stack_trace_message:=""

addons/logger/plugin.cfg

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ New for the fork:
1616
- Adds a scripted breakpoint (optional in setting) so errors freeze the execution and shows relevant info in the godot debugger.
1717
- Adds support for multiple log files.
1818
- Adds a test scene that can be used as an example of how the plugin can be used."
19-
author="Albinaask, avivajpeyi, Chrisknyfe, cstaaben, florianvazelle, SeannMoser"
20-
version="1.1.2"
19+
author="albinaask, avivajpeyi, Chrisknyfe, cstaaben, florianvazelle, SeannMoser"
20+
version="1.2.0"
2121
script="plugin.gd"

addons/logger/settings.gd

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
# Settings
22

33
## Controls how the message should be formatted, follows String.format(), valid keys are: "level", "time", "log_name", "message"
4-
const LOG_MESSAGE_FORMAT_KEY := "addons/logger/log_message_format"
5-
const LOG_MESSAGE_FORMAT_DEFAULT_VALUE := "{log_name}/{level} [lb]{hour}:{minute}:{second}[rb] {message}"
4+
const LOG_MESSAGE_FORMAT_KEY = "addons/Log/log_message_format"
5+
##BBCode friendly, aka any BBCode may be inserted here.
6+
const LOG_MESSAGE_FORMAT_DEFAULT_VALUE = "{log_name}/{level} [lb]{hour}:{minute}:{second}[rb] {message}"
67

78
## Whether to use the UTC time or the user
8-
const USE_UTC_TIME_FORMAT_KEY := "addons/logger/use_utc_time_format"
9-
const USE_UTC_TIME_FORMAT_DEFAULT_VALUE := false
9+
const USE_UTC_TIME_FORMAT_KEY = "addons/Log/use_utc_time_format"
10+
const USE_UTC_TIME_FORMAT_DEFAULT_VALUE = false
1011

1112
## Enables a breakpoint to mimic the godot behavior where the application doesn't crash when connected to debug environment,
1213
## but instead freezed and shows the stack etc in the debug panel.
13-
const BREAK_ON_ERROR_KEY := "addons/logger/break_on_error"
14-
const BREAK_ON_ERROR_DEFAULT_VALUE := true
14+
const BREAK_ON_ERROR_KEY = "addons/Log/break_on_error"
15+
const BREAK_ON_ERROR_DEFAULT_VALUE = true
16+
17+
18+
##Whether to dump the tree to the log on error.
19+
const PRINT_TREE_ON_ERROR_KEY = "addons/Log/print_tree_on_error"
20+
const PRINT_TREE_ON_ERROR_DEFAULT_VALUE = false

tests/logtest.tscn

+3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ func _ready():
1111
_break_on_error_current_val = ProjectSettings.get_setting(Log.settings.BREAK_ON_ERROR_KEY)
1212

1313
##Print \"something\" to the main log channel
14+
var start = Time.get_ticks_usec()
1415
Log.info(\"something\")
16+
var end = Time.get_ticks_usec()
17+
print(\"a log message takes \" + str(end-start) + \" us\")
1518
Log.info(\"testing a value\", 5)
1619
Log.info(\"testing more values\", {\"bar\":Vector3(1,2,3)})
1720
#Does not show since the default minimum log level for a given logstream is set to info, and debug < info.

0 commit comments

Comments
 (0)