Skip to content

Commit b1a84f4

Browse files
committed
improvement: log system details
1 parent 9654f4c commit b1a84f4

File tree

3 files changed

+97
-21
lines changed

3 files changed

+97
-21
lines changed

autoload/Log.gd

+65-16
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,23 @@ enum LEVEL {
1111

1212
var also_print_to_godot := false;
1313
var _js_available := OS.has_feature("JavaScript")
14+
var is_joypad = false
15+
var joypad_index = -1
1416
var GDQUEST: JavaScriptObject
1517

1618

19+
func _init() -> void:
20+
var _err = Input.connect("joy_connection_changed", self, "_on_joy_connection_changed")
21+
if not _js_available:
22+
return
23+
GDQUEST = JavaScript.get_interface("GDQUEST")
24+
if not GDQUEST:
25+
_js_available = false
26+
return
27+
trim_if_over_limit()
28+
log_system_info_if_log_is_empty(get_info())
29+
30+
1731
func write(level: int, properties: Dictionary, message: String) -> void:
1832
if also_print_to_godot and OS.is_debug_build():
1933
var level_index := LEVEL.values().find(level)
@@ -33,9 +47,7 @@ func write(level: int, properties: Dictionary, message: String) -> void:
3347
print(object)
3448
if not _js_available:
3549
return
36-
var props = JavaScript.create_object("Object", {})
37-
for key in properties:
38-
props[key] = properties[key]
50+
var props = godot_dict_to_js_obj(properties)
3951
match(level):
4052
LEVEL.FATAL:
4153
# warning-ignore:unsafe_property_access
@@ -57,16 +69,6 @@ func write(level: int, properties: Dictionary, message: String) -> void:
5769
GDQUEST.log.trace(props, message)
5870

5971

60-
func _init() -> void:
61-
if not _js_available:
62-
return
63-
GDQUEST = JavaScript.get_interface("GDQUEST")
64-
if not GDQUEST:
65-
_js_available = false
66-
return
67-
trim_if_over_limit()
68-
69-
7072
func trace(properties: Dictionary, message: String) -> void:
7173
write(LEVEL.TRACE, properties, message)
7274

@@ -102,8 +104,55 @@ func download() -> void:
102104

103105
# Makes sure the log doesn't fill user's localStorage. Safe to call in all
104106
# environments, will no-op when JS is not available.
105-
func trim_if_over_limit(max_kilo_bytes := 1000) -> void:
107+
func trim_if_over_limit(max_kilo_bytes := 1000) -> bool:
106108
if not _js_available:
107-
return
109+
return false
108110
# warning-ignore:unsafe_property_access
109-
GDQUEST.log.trimIfOverLimit(max_kilo_bytes)
111+
return GDQUEST.log.trimIfOverLimit(max_kilo_bytes)
112+
113+
114+
# Logs system info if the log is empty. Safe to call in all environments, will
115+
# no-op when JS is not available.
116+
func log_system_info_if_log_is_empty(additional_data := {}) -> void:
117+
if not _js_available:
118+
return
119+
if additional_data.size() > 0:
120+
var props = godot_dict_to_js_obj(additional_data)
121+
# warning-ignore:unsafe_property_access
122+
GDQUEST.log.logSystemInfoIfLogIsEmpty(props)
123+
else:
124+
# warning-ignore:unsafe_property_access
125+
GDQUEST.log.logSystemInfoIfLogIsEmpty()
126+
127+
128+
func godot_dict_to_js_obj(properties: Dictionary):
129+
var props = JavaScript.create_object("Object", {})
130+
for key in properties:
131+
var value = properties[key]
132+
if value is Dictionary:
133+
value = godot_dict_to_js_obj(value)
134+
elif value is Vector2 or value is Vector3:
135+
value = "%s"%[value]
136+
props[key] = value
137+
return props
138+
139+
140+
func get_info():
141+
var info = {
142+
"OS": OS.get_name(),
143+
"datetime": OS.get_datetime(),
144+
"video_driver": OS.get_video_driver_name(OS.get_current_video_driver()),
145+
"video_adapter": VisualServer.get_video_adapter_name(),
146+
"video_vendor": VisualServer.get_video_adapter_vendor(),
147+
"screen_size": OS.get_screen_size(),
148+
"screen_dpi": OS.get_screen_dpi(),
149+
"cores": OS.get_processor_count(),
150+
"locale": OS.get_locale(),
151+
"joypad": Input.get_joy_name(joypad_index) if is_joypad else ""
152+
}
153+
return info
154+
155+
156+
func _on_joy_connection_changed(device_id, connected):
157+
is_joypad = connected
158+
joypad_index = device_id

html_export/static/bootstrap.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ interface Log {
2323
display: () => void;
2424
clear: () => void;
2525
download: () => void;
26-
trimIfOverLimit: (maxKiloBytes?: number) => void;
26+
trimIfOverLimit: (maxKiloBytes?: number) => boolean;
27+
logSystemInfoIfLogIsEmpty: (additionalData?: Record<string, any>) => void;
2728
get: () => LogLine[];
2829
}
2930
interface GodotEngineInstanceStartGameOptions {

html_export/static/bootstrap.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ window.GDQUEST = ((/** @type {GDQuestLib} */ GDQUEST) => {
7171
*/
7272
const onProgress = (current, total) => {
7373
if (total > 0) {
74-
console.info("loading...", total);
7574
setStatusMode(StatusMode.PROGRESS);
7675
displayPercentage(current / total);
7776
if (current === total) {
@@ -214,8 +213,7 @@ window.GDQUEST = ((/** @type {GDQuestLib} */ GDQUEST) => {
214213
} else {
215214
console.log(msg);
216215
}
217-
}
218-
if (level < 40) {
216+
} else if (level < 40) {
219217
if (anything) {
220218
console.info(msg, anything);
221219
} else {
@@ -247,15 +245,43 @@ window.GDQUEST = ((/** @type {GDQuestLib} */ GDQUEST) => {
247245

248246
/** @type { Log['trimIfOverLimit'] } */
249247
const trimIfOverLimit = (maxKiloBytes = 1000) => {
248+
let response = false;
250249
while (getLocalStorageSize() > maxKiloBytes) {
251250
log_lines.shift();
252251
localStorage.setItem(KEY, JSON.stringify(log_lines));
252+
response = true;
253+
}
254+
return response;
255+
};
256+
257+
const logSystemInfoIfLogIsEmpty = (additionalData = {}) => {
258+
if (log_lines.length == 0) {
259+
const { userAgent, vendor } = navigator;
260+
const { width, height } = screen;
261+
const { innerHeight, innerWidth } = window;
262+
const data = {
263+
userAgent,
264+
vendor,
265+
width,
266+
height,
267+
innerHeight,
268+
innerWidth,
269+
...additionalData,
270+
};
271+
makeLogFunction(LEVELS.TRACE)(data, `INIT`);
253272
}
254273
};
255274

256275
/** @type { Log } */
257276
// @ts-ignore
258-
const log = { display, clear, get, trimIfOverLimit, download };
277+
const log = {
278+
display,
279+
clear,
280+
get,
281+
trimIfOverLimit,
282+
download,
283+
logSystemInfoIfLogIsEmpty,
284+
};
259285
Object.keys(LEVELS).forEach(
260286
(key) => (log[key.toLowerCase()] = makeLogFunction(LEVELS[key]))
261287
);

0 commit comments

Comments
 (0)