Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Logger
继承: RefCounted < Object
自定义日志记录器,能够从内部错误/警告流接收消息。
描述
自定义日志记录器,能够从内部错误/警告流接收消息。日志记录器通过 OS.add_logger() 注册。
教程
方法
void |
_log_error(function: String, file: String, line: int, code: String, rationale: String, editor_notify: bool, error_type: int, script_backtraces: Array[ScriptBacktrace]) virtual |
void |
_log_message(message: String, error: bool) virtual |
枚举
enum ErrorType: 🔗
ErrorType ERROR_TYPE_ERROR = 0
接收到的消息是错误。
ErrorType ERROR_TYPE_WARNING = 1
接收到的消息是警告。
ErrorType ERROR_TYPE_SCRIPT = 2
接收到的消息是脚本错误。
ErrorType ERROR_TYPE_SHADER = 3
接收到的消息是着色器错误。
方法说明
void _log_error(function: String, file: String, line: int, code: String, rationale: String, editor_notify: bool, error_type: int, script_backtraces: Array[ScriptBacktrace]) virtual 🔗
当有错误被记录时会调用此方法。该错误会提供其来源的 function(函数名)、file(文件名)和 line(行号),以及生成该错误的 code(错误代码)或一段 rationale(原因说明)。
error_type 所提供的错误类型在 ErrorType 枚举中有具体描述。
此外,script_backtraces 会为每种脚本语言提供对应的回溯信息。默认情况下,这些信息仅在编辑器构建(editor builds)和调试构建(debug builds)中包含堆栈帧。如果你希望在发布构建(release builds)中也启用它们,需要开启 ProjectSettings.debug/settings/gdscript/always_track_call_stacks。
警告: 此方法可能会被主线程以外的其他线程调用,甚至可能同时被调用。因此,在实现该方法时,你需要采取某种线程安全措施,比如使用 Mutex(互斥锁)。
注意: script_backtraces 不会包含任何被捕获的变量,因为这样做的性能开销极高。如果你需要获取这些变量,需要在 Logger 的虚方法内部,使用 Engine.capture_script_backtraces() 自行捕获回溯信息。
注意: 不支持在此方法内部使用类似 @GlobalScope.push_error() 或 @GlobalScope.push_warning() 这样的函数来记录错误,因为这可能会导致无限递归。这些错误只会显示在控制台输出中。
void _log_message(message: String, error: bool) virtual 🔗
当记录消息时调用。如果 error 为 true,则该消息应发送到 stderr。
警告:该方法可能会从主线程以外的其他线程调用,甚至可能同时被多个线程调用,因此需要在实现该方法时确保线程安全,例如使用 Mutex。
注意:不支持在该方法中使用 @GlobalScope.print() 等函数记录其他消息,因为这可能导致无限递归。这些消息只会显示在控制台输出中。