Skip to content

Commit

Permalink
Added a new important log level to PLogger.
Browse files Browse the repository at this point in the history
  • Loading branch information
Relintai committed Nov 16, 2024
1 parent e53f2b2 commit 642db98
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/bind/logger_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ void _PLogger::log_error(const String &str) {
PLogger::log_error(str);
}

void _PLogger::log_important(const String &str) {
PLogger::log_important(str);
}

_PLogger::LogLevel _PLogger::get_log_level() {
return static_cast<LogLevel>(static_cast<int>(PLogger::get_log_level()));
}
Expand Down Expand Up @@ -83,6 +87,7 @@ 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("log_important", "str"), &_PLogger::log_important);

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);
Expand All @@ -92,6 +97,7 @@ void _PLogger::_bind_methods() {
BIND_ENUM_CONSTANT(LOG_LEVEL_MESSAGE);
BIND_ENUM_CONSTANT(LOG_LEVEL_WARNING);
BIND_ENUM_CONSTANT(LOG_LEVEL_ERROR);
BIND_ENUM_CONSTANT(LOG_LEVEL_IMPORTANT);
BIND_ENUM_CONSTANT(LOG_LEVEL_NONE);
}

Expand Down
2 changes: 2 additions & 0 deletions core/bind/logger_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class _PLogger : public Object {
LOG_LEVEL_MESSAGE,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR,
LOG_LEVEL_IMPORTANT,
LOG_LEVEL_NONE,
};

Expand All @@ -58,6 +59,7 @@ class _PLogger : public Object {
void log_message(const String &str);
void log_warning(const String &str);
void log_error(const String &str);
void log_important(const String &str);

LogLevel get_log_level();
void set_log_level(const LogLevel p_log_level);
Expand Down
69 changes: 69 additions & 0 deletions core/log/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,67 @@ void PLogger::log_error(const char *p_function, const char *p_file, int p_line,
do_log_error(s);
}

void PLogger::log_important(const String &str) {
if (_log_level > LOG_LEVEL_IMPORTANT) {
return;
}

String s;
s += "I ";
s += str;
//s += "\n";

do_log_important(s);
}
void PLogger::log_important(const char *str) {
if (_log_level > LOG_LEVEL_IMPORTANT) {
return;
}

String s;
s += "I ";
s += str;
//s += "\n";

do_log_important(s);
}
void PLogger::log_important(const char *p_function, const char *p_file, int p_line, const char *str) {
if (_log_level > LOG_LEVEL_IMPORTANT) {
return;
}

String s;
s += "I | ";
s += p_file;
s += "::";
s += p_function;
s += ":";
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";

do_log_important(s);
}
void PLogger::log_important(const char *p_function, const char *p_file, int p_line, const String &str) {
if (_log_level > LOG_LEVEL_IMPORTANT) {
return;
}

String s;
s += "I | ";
s += p_file;
s += "::";
s += p_function;
s += ":";
s += String::num(p_line);
s += " | ";
s += str;
//s += "\n";

do_log_important(s);
}

void PLogger::do_log_trace(const String &str) {
if (_backend.is_valid()) {
_backend->log_trace(str);
Expand Down Expand Up @@ -310,6 +371,14 @@ void PLogger::do_log_error(const String &str) {
}
}

void PLogger::do_log_important(const String &str) {
if (_backend.is_valid()) {
_backend->log_important(str);
} else {
force_print_error(str);
}
}

PLogger::LogLevel PLogger::get_log_level() {
return _log_level;
}
Expand Down
10 changes: 10 additions & 0 deletions core/log/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ class String;
#define PLOG_ERR(str) \
PLogger::log_error(__FUNCTION__, __FILE__, __LINE__, str);

#define PLOG_IMPORTANT(str) \
PLogger::log_important(__FUNCTION__, __FILE__, __LINE__, str);

class PLogger : public Object {
public:
enum LogLevel {
LOG_LEVEL_TRACE = 0,
LOG_LEVEL_MESSAGE,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR,
LOG_LEVEL_IMPORTANT,
LOG_LEVEL_NONE,
};

Expand All @@ -81,10 +85,16 @@ class PLogger : public Object {
static void log_error(const char *p_function, const char *p_file, int p_line, const char *str);
static void log_error(const char *p_function, const char *p_file, int p_line, const String &str);

static void log_important(const String &str);
static void log_important(const char *str);
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 do_log_trace(const String &str);
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 void do_log_important(const String &str);

static LogLevel get_log_level();
static void set_log_level(const LogLevel p_log_level);
Expand Down
9 changes: 9 additions & 0 deletions core/log/logger_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ void LoggerBackend::log_warning(const String &str) {
void LoggerBackend::log_error(const String &str) {
call("_log_error", str);
}
void LoggerBackend::log_important(const String &str) {
call("_log_important", str);
}

void LoggerBackend::_log_trace(const String &str) {
print_line(str);
Expand All @@ -58,6 +61,9 @@ void LoggerBackend::_log_warning(const String &str) {
void LoggerBackend::_log_error(const String &str) {
print_error(str);
}
void LoggerBackend::_log_important(const String &str) {
print_error(str);
}

LoggerBackend::LoggerBackend() {
}
Expand All @@ -70,14 +76,17 @@ void LoggerBackend::_bind_methods() {
BIND_VMETHOD(MethodInfo("_log_message", PropertyInfo(Variant::STRING, "str")));
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")));

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_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);
}
2 changes: 2 additions & 0 deletions core/log/logger_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ class LoggerBackend : public Reference {
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_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);

LoggerBackend();
~LoggerBackend();
Expand Down

0 comments on commit 642db98

Please sign in to comment.