diff --git a/core/bind/logger_bind.cpp b/core/bind/logger_bind.cpp index 301c9d8bf..6d90417d0 100644 --- a/core/bind/logger_bind.cpp +++ b/core/bind/logger_bind.cpp @@ -26,6 +26,13 @@ void _PLogger::log_error(const String &str) { PLogger::log_error(str); } +_PLogger::LogLevel _PLogger::get_log_level() { + return static_cast(static_cast(PLogger::get_log_level())); +} +void _PLogger::set_log_level(const LogLevel p_log_level) { + PLogger::set_log_level(static_cast(static_cast(p_log_level))); +} + _PLogger *_PLogger::get_singleton() { return _self; } @@ -46,6 +53,16 @@ void _PLogger::_bind_methods() { ClassDB::bind_method(D_METHOD("log_message", "str"), &_PLogger::log_message); ClassDB::bind_method(D_METHOD("log_warning", "str"), &_PLogger::log_warning); ClassDB::bind_method(D_METHOD("log_error", "str"), &_PLogger::log_error); + + ClassDB::bind_method(D_METHOD("get_log_level"), &_PLogger::get_log_level); + ClassDB::bind_method(D_METHOD("set_log_level", "log_level"), &_PLogger::set_log_level); + ADD_PROPERTY(PropertyInfo(Variant::INT, "log_level", PROPERTY_HINT_ENUM, "Trace,Message,Warning,Error,None"), "set_log_level", "get_log_level"); + + BIND_ENUM_CONSTANT(LOG_LEVEL_TRACE); + BIND_ENUM_CONSTANT(LOG_LEVEL_MESSAGE); + BIND_ENUM_CONSTANT(LOG_LEVEL_WARNING); + BIND_ENUM_CONSTANT(LOG_LEVEL_ERROR); + BIND_ENUM_CONSTANT(LOG_LEVEL_NONE); } _PLogger *_PLogger::_self = nullptr; diff --git a/core/bind/logger_bind.h b/core/bind/logger_bind.h index 94443d3a5..002af4ebc 100644 --- a/core/bind/logger_bind.h +++ b/core/bind/logger_bind.h @@ -13,6 +13,14 @@ class _PLogger : public Object { GDCLASS(_PLogger, Object); public: + enum LogLevel { + LOG_LEVEL_TRACE = 0, + LOG_LEVEL_MESSAGE, + LOG_LEVEL_WARNING, + LOG_LEVEL_ERROR, + LOG_LEVEL_NONE, + }; + Ref get_backend(); void set_backend(const Ref &backend); @@ -20,6 +28,9 @@ public: void log_message(const String &str); void log_warning(const String &str); void log_error(const String &str); + + LogLevel get_log_level(); + void set_log_level(const LogLevel p_log_level); static _PLogger *get_singleton(); @@ -32,4 +43,6 @@ protected: static _PLogger *_self; }; +VARIANT_ENUM_CAST(_PLogger::LogLevel); + #endif diff --git a/core/log/logger.cpp b/core/log/logger.cpp index d9efa8dcf..2671e8a78 100644 --- a/core/log/logger.cpp +++ b/core/log/logger.cpp @@ -5,6 +5,10 @@ #include "core/typedefs.h" void PLogger::log_trace(const String &str) { + if (_log_level > LOG_LEVEL_TRACE) { + return; + } + String s; s += "T "; s += str; @@ -13,6 +17,10 @@ void PLogger::log_trace(const String &str) { do_log_trace(s); } void PLogger::log_trace(const char *str) { + if (_log_level > LOG_LEVEL_TRACE) { + return; + } + String s; s += "T "; s += str; @@ -21,6 +29,10 @@ void PLogger::log_trace(const char *str) { do_log_trace(s); } void PLogger::log_trace(const char *p_function, const char *p_file, int p_line, const char *str) { + if (_log_level > LOG_LEVEL_TRACE) { + return; + } + String s; s += "T | "; s += p_file; @@ -35,6 +47,10 @@ void PLogger::log_trace(const char *p_function, const char *p_file, int p_line, do_log_trace(s); } void PLogger::log_trace(const char *p_function, const char *p_file, int p_line, const String &str) { + if (_log_level > LOG_LEVEL_TRACE) { + return; + } + String s; s += "T | "; s += p_file; @@ -50,6 +66,10 @@ void PLogger::log_trace(const char *p_function, const char *p_file, int p_line, } void PLogger::log_message(const String &str) { + if (_log_level > LOG_LEVEL_MESSAGE) { + return; + } + String s; s += "M "; s += str; @@ -58,6 +78,10 @@ void PLogger::log_message(const String &str) { do_log_trace(s); } void PLogger::log_message(const char *str) { + if (_log_level > LOG_LEVEL_MESSAGE) { + return; + } + String s; s += "M "; s += str; @@ -66,6 +90,10 @@ void PLogger::log_message(const char *str) { do_log_trace(s); } void PLogger::log_message(const char *p_function, const char *p_file, int p_line, const char *str) { + if (_log_level > LOG_LEVEL_MESSAGE) { + return; + } + String s; s += "M | "; s += p_file; @@ -80,6 +108,10 @@ void PLogger::log_message(const char *p_function, const char *p_file, int p_line do_log_trace(s); } void PLogger::log_message(const char *p_function, const char *p_file, int p_line, const String &str) { + if (_log_level > LOG_LEVEL_MESSAGE) { + return; + } + String s; s += "M | "; s += p_file; @@ -95,6 +127,10 @@ void PLogger::log_message(const char *p_function, const char *p_file, int p_line } void PLogger::log_warning(const String &str) { + if (_log_level > LOG_LEVEL_WARNING) { + return; + } + String s; s += "W "; s += str; @@ -103,6 +139,10 @@ void PLogger::log_warning(const String &str) { do_log_trace(s); } void PLogger::log_warning(const char *str) { + if (_log_level > LOG_LEVEL_WARNING) { + return; + } + String s; s += "W "; s += str; @@ -111,6 +151,10 @@ void PLogger::log_warning(const char *str) { do_log_trace(s); } void PLogger::log_warning(const char *p_function, const char *p_file, int p_line, const char *str) { + if (_log_level > LOG_LEVEL_WARNING) { + return; + } + String s; s += "W | "; s += p_file; @@ -125,6 +169,10 @@ void PLogger::log_warning(const char *p_function, const char *p_file, int p_line do_log_trace(s); } void PLogger::log_warning(const char *p_function, const char *p_file, int p_line, const String &str) { + if (_log_level > LOG_LEVEL_WARNING) { + return; + } + String s; s += "W | "; s += p_file; @@ -140,6 +188,10 @@ void PLogger::log_warning(const char *p_function, const char *p_file, int p_line } void PLogger::log_error(const String &str) { + if (_log_level > LOG_LEVEL_ERROR) { + return; + } + String s; s += "E "; s += str; @@ -148,6 +200,10 @@ void PLogger::log_error(const String &str) { do_log_trace(s); } void PLogger::log_error(const char *str) { + if (_log_level > LOG_LEVEL_ERROR) { + return; + } + String s; s += "E "; s += str; @@ -156,6 +212,10 @@ void PLogger::log_error(const char *str) { do_log_trace(s); } void PLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *str) { + if (_log_level > LOG_LEVEL_ERROR) { + return; + } + String s; s += "E | "; s += p_file; @@ -170,6 +230,10 @@ void PLogger::log_error(const char *p_function, const char *p_file, int p_line, do_log_trace(s); } void PLogger::log_error(const char *p_function, const char *p_file, int p_line, const String &str) { + if (_log_level > LOG_LEVEL_ERROR) { + return; + } + String s; s += "E | "; s += p_file; @@ -216,4 +280,12 @@ void PLogger::do_log_error(const String &str) { } } +PLogger::LogLevel PLogger::get_log_level() { + return _log_level; +} +void PLogger::set_log_level(const LogLevel p_log_level) { + _log_level = p_log_level; +} + +PLogger::LogLevel PLogger::_log_level = LOG_LEVEL_TRACE; Ref PLogger::_backend; diff --git a/core/log/logger.h b/core/log/logger.h index 55e7f5132..3de02c17a 100644 --- a/core/log/logger.h +++ b/core/log/logger.h @@ -23,6 +23,14 @@ class String; class PLogger : public Object { public: + enum LogLevel { + LOG_LEVEL_TRACE = 0, + LOG_LEVEL_MESSAGE, + LOG_LEVEL_WARNING, + LOG_LEVEL_ERROR, + LOG_LEVEL_NONE, + }; + static void log_trace(const String &str); static void log_trace(const char *str); static void log_trace(const char *p_function, const char *p_file, int p_line, const String &str); @@ -47,7 +55,11 @@ public: static void do_log_message(const String &str); static void do_log_warning(const String &str); static void do_log_error(const String &str); + + static LogLevel get_log_level(); + static void set_log_level(const LogLevel p_log_level); + static LogLevel _log_level; static Ref _backend; };