OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / autoloads / debug_log.gd

58 lines · gd ref:
extends Node
## Debug logging with category filtering.
## Quiet by default — enable categories via --debug flag.
##
## Usage:
##   godot4 --debug terrain,edits,network,game
##   bare index.js --debug terrain,network
##
## In code:
##   DebugLog.log("terrain", "rebuild complete, %d chunks" % count)
##   DebugLog.log("edits", "Box edit at %s" % pos)
##   DebugLog.log("network", "Peer %d joined" % peer_id)
##   DebugLog.log("game", "Grapple fired at %s" % target)
##
## Categories: terrain, edits, network, game, keet, all

var _enabled: Dictionary = {}  # category → true


func _ready() -> void:
	_parse_debug_args()
	if not _enabled.is_empty():
		print("[Debug] Enabled categories: %s" % ", ".join(_enabled.keys()))


func _parse_debug_args() -> void:
	# HTPS_DEBUG as well as --debug: the engine has its own --debug flag, and a
	# session launched with `--debug game` produced no [Debug] Enabled line at all,
	# so the argument is not arriving here intact.
	if OS.has_environment("HTPS_DEBUG"):
		for c in OS.get_environment("HTPS_DEBUG").split(","):
			var cc: String = c.strip_edges().to_lower()
			if cc == "all":
				for a in ["terrain", "edits", "network", "game", "keet"]:
					_enabled[a] = true
			elif cc != "":
				_enabled[cc] = true
	var args: PackedStringArray = OS.get_cmdline_args()
	for i in range(args.size()):
		if args[i] == "--debug" and i + 1 < args.size():
			var cats: PackedStringArray = args[i + 1].split(",")
			for cat in cats:
				var c: String = cat.strip_edges().to_lower()
				if c == "all":
					for a in ["terrain", "edits", "network", "game", "keet"]:
						_enabled[a] = true
				elif c != "":
					_enabled[c] = true


func is_enabled(category: String) -> bool:
	return _enabled.has(category)


func log(category: String, msg: String) -> void:
	if _enabled.has(category):
		print("[%s] %s" % [category, msg])