55 lines
1021 B
GDScript
55 lines
1021 B
GDScript
extends Node
|
|
class_name Logger
|
|
|
|
var thread: Thread = Thread.new()
|
|
var mutex: Mutex = Mutex.new()
|
|
var log_queue: Array[String] = []
|
|
var running: bool = true
|
|
|
|
|
|
func _ready():
|
|
thread.start(log_writer)
|
|
|
|
|
|
func info(msg: Variant):
|
|
mutex.lock()
|
|
log_queue.append("[color=white][b]INFO:[/b] [/color]" + msg)
|
|
mutex.unlock()
|
|
|
|
|
|
func debug(msg: Variant):
|
|
mutex.lock()
|
|
log_queue.append("[color=cyan][b]DEBUG:[/b] [/color]" + msg)
|
|
mutex.unlock()
|
|
|
|
|
|
func warning(msg: Variant):
|
|
mutex.lock()
|
|
log_queue.append("[color=yellow][b]WARN:[/b] [/color]" + msg)
|
|
push_warning(msg)
|
|
mutex.unlock()
|
|
|
|
|
|
func error(msg: Variant):
|
|
mutex.lock()
|
|
log_queue.append("[color=red][b]ERROR:[/b] [/color]" + msg)
|
|
push_error(msg)
|
|
mutex.unlock()
|
|
|
|
|
|
func log_writer():
|
|
while running:
|
|
mutex.lock()
|
|
while log_queue.size() > 0:
|
|
var log_message = log_queue.pop_front()
|
|
print_rich(log_message)
|
|
mutex.unlock()
|
|
|
|
# Prevent high CPU usage
|
|
await get_tree().create_timer(0.1).timeout
|
|
|
|
|
|
func _exit_tree():
|
|
running = false
|
|
thread.wait_to_finish()
|