Added log_custom method to PLogger. It's a lot more customizable than the others by design. Also small cleanups.

This commit is contained in:
Relintai 2024-11-17 14:51:44 +01:00
parent f4917d1ec4
commit 618e22306a
6 changed files with 171 additions and 25 deletions

View File

@ -60,6 +60,10 @@ void _PLogger::log_important(const String &str) {
PLogger::log_important(str);
}
void _PLogger::log_custom(const StringName &p_category, const int p_level, const String &str) {
PLogger::log_custom(p_category, p_level, str);
}
_PLogger::LogLevel _PLogger::get_log_level() {
return static_cast<LogLevel>(static_cast<int>(PLogger::get_log_level()));
}
@ -88,6 +92,7 @@ void _PLogger::_bind_methods() {
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_important", "str"), &_PLogger::log_important);
ClassDB::bind_method(D_METHOD("log_custom", "category", "level", "str"), &_PLogger::log_custom);
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);

View File

@ -60,6 +60,7 @@ public:
void log_warning(const String &str);
void log_error(const String &str);
void log_important(const String &str);
void log_custom(const StringName &p_category, const int p_level, const String &str);
LogLevel get_log_level();
void set_log_level(const LogLevel p_log_level);

View File

@ -42,7 +42,6 @@ void PLogger::log_trace(const String &str) {
String s;
s += "T ";
s += str;
//s += "\n";
do_log_trace(s);
}
@ -54,7 +53,6 @@ void PLogger::log_trace(const char *str) {
String s;
s += "T ";
s += str;
//s += "\n";
do_log_trace(s);
}
@ -72,7 +70,6 @@ void PLogger::log_trace(const char *p_function, const char *p_file, int p_line,
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";
do_log_trace(s);
}
@ -90,7 +87,6 @@ void PLogger::log_trace(const char *p_function, const char *p_file, int p_line,
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";
do_log_trace(s);
}
@ -103,7 +99,6 @@ void PLogger::log_message(const String &str) {
String s;
s += "M ";
s += str;
//s += "\n";
do_log_message(s);
}
@ -115,7 +110,6 @@ void PLogger::log_message(const char *str) {
String s;
s += "M ";
s += str;
//s += "\n";
do_log_message(s);
}
@ -133,7 +127,6 @@ void PLogger::log_message(const char *p_function, const char *p_file, int p_line
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";
do_log_message(s);
}
@ -151,7 +144,6 @@ void PLogger::log_message(const char *p_function, const char *p_file, int p_line
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";
do_log_message(s);
}
@ -164,7 +156,6 @@ void PLogger::log_warning(const String &str) {
String s;
s += "W ";
s += str;
//s += "\n";
do_log_warning(s);
}
@ -176,7 +167,6 @@ void PLogger::log_warning(const char *str) {
String s;
s += "W ";
s += str;
//s += "\n";
do_log_warning(s);
}
@ -194,7 +184,6 @@ void PLogger::log_warning(const char *p_function, const char *p_file, int p_line
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";
do_log_warning(s);
}
@ -212,7 +201,6 @@ void PLogger::log_warning(const char *p_function, const char *p_file, int p_line
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";
do_log_warning(s);
}
@ -225,7 +213,6 @@ void PLogger::log_error(const String &str) {
String s;
s += "E ";
s += str;
//s += "\n";
do_log_error(s);
}
@ -237,7 +224,6 @@ void PLogger::log_error(const char *str) {
String s;
s += "E ";
s += str;
//s += "\n";
do_log_error(s);
}
@ -255,7 +241,6 @@ void PLogger::log_error(const char *p_function, const char *p_file, int p_line,
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";
do_log_error(s);
}
@ -273,7 +258,6 @@ void PLogger::log_error(const char *p_function, const char *p_file, int p_line,
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";
do_log_error(s);
}
@ -286,7 +270,6 @@ void PLogger::log_important(const String &str) {
String s;
s += "I ";
s += str;
//s += "\n";
do_log_important(s);
}
@ -298,7 +281,6 @@ void PLogger::log_important(const char *str) {
String s;
s += "I ";
s += str;
//s += "\n";
do_log_important(s);
}
@ -316,7 +298,6 @@ void PLogger::log_important(const char *p_function, const char *p_file, int p_li
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";
do_log_important(s);
}
@ -334,11 +315,135 @@ void PLogger::log_important(const char *p_function, const char *p_file, int p_li
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";
do_log_important(s);
}
void PLogger::log_custom(const StringName &p_category, const int p_level, const String &str) {
if (_backend.is_valid()) {
_backend->log_custom(p_category, p_level, str);
return;
}
if (_log_level > p_level) {
return;
}
String s;
s += p_category;
s += " : ";
s += String::num(p_level);
s += " | ";
s += str;
if (p_level < LOG_LEVEL_ERROR) {
force_print_line(s);
} else {
force_print_error(s);
}
}
void PLogger::log_custom(const StringName &p_category, const int p_level, const char *str) {
if (_backend.is_valid()) {
_backend->log_custom(p_category, p_level, str);
return;
}
if (_log_level > p_level) {
return;
}
String s;
s += p_category;
s += " : ";
s += String::num(p_level);
s += " | ";
s += str;
if (p_level < LOG_LEVEL_ERROR) {
force_print_line(s);
} else {
force_print_error(s);
}
}
void PLogger::log_custom(const StringName &p_category, const int p_level, const char *p_function, const char *p_file, int p_line, const char *str) {
if (_backend.is_valid()) {
String s;
s += p_file;
s += "::";
s += p_function;
s += ":";
s += String::num(p_line);
s += " | ";
s += str;
_backend->log_custom(p_category, p_level, s);
return;
}
if (_log_level > p_level) {
return;
}
String s;
s += p_category;
s += " : ";
s += String::num(p_level);
s += " | ";
s += p_file;
s += "::";
s += p_function;
s += ":";
s += String::num(p_line);
s += " | ";
s += str;
if (p_level < LOG_LEVEL_ERROR) {
force_print_line(s);
} else {
force_print_error(s);
}
}
void PLogger::log_custom(const StringName &p_category, const int p_level, const char *p_function, const char *p_file, int p_line, const String &str) {
if (_backend.is_valid()) {
String s;
s += p_file;
s += "::";
s += p_function;
s += ":";
s += String::num(p_line);
s += " | ";
s += str;
_backend->log_custom(p_category, p_level, s);
return;
}
if (_log_level > p_level) {
return;
}
String s;
s += p_category;
s += " : ";
s += String::num(p_level);
s += " | ";
s += p_file;
s += "::";
s += p_function;
s += ":";
s += String::num(p_line);
s += " | ";
s += str;
if (p_level < LOG_LEVEL_ERROR) {
force_print_line(s);
} else {
force_print_error(s);
}
}
void PLogger::do_log_trace(const String &str) {
if (_backend.is_valid()) {
_backend->log_trace(str);

View File

@ -54,6 +54,9 @@ class String;
#define PLOG_IMPORTANT(str) \
PLogger::log_important(__FUNCTION__, __FILE__, __LINE__, str);
#define PLOG_CUSTOM(category, level, str) \
PLogger::log_important(category, level, __FUNCTION__, __FILE__, __LINE__, str);
class PLogger : public Object {
public:
enum LogLevel {
@ -90,6 +93,11 @@ public:
static void log_important(const char *p_function, const char *p_file, int p_line, const char *str);
static void log_important(const char *p_function, const char *p_file, int p_line, const String &str);
static void log_custom(const StringName &p_category, const int p_level, const String &p_str);
static void log_custom(const StringName &p_category, const int p_level, const char *str);
static void log_custom(const StringName &p_category, const int p_level, const char *p_function, const char *p_file, int p_line, const char *str);
static void log_custom(const StringName &p_category, const int p_level, const char *p_function, const char *p_file, int p_line, const String &str);
static void do_log_trace(const String &str);
static void do_log_message(const String &str);
static void do_log_warning(const String &str);

View File

@ -31,6 +31,8 @@
#include "logger_backend.h"
#include "core/io/logger.h"
#include "core/log/logger.h"
#include "core/string/print_string.h"
void LoggerBackend::log_trace(const String &str) {
@ -48,21 +50,38 @@ void LoggerBackend::log_error(const String &str) {
void LoggerBackend::log_important(const String &str) {
call("_log_important", str);
}
void LoggerBackend::log_custom(const StringName &p_category, const int p_level, const String &str) {
call("_log_custom", p_category, p_level, str);
}
void LoggerBackend::_log_trace(const String &str) {
print_line(str);
force_print_line(str);
}
void LoggerBackend::_log_message(const String &str) {
print_line(str);
force_print_line(str);
}
void LoggerBackend::_log_warning(const String &str) {
print_line(str);
force_print_line(str);
}
void LoggerBackend::_log_error(const String &str) {
print_error(str);
force_print_error(str);
}
void LoggerBackend::_log_important(const String &str) {
print_error(str);
force_print_error(str);
}
void LoggerBackend::_log_custom(const StringName &p_category, const int p_level, const String &str) {
String s;
s += p_category;
s += " : ";
s += String::num(p_level);
s += " | ";
s += str;
if (p_level < PLogger::LOG_LEVEL_ERROR) {
force_print_line(s);
} else {
force_print_error(s);
}
}
LoggerBackend::LoggerBackend() {
@ -77,16 +96,22 @@ void LoggerBackend::_bind_methods() {
BIND_VMETHOD(MethodInfo("_log_warning", PropertyInfo(Variant::STRING, "str")));
BIND_VMETHOD(MethodInfo("_log_error", PropertyInfo(Variant::STRING, "str")));
BIND_VMETHOD(MethodInfo("_log_important", PropertyInfo(Variant::STRING, "str")));
BIND_VMETHOD(MethodInfo("_log_custom",
PropertyInfo(Variant::STRING_NAME, "category"),
PropertyInfo(Variant::INT, "level"),
PropertyInfo(Variant::STRING, "str")));
ClassDB::bind_method(D_METHOD("log_trace", "str"), &LoggerBackend::log_trace);
ClassDB::bind_method(D_METHOD("log_message", "str"), &LoggerBackend::log_message);
ClassDB::bind_method(D_METHOD("log_warning", "str"), &LoggerBackend::log_warning);
ClassDB::bind_method(D_METHOD("log_error", "str"), &LoggerBackend::log_error);
ClassDB::bind_method(D_METHOD("log_important", "str"), &LoggerBackend::log_important);
ClassDB::bind_method(D_METHOD("log_custom", "category", "level", "str"), &LoggerBackend::log_custom);
ClassDB::bind_method(D_METHOD("_log_trace", "str"), &LoggerBackend::_log_trace);
ClassDB::bind_method(D_METHOD("_log_message", "str"), &LoggerBackend::_log_message);
ClassDB::bind_method(D_METHOD("_log_warning", "str"), &LoggerBackend::_log_warning);
ClassDB::bind_method(D_METHOD("_log_error", "str"), &LoggerBackend::_log_error);
ClassDB::bind_method(D_METHOD("_log_important", "str"), &LoggerBackend::_log_important);
ClassDB::bind_method(D_METHOD("_log_custom", "category", "level", "str"), &LoggerBackend::log_custom);
}

View File

@ -46,12 +46,14 @@ public:
virtual void log_warning(const String &str);
virtual void log_error(const String &str);
virtual void log_important(const String &str);
virtual void log_custom(const StringName &p_category, const int p_level, const String &str);
virtual void _log_trace(const String &str);
virtual void _log_message(const String &str);
virtual void _log_warning(const String &str);
virtual void _log_error(const String &str);
virtual void _log_important(const String &str);
virtual void _log_custom(const StringName &p_category, const int p_level, const String &str);
LoggerBackend();
~LoggerBackend();