Skip to content

Commit

Permalink
Added log_custom method to PLogger. It's a lot more customizable than…
Browse files Browse the repository at this point in the history
… the others by design. Also small cleanups.
  • Loading branch information
Relintai committed Nov 17, 2024
1 parent f4917d1 commit 618e223
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 25 deletions.
5 changes: 5 additions & 0 deletions core/bind/logger_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions core/bind/logger_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class _PLogger : public Object {
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);
Expand Down
145 changes: 125 additions & 20 deletions core/log/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void PLogger::log_trace(const String &str) {
String s;
s += "T ";
s += str;
//s += "\n";

do_log_trace(s);
}
Expand All @@ -54,7 +53,6 @@ void PLogger::log_trace(const char *str) {
String s;
s += "T ";
s += str;
//s += "\n";

do_log_trace(s);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -103,7 +99,6 @@ void PLogger::log_message(const String &str) {
String s;
s += "M ";
s += str;
//s += "\n";

do_log_message(s);
}
Expand All @@ -115,7 +110,6 @@ void PLogger::log_message(const char *str) {
String s;
s += "M ";
s += str;
//s += "\n";

do_log_message(s);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -164,7 +156,6 @@ void PLogger::log_warning(const String &str) {
String s;
s += "W ";
s += str;
//s += "\n";

do_log_warning(s);
}
Expand All @@ -176,7 +167,6 @@ void PLogger::log_warning(const char *str) {
String s;
s += "W ";
s += str;
//s += "\n";

do_log_warning(s);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -225,7 +213,6 @@ void PLogger::log_error(const String &str) {
String s;
s += "E ";
s += str;
//s += "\n";

do_log_error(s);
}
Expand All @@ -237,7 +224,6 @@ void PLogger::log_error(const char *str) {
String s;
s += "E ";
s += str;
//s += "\n";

do_log_error(s);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -286,7 +270,6 @@ void PLogger::log_important(const String &str) {
String s;
s += "I ";
s += str;
//s += "\n";

do_log_important(s);
}
Expand All @@ -298,7 +281,6 @@ void PLogger::log_important(const char *str) {
String s;
s += "I ";
s += str;
//s += "\n";

do_log_important(s);
}
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions core/log/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -90,6 +93,11 @@ class PLogger : public Object {
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);
Expand Down
Loading

0 comments on commit 618e223

Please sign in to comment.