add logging
This commit is contained in:
@@ -2,16 +2,26 @@ extends Node
|
||||
|
||||
signal debug_toggled(enabled: bool)
|
||||
|
||||
enum LogLevel {
|
||||
TRACE = 0,
|
||||
DEBUG = 1,
|
||||
INFO = 2,
|
||||
WARN = 3,
|
||||
ERROR = 4,
|
||||
FATAL = 5
|
||||
}
|
||||
|
||||
var debug_enabled: bool = false
|
||||
var debug_overlay_visible: bool = false
|
||||
var current_log_level: LogLevel = LogLevel.INFO
|
||||
|
||||
func _ready():
|
||||
print("DebugManager loaded")
|
||||
log_info("DebugManager loaded")
|
||||
|
||||
func toggle_debug():
|
||||
debug_enabled = !debug_enabled
|
||||
debug_toggled.emit(debug_enabled)
|
||||
print("Debug mode: ", "ON" if debug_enabled else "OFF")
|
||||
log_info("Debug mode: " + ("ON" if debug_enabled else "OFF"))
|
||||
|
||||
func set_debug_enabled(enabled: bool):
|
||||
if debug_enabled != enabled:
|
||||
@@ -30,6 +40,70 @@ func set_overlay_visible(visible: bool):
|
||||
func is_overlay_visible() -> bool:
|
||||
return debug_overlay_visible
|
||||
|
||||
func log_debug(message: String, category: String = "DEBUG"):
|
||||
if debug_enabled:
|
||||
print("[", category, "] ", message)
|
||||
func set_log_level(level: LogLevel):
|
||||
current_log_level = level
|
||||
log_info("Log level set to: " + _log_level_to_string(level))
|
||||
|
||||
func get_log_level() -> LogLevel:
|
||||
return current_log_level
|
||||
|
||||
func _should_log(level: LogLevel) -> bool:
|
||||
return level >= current_log_level
|
||||
|
||||
func _log_level_to_string(level: LogLevel) -> String:
|
||||
match level:
|
||||
LogLevel.TRACE:
|
||||
return "TRACE"
|
||||
LogLevel.DEBUG:
|
||||
return "DEBUG"
|
||||
LogLevel.INFO:
|
||||
return "INFO"
|
||||
LogLevel.WARN:
|
||||
return "WARN"
|
||||
LogLevel.ERROR:
|
||||
return "ERROR"
|
||||
LogLevel.FATAL:
|
||||
return "FATAL"
|
||||
_:
|
||||
return "UNKNOWN"
|
||||
|
||||
func _format_log_message(level: LogLevel, message: String, category: String = "") -> String:
|
||||
var timestamp = Time.get_datetime_string_from_system()
|
||||
var level_str = _log_level_to_string(level)
|
||||
var category_str = (" [" + category + "]") if category != "" else ""
|
||||
return "[%s] %s%s: %s" % [timestamp, level_str, category_str, message]
|
||||
|
||||
func log_trace(message: String, category: String = ""):
|
||||
if _should_log(LogLevel.TRACE):
|
||||
var formatted = _format_log_message(LogLevel.TRACE, message, category)
|
||||
if debug_enabled:
|
||||
print(formatted)
|
||||
|
||||
func log_debug(message: String, category: String = ""):
|
||||
if _should_log(LogLevel.DEBUG):
|
||||
var formatted = _format_log_message(LogLevel.DEBUG, message, category)
|
||||
if debug_enabled:
|
||||
print(formatted)
|
||||
|
||||
func log_info(message: String, category: String = ""):
|
||||
if _should_log(LogLevel.INFO):
|
||||
var formatted = _format_log_message(LogLevel.INFO, message, category)
|
||||
print(formatted)
|
||||
|
||||
func log_warn(message: String, category: String = ""):
|
||||
if _should_log(LogLevel.WARN):
|
||||
var formatted = _format_log_message(LogLevel.WARN, message, category)
|
||||
print(formatted)
|
||||
push_warning(formatted)
|
||||
|
||||
func log_error(message: String, category: String = ""):
|
||||
if _should_log(LogLevel.ERROR):
|
||||
var formatted = _format_log_message(LogLevel.ERROR, message, category)
|
||||
print(formatted)
|
||||
push_error(formatted)
|
||||
|
||||
func log_fatal(message: String, category: String = ""):
|
||||
if _should_log(LogLevel.FATAL):
|
||||
var formatted = _format_log_message(LogLevel.FATAL, message, category)
|
||||
print(formatted)
|
||||
push_error(formatted)
|
||||
|
||||
Reference in New Issue
Block a user