Added log level support for PLogger.

This commit is contained in:
Relintai 2023-10-25 01:25:15 +02:00
parent de91cf5873
commit b4f0ef4493
4 changed files with 114 additions and 0 deletions

View File

@ -26,6 +26,13 @@ void _PLogger::log_error(const String &str) {
PLogger::log_error(str); PLogger::log_error(str);
} }
_PLogger::LogLevel _PLogger::get_log_level() {
return static_cast<LogLevel>(static_cast<int>(PLogger::get_log_level()));
}
void _PLogger::set_log_level(const LogLevel p_log_level) {
PLogger::set_log_level(static_cast<PLogger::LogLevel>(static_cast<int>(p_log_level)));
}
_PLogger *_PLogger::get_singleton() { _PLogger *_PLogger::get_singleton() {
return _self; 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_message", "str"), &_PLogger::log_message);
ClassDB::bind_method(D_METHOD("log_warning", "str"), &_PLogger::log_warning); 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("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; _PLogger *_PLogger::_self = nullptr;

View File

@ -13,6 +13,14 @@ class _PLogger : public Object {
GDCLASS(_PLogger, Object); GDCLASS(_PLogger, Object);
public: public:
enum LogLevel {
LOG_LEVEL_TRACE = 0,
LOG_LEVEL_MESSAGE,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR,
LOG_LEVEL_NONE,
};
Ref<LoggerBackend> get_backend(); Ref<LoggerBackend> get_backend();
void set_backend(const Ref<LoggerBackend> &backend); void set_backend(const Ref<LoggerBackend> &backend);
@ -20,6 +28,9 @@ public:
void log_message(const String &str); void log_message(const String &str);
void log_warning(const String &str); void log_warning(const String &str);
void log_error(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(); static _PLogger *get_singleton();
@ -32,4 +43,6 @@ protected:
static _PLogger *_self; static _PLogger *_self;
}; };
VARIANT_ENUM_CAST(_PLogger::LogLevel);
#endif #endif

View File

@ -5,6 +5,10 @@
#include "core/typedefs.h" #include "core/typedefs.h"
void PLogger::log_trace(const String &str) { void PLogger::log_trace(const String &str) {
if (_log_level > LOG_LEVEL_TRACE) {
return;
}
String s; String s;
s += "T "; s += "T ";
s += str; s += str;
@ -13,6 +17,10 @@ void PLogger::log_trace(const String &str) {
do_log_trace(s); do_log_trace(s);
} }
void PLogger::log_trace(const char *str) { void PLogger::log_trace(const char *str) {
if (_log_level > LOG_LEVEL_TRACE) {
return;
}
String s; String s;
s += "T "; s += "T ";
s += str; s += str;
@ -21,6 +29,10 @@ void PLogger::log_trace(const char *str) {
do_log_trace(s); do_log_trace(s);
} }
void PLogger::log_trace(const char *p_function, const char *p_file, int p_line, const char *str) { 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; String s;
s += "T | "; s += "T | ";
s += p_file; 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); do_log_trace(s);
} }
void PLogger::log_trace(const char *p_function, const char *p_file, int p_line, const String &str) { 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; String s;
s += "T | "; s += "T | ";
s += p_file; 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) { void PLogger::log_message(const String &str) {
if (_log_level > LOG_LEVEL_MESSAGE) {
return;
}
String s; String s;
s += "M "; s += "M ";
s += str; s += str;
@ -58,6 +78,10 @@ void PLogger::log_message(const String &str) {
do_log_trace(s); do_log_trace(s);
} }
void PLogger::log_message(const char *str) { void PLogger::log_message(const char *str) {
if (_log_level > LOG_LEVEL_MESSAGE) {
return;
}
String s; String s;
s += "M "; s += "M ";
s += str; s += str;
@ -66,6 +90,10 @@ void PLogger::log_message(const char *str) {
do_log_trace(s); do_log_trace(s);
} }
void PLogger::log_message(const char *p_function, const char *p_file, int p_line, const char *str) { 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; String s;
s += "M | "; s += "M | ";
s += p_file; 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); do_log_trace(s);
} }
void PLogger::log_message(const char *p_function, const char *p_file, int p_line, const String &str) { 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; String s;
s += "M | "; s += "M | ";
s += p_file; 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) { void PLogger::log_warning(const String &str) {
if (_log_level > LOG_LEVEL_WARNING) {
return;
}
String s; String s;
s += "W "; s += "W ";
s += str; s += str;
@ -103,6 +139,10 @@ void PLogger::log_warning(const String &str) {
do_log_trace(s); do_log_trace(s);
} }
void PLogger::log_warning(const char *str) { void PLogger::log_warning(const char *str) {
if (_log_level > LOG_LEVEL_WARNING) {
return;
}
String s; String s;
s += "W "; s += "W ";
s += str; s += str;
@ -111,6 +151,10 @@ void PLogger::log_warning(const char *str) {
do_log_trace(s); do_log_trace(s);
} }
void PLogger::log_warning(const char *p_function, const char *p_file, int p_line, const char *str) { 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; String s;
s += "W | "; s += "W | ";
s += p_file; 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); do_log_trace(s);
} }
void PLogger::log_warning(const char *p_function, const char *p_file, int p_line, const String &str) { 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; String s;
s += "W | "; s += "W | ";
s += p_file; 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) { void PLogger::log_error(const String &str) {
if (_log_level > LOG_LEVEL_ERROR) {
return;
}
String s; String s;
s += "E "; s += "E ";
s += str; s += str;
@ -148,6 +200,10 @@ void PLogger::log_error(const String &str) {
do_log_trace(s); do_log_trace(s);
} }
void PLogger::log_error(const char *str) { void PLogger::log_error(const char *str) {
if (_log_level > LOG_LEVEL_ERROR) {
return;
}
String s; String s;
s += "E "; s += "E ";
s += str; s += str;
@ -156,6 +212,10 @@ void PLogger::log_error(const char *str) {
do_log_trace(s); do_log_trace(s);
} }
void PLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *str) { 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; String s;
s += "E | "; s += "E | ";
s += p_file; 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); do_log_trace(s);
} }
void PLogger::log_error(const char *p_function, const char *p_file, int p_line, const String &str) { 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; String s;
s += "E | "; s += "E | ";
s += p_file; 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<LoggerBackend> PLogger::_backend; Ref<LoggerBackend> PLogger::_backend;

View File

@ -23,6 +23,14 @@ class String;
class PLogger : public Object { class PLogger : public Object {
public: 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 String &str);
static void log_trace(const char *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); 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_message(const String &str);
static void do_log_warning(const String &str); static void do_log_warning(const String &str);
static void do_log_error(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<LoggerBackend> _backend; static Ref<LoggerBackend> _backend;
}; };