############################################################################## ### CORE FRAMEWORK SOURCE FILE ### ### Copyright (c) 2024 The StarOpenSource Project & Contributors ### ### Licensed under the GNU General Public License v3 ### ### ### ### This program is free software: you can redistribute it and/or modify ### ### it under the terms of the GNU General Public License as published by ### ### the Free Software Foundation, either version 3 of the License, or ### ### (at your option) any later version. ### ### ### ### This program is distributed in the hope that it will be useful, ### ### but WITHOUT ANY WARRANTY; without even the implied warranty of ### ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ### ### GNU General Public License for more details. ### ### ### ### You should have received a copy of the GNU General Public License ### ### along with this program. If not, see . ### ############################################################################## ### src/logger.gd (Logger implementation) ### ### ### ### Prints formatted strings into the console/log. ### ############################################################################## extends CoreBaseModule # Signals signal log_event # Configuration var config_level: CoreTypes.LoggerLevel var config_colored: bool var config_format: String var config_newlines_override: bool var config_newlines_sizelimit: int # Update configuration func _pull_config() -> void: config_level = core.config.logger_level config_colored = core.config.logger_colored config_format = core.config.logger_format config_newlines_override = core.config.logger_newlines_override config_newlines_sizelimit = core.config.logger_newlines_sizelimit # Creates log messages func _log(level: CoreTypes.LoggerLevel, message: String) -> void: var origin: Dictionary = get_origin() if !is_level_allowed(level): emit_signal("log_event", false, level, origin, message, "") return var format: String = config_format format = format.replace("%time_ms%", str(Time.get_ticks_msec())) format = format.replace("%time%", Time.get_time_string_from_system(true)) format = format.replace("%source%", origin["source_clean"]) format = format.replace("%source_raw%", origin["source"]) format = format.replace("%function%", origin["function"]) format = format.replace("%line%", str(origin["line"])) var format_newline: String = format.replace("%color%", "").replace("%message%", "") if !config_colored: format = format.replace("%color%", "") match(level): CoreTypes.LoggerLevel.DIAG: format = format.replace("%level%", "DIAG") format_newline = format_newline.replace("%level%", "DIAG") format.replace("%color%", "[color=dark_gray]") CoreTypes.LoggerLevel.VERB: format = format.replace("%level%", "VERB") format_newline = format_newline.replace("%level%", "VERB") format = format.replace("%color%", "[color=gray]") CoreTypes.LoggerLevel.INFO: format = format.replace("%level%", "INFO") format_newline = format_newline.replace("%level%", "INFO") format = format.replace("%color%", "[color=white]") CoreTypes.LoggerLevel.WARN: format = format.replace("%level%", "WARN") format_newline = format_newline.replace("%level%", "WARN") format = format.replace("%color%", "[color=yellow]") CoreTypes.LoggerLevel.ERROR: format = format.replace("%level%", "ERR!") format_newline = format_newline.replace("%level%", "ERR!") format = format.replace("%color%", "[color=red]") CoreTypes.LoggerLevel.NONE: format = format.replace("%level%", "NONE") format_newline = format_newline.replace("%level%", "NONE") format = format.replace("%color%", "[color=black]") # Replace %message% if config_newlines_override and config_newlines_sizelimit <= -1 or format_newline.length() <= config_newlines_sizelimit: message = message.replace("\n", "\n" + " ".repeat(format_newline.length())) format = format.replace("%message%", message) emit_signal("log_event", true, level, origin, message, format) if config_colored: print_rich(format) else: print(format) # Get function caller func get_origin() -> Dictionary: var stack: Dictionary = get_stack()[3] return { "source": stack["source"], "source_clean": stack["source"].replace("res://", "").replace("user://", ""), "function": stack["function"], "line": stack["line"] } # Check if level is allowed func is_level_allowed(level: CoreTypes.LoggerLevel) -> bool: if level <= config_level: return true else: return false # Self explanitory func diag(message: String) -> void: _log(CoreTypes.LoggerLevel.DIAG, message) func verb(message: String) -> void: _log(CoreTypes.LoggerLevel.VERB, message) func info(message: String) -> void: _log(CoreTypes.LoggerLevel.INFO, message) func warn(message: String) -> void: _log(CoreTypes.LoggerLevel.WARN, message) func error(message: String) -> void: _log(CoreTypes.LoggerLevel.ERROR, message) func crash(_message: String) -> void: await get_tree().process_frame error("crash() isn't implemented yet")