sandbox logger (wip)

This commit is contained in:
Nordup 2024-10-30 07:28:05 +04:00
parent 61d0534f07
commit 5e4a776dde
6 changed files with 84 additions and 18 deletions

View file

@ -0,0 +1,56 @@
extends Node
class_name SandboxLogger
@export var gate_events: GateEvents
const LOG_FOLDER := "user://logs"
const LOG_FILE := "log.txt"
var log_file: FileAccess
var pipe: Dictionary
var is_started: bool
var logs_sent: bool
func _ready() -> void:
gate_events.not_responding.connect(send_logs)
func start(_pipe: Dictionary, gate: Gate) -> void:
pipe = _pipe
is_started = true
var gate_folder = gate.url.replace("http://", "").replace("https://", "").replace(".gate", "")
var path = LOG_FOLDER + "/" + gate_folder + "/" + LOG_FILE
DirAccess.make_dir_recursive_absolute(path.get_base_dir())
log_file = FileAccess.open(path, FileAccess.WRITE_READ)
Debug.logclr("Logs written to " + path, Color.DIM_GRAY)
func _process(_delta: float) -> void:
if not is_started: return
if pipe["stdio"].is_open():
var buffer = PackedByteArray()
while true:
buffer.append_array(pipe["stdio"].get_buffer(2048))
if pipe["stdio"].get_error() != OK:
break;
while true:
buffer.append_array(pipe["stderr"].get_buffer(2048))
if pipe["stderr"].get_error() != OK:
break;
if !buffer.is_empty():
printraw(buffer.get_string_from_utf8())
log_file.store_buffer(buffer)
func send_logs() -> void:
if logs_sent: return
logs_sent = true
Debug.logr("logs sent")

View file

@ -3,12 +3,13 @@ class_name SandboxManager
@export var gate_events: GateEvents
@export var render_result: RenderResult
@export var snbx_logger: SandboxLogger
@export var snbx_executable: SandboxExecutable
@export var snbx_env: SandboxEnv
const IPC_FOLDER := "sandbox"
var sandbox_pid: int
var snbx_pid: int
func _ready() -> void:
@ -41,9 +42,12 @@ func start_sandbox_linux(gate: Gate) -> void:
"--resolution", "%dx%d" % [render_result.width, render_result.height]
]
if OS.is_stdout_verbose(): args += ["--verbose"]
args += ["--verbose"]
Debug.logclr(snbx_env.start + " " + " ".join(args), Color.DIM_GRAY)
sandbox_pid = OS.create_process(snbx_env.start, args)
var pipe = OS.execute_with_pipe(snbx_env.start, args, false)
snbx_logger.start(pipe, gate)
snbx_pid = pipe["pid"]
gate_events.gate_entered_emit()
@ -64,7 +68,7 @@ func start_sandbox_windows(gate: Gate) -> void:
if OS.is_stdout_verbose(): args += ["--verbose"]
Debug.logclr(snbx_executable.path + " " + " ".join(args), Color.DIM_GRAY)
sandbox_pid = OS.create_process(snbx_executable.path, args)
snbx_pid = OS.create_process(snbx_executable.path, args)
gate_events.gate_entered_emit()
@ -83,7 +87,7 @@ func start_sandbox_macos(gate: Gate) -> void:
if OS.is_stdout_verbose(): args += ["--verbose"]
Debug.logclr(snbx_executable.path + " " + " ".join(args), Color.DIM_GRAY)
sandbox_pid = OS.create_process(snbx_executable.path, args)
snbx_pid = OS.create_process(snbx_executable.path, args)
gate_events.gate_entered_emit()
@ -101,10 +105,10 @@ func kill_sandbox() -> void:
func kill_sandbox_linux() -> void:
if sandbox_pid == 0: return
if snbx_pid == 0: return
var pids = snbx_env.get_subprocesses(sandbox_pid)
pids.append(sandbox_pid)
var pids = snbx_env.get_subprocesses(snbx_pid)
pids.append(snbx_pid)
for pid in pids:
OS.kill(pid)
@ -114,15 +118,15 @@ func kill_sandbox_linux() -> void:
func kill_sandbox_windows() -> void:
if OS.is_process_running(sandbox_pid):
OS.kill(sandbox_pid)
Debug.logclr("Process killed " + str(sandbox_pid), Color.DIM_GRAY)
if OS.is_process_running(snbx_pid):
OS.kill(snbx_pid)
Debug.logclr("Process killed " + str(snbx_pid), Color.DIM_GRAY)
func kill_sandbox_macos() -> void:
if OS.is_process_running(sandbox_pid):
OS.kill(sandbox_pid)
Debug.logclr("Process killed " + str(sandbox_pid), Color.DIM_GRAY)
if OS.is_process_running(snbx_pid):
OS.kill(snbx_pid)
Debug.logclr("Process killed " + str(snbx_pid), Color.DIM_GRAY)
func _exit_tree() -> void: