Logger API cleanup.

This commit is contained in:
Relintai 2022-06-11 16:55:09 +02:00
parent b52dd560ce
commit 7d60706249
2 changed files with 98 additions and 237 deletions

View File

@ -8,198 +8,100 @@
#include <stdio.h> #include <stdio.h>
#include <thread> #include <thread>
#include "core/print_string.h"
void RLogger::print_trace(const String &str) { void PLogger::log_trace(const String &str) {
print_trace(str.data()); String s;
s += "T ";
s += str;
s += "\n";
do_log_trace(s);
} }
void RLogger::print_trace(const char *str) { void PLogger::log_trace(const char *str) {
printf("T %s\n", str); String s;
s += "T ";
s += str;
s += "\n";
do_log_trace(s);
} }
void RLogger::print_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) {
String s;
s += "T | ";
s += p_file;
s += "::";
s += p_function;
s += ":";
s += p_line;
s += " | ";
s += str;
s += "\n";
do_log_trace(s);
printf("T | %s::%s:%d | %s\n", p_file, p_function, p_line, str); printf("T | %s::%s:%d | %s\n", p_file, p_function, p_line, str);
} }
void RLogger::print_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) {
printf("T | %s::%s:%d | %s\n", p_file, p_function, p_line, str.c_str());
} }
void RLogger::print_message(const String &str) { void PLogger::log_message(const String &str) {
print_message(str.data());
} }
void RLogger::print_message(const char *str) { void PLogger::log_message(const char *str) {
printf("M %s\n", str);
} }
void RLogger::print_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) {
printf("M | %s::%s:%d | %s\n", p_file, p_function, p_line, str);
} }
void RLogger::print_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) {
printf("M | %s::%s:%d | %s\n", p_file, p_function, p_line, str.c_str());
} }
void RLogger::print_warning(const String &str) { void PLogger::log_warning(const String &str) {
print_warning(str.data());
} }
void RLogger::print_warning(const char *str) { void PLogger::log_warning(const char *str) {
printf("W %s\n", str);
} }
void RLogger::print_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) {
printf("W | %s::%s:%d | %s\n", p_file, p_function, p_line, str);
} }
void RLogger::print_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) {
printf("W | %s::%s:%d | %s\n", p_file, p_function, p_line, str.c_str());
} }
void RLogger::print_error(const String &str) { void PLogger::log_error(const String &str) {
print_error(str.data());
} }
void RLogger::print_error(const char *str) { void PLogger::log_error(const char *str) {
printf("E %s\n", str); }
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 String &str) {
} }
void RLogger::print_error(const char *p_function, const char *p_file, int p_line, const char *str) { void PLogger::do_log_trace(const String &str) {
printf("E | %s::%s:%d | %s\n", p_file, p_function, p_line, str); if (_backend.is_valid()) {
} _backend->log_trace(str);
void RLogger::print_error(const char *p_function, const char *p_file, int p_line, const String &str) { } else {
printf("E | %s::%s:%d | %s\n", p_file, p_function, p_line, str.c_str()); print_line(str);
} }
void RLogger::print_msg_error(const char *p_function, const char *p_file, int p_line, const char *p_msg, const char *str) {
printf("E | %s::%s:%d | :: %s. %s\n", p_file, p_function, p_line, str, p_msg);
}
void RLogger::print_index_error(const char *p_function, const char *p_file, int p_line, const int index, const int size, const char *str) {
printf("E (INDEX) | %s::%s:%d | :: index: %d/%d. %s\n", p_file, p_function, p_line, index, size, str);
} }
void RLogger::log_trace(const String &str) { void PLogger::do_log_message(const String &str) {
log_trace(str.data()); if (_backend.is_valid()) {
} _backend->log_message(str);
void RLogger::log_trace(const char *str) { } else {
printf("T %s\n", str); print_line(str);
} }
void RLogger::log_trace(const char *p_function, const char *p_file, int p_line, const char *str) {
printf("T | %s::%s:%d | %s\n", p_file, p_function, p_line, str);
}
void RLogger::log_trace(const char *p_function, const char *p_file, int p_line, const String &str) {
printf("T | %s::%s:%d | %s\n", p_file, p_function, p_line, str.c_str());
} }
void RLogger::log_message(const String &str) { void PLogger::do_log_warning(const String &str) {
log_message(str.data()); if (_backend.is_valid()) {
} _backend->log_warning(str);
void RLogger::log_message(const char *str) { } else {
printf("M %s\n", str); print_line(str);
} }
void RLogger::log_message(const char *p_function, const char *p_file, int p_line, const char *str) {
printf("M | %s::%s:%d | %s\n", p_file, p_function, p_line, str);
}
void RLogger::log_message(const char *p_function, const char *p_file, int p_line, const String &str) {
printf("M | %s::%s:%d | %s\n", p_file, p_function, p_line, str.c_str());
} }
void RLogger::log_warning(const String &str) { void PLogger::do_log_error(const String &str) {
log_warning(str.data()); if (_backend.is_valid()) {
} _backend->log_error(str);
void RLogger::log_warning(const char *str) { } else {
printf("W %s\n", str); print_line(str);
} }
void RLogger::log_warning(const char *p_function, const char *p_file, int p_line, const char *str) {
printf("W | %s::%s:%d | %s\n", p_file, p_function, p_line, str);
}
void RLogger::log_warning(const char *p_function, const char *p_file, int p_line, const String &str) {
printf("W | %s::%s:%d | %s\n", p_file, p_function, p_line, str.c_str());
} }
void RLogger::log_error(const String &str) { Ref<LoggerBackend> PLogger::_backend;
log_error(str.data());
}
void RLogger::log_error(const char *str) {
printf("E %s\n", str);
}
void RLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *str) {
printf("E | %s::%s:%d | %s\n", p_file, p_function, p_line, str);
}
void RLogger::log_error(const char *p_function, const char *p_file, int p_line, const String &str) {
printf("E | %s::%s:%d | %s\n", p_file, p_function, p_line, str.c_str());
}
void RLogger::log_msg_error(const char *p_function, const char *p_file, int p_line, const char *p_msg, const char *str) {
printf("E | %s::%s:%d | :: %s. %s\n", p_file, p_function, p_line, str, p_msg);
}
void RLogger::log_index_error(const char *p_function, const char *p_file, int p_line, const int index, const int size, const char *str) {
printf("E (INDEX) | %s::%s:%d | :: index: %d/%d. %s\n", p_file, p_function, p_line, index, size, str);
}
String *RLogger::get_string_ptr(const int p_default_size) {
return new String(p_default_size);
}
String *RLogger::get_string_ptr(const char *p_function, const char *p_file, int p_line, const int p_default_size) {
String *s = new String(p_default_size);
s->append_str(p_function);
s->append_str("::");
s->append_str(p_file);
s->append_str(":");
s->append_str(String::num(p_line));
s->append_str(" | ");
return s;
}
String *RLogger::get_string_ptr(const char *p_prefix, const char *p_function, const char *p_file, int p_line, const int p_default_size) {
String *s = new String(p_default_size);
s->append_str(p_prefix);
s->append_str(" | ");
s->append_str(p_function);
s->append_str("::");
s->append_str(p_file);
s->append_str(":");
s->append_str(String::num(p_line));
s->append_str(" | ");
return s;
}
void RLogger::return_string_ptr(String *str) {
delete str;
}
String *RLogger::get_trace_string_ptr(const int p_default_size) {
String *str = get_string_ptr(p_default_size);
str->append_str("T ");
return str;
}
String *RLogger::get_message_string_ptr(const int p_default_size) {
String *str = get_string_ptr(p_default_size);
str->append_str("M ");
return str;
}
String *RLogger::get_warning_string_ptr(const int p_default_size) {
String *str = get_string_ptr(p_default_size);
str->append_str("W ");
return str;
}
String *RLogger::get_error_string_ptr(const int p_default_size) {
String *str = get_string_ptr(p_default_size);
str->append_str("E ");
return str;
}
String *RLogger::get_trace_string_ptr(const char *p_function, const char *p_file, int p_line, const int p_default_size) {
return get_string_ptr("T", p_function, p_file, p_line, p_default_size);
}
String *RLogger::get_message_string_ptr(const char *p_function, const char *p_file, int p_line, const int p_default_size) {
return get_string_ptr("M", p_function, p_file, p_line, p_default_size);
}
String *RLogger::get_warning_string_ptr(const char *p_function, const char *p_file, int p_line, const int p_default_size) {
return get_string_ptr("W", p_function, p_file, p_line, p_default_size);
}
String *RLogger::get_error_string_ptr(const char *p_function, const char *p_file, int p_line, const int p_default_size) {
return get_string_ptr("E", p_function, p_file, p_line, p_default_size);
}
void RLogger::log_ptr(String *str) {
printf("%s\n", str->data());
}
void RLogger::log_ret_ptr(String *str) {
log_ptr(str);
return_string_ptr(str);
}

View File

@ -1,40 +1,16 @@
#ifndef LOGGER_H #ifndef PLOGGER_H
#define LOGGER_H #define PLOGGER_H
#include "core/reference.h" #include "core/reference.h"
#include "core/typedefs.h"
#include "core/ustring.h" #include "core/ustring.h"
#include "logger_backend.h" #include "logger_backend.h"
#include <cstring>
#include <functional>
class String; class String;
class RLogger : public Object { class PLogger : public Object {
public: public:
static void print_trace(const String &str);
static void print_trace(const char *str);
static void print_trace(const char *p_function, const char *p_file, int p_line, const String &str);
static void print_trace(const char *p_function, const char *p_file, int p_line, const char *str);
static void print_message(const String &str);
static void print_message(const char *str);
static void print_message(const char *p_function, const char *p_file, int p_line, const String &str);
static void print_message(const char *p_function, const char *p_file, int p_line, const char *str);
static void print_warning(const String &str);
static void print_warning(const char *str);
static void print_warning(const char *p_function, const char *p_file, int p_line, const String &str);
static void print_warning(const char *p_function, const char *p_file, int p_line, const char *str);
static void print_error(const String &str);
static void print_error(const char *str);
static void print_error(const char *p_function, const char *p_file, int p_line, const char *str);
static void print_error(const char *p_function, const char *p_file, int p_line, const String &str);
static void print_msg_error(const char *p_function, const char *p_file, int p_line, const char *p_msg, const char *str);
static void print_index_error(const char *p_function, const char *p_file, int p_line, const int index, const int size, const char *str);
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);
@ -54,30 +30,13 @@ public:
static void log_error(const char *str); static void log_error(const char *str);
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 char *str);
static void log_error(const char *p_function, const char *p_file, int p_line, const String &str); static void log_error(const char *p_function, const char *p_file, int p_line, const String &str);
static void log_msg_error(const char *p_function, const char *p_file, int p_line, const char *p_msg, const char *str);
static void log_index_error(const char *p_function, const char *p_file, int p_line, const int index, const int size, const char *str);
static String *get_string_ptr(const int p_default_size = 100); static void do_log_trace(const String &str);
static String *get_string_ptr(const char *p_function, const char *p_file, int p_line, const int p_default_size = 300); static void do_log_message(const String &str);
static String *get_string_ptr(const char *p_prefix, const char *p_function, const char *p_file, int p_line, const int p_default_size = 300); static void do_log_warning(const String &str);
static void return_string_ptr(String *str); static void do_log_error(const String &str);
static String *get_trace_string_ptr(const int p_default_size = 100); static Ref<LoggerBackend> _backend;
static String *get_message_string_ptr(const int p_default_size = 100);
static String *get_warning_string_ptr(const int p_default_size = 100);
static String *get_error_string_ptr(const int p_default_size = 100);
static String *get_trace_string_ptr(const char *p_function, const char *p_file, int p_line, const int p_default_size = 300);
static String *get_message_string_ptr(const char *p_function, const char *p_file, int p_line, const int p_default_size = 300);
static String *get_warning_string_ptr(const char *p_function, const char *p_file, int p_line, const int p_default_size = 300);
static String *get_error_string_ptr(const char *p_function, const char *p_file, int p_line, const int p_default_size = 300);
static void log_ptr(String *str);
static void log_ret_ptr(String *str);
Ref<LoggerBackend> _backend;
//func impl -> uif backend() -> call, else printf().
}; };
// template methods for the variadic log macros. Add more as needed. // template methods for the variadic log macros. Add more as needed.
@ -126,44 +85,44 @@ _FORCE_INLINE_ void _RLOG_MACRO_TEMPLATE_FUNC(STR str, A p0, B p1, C p2, D p3, E
str->append(p4); str->append(p4);
} }
#define RPRINT_TRACE(str) \ #define PLOG_TRACE(str) \
RLogger::print_trace(__FUNCTION__, __FILE__, __LINE__, str); PLogger::print_trace(__FUNCTION__, __FILE__, __LINE__, str);
#define RLOG_TRACE(...) \ #define PALOG_TRACE(...) \
{ \ { \
String *_rlogger_string_ptr = RLogger::get_trace_string_ptr(__FUNCTION__, __FILE__, __LINE__); \ String *_rlogger_string_ptr = PLogger::get_trace_string_ptr(__FUNCTION__, __FILE__, __LINE__); \
_RLOG_MACRO_TEMPLATE_FUNC(_rlogger_string_ptr, __VA_ARGS__); \ _RLOG_MACRO_TEMPLATE_FUNC(_rlogger_string_ptr, __VA_ARGS__); \
RLogger::log_ret_ptr(_rlogger_string_ptr); \ PLogger::log_ret_ptr(_rlogger_string_ptr); \
} }
#define RPRINT_MSG(str) \ #define PLOG_MSG(str) \
RLogger::print_message(__FUNCTION__, __FILE__, __LINE__, str); PLogger::print_message(__FUNCTION__, __FILE__, __LINE__, str);
#define RLOG_MSG(...) \ #define PALOG_MSG(...) \
{ \ { \
String *_rlogger_string_ptr = RLogger::get_message_string_ptr(__FUNCTION__, __FILE__, __LINE__); \ String *_rlogger_string_ptr = PLogger::get_message_string_ptr(__FUNCTION__, __FILE__, __LINE__); \
_RLOG_MACRO_TEMPLATE_FUNC(_rlogger_string_ptr, __VA_ARGS__); \ _RLOG_MACRO_TEMPLATE_FUNC(_rlogger_string_ptr, __VA_ARGS__); \
RLogger::log_ret_ptr(_rlogger_string_ptr); \ PLogger::log_ret_ptr(_rlogger_string_ptr); \
} }
#define RPRINT_WARN(str) \ #define PLOG_WARN(str) \
RLogger::print_warning(__FUNCTION__, __FILE__, __LINE__, str); PLogger::print_warning(__FUNCTION__, __FILE__, __LINE__, str);
#define RLOG_WARN(...) \ #define PALOG_WARN(...) \
{ \ { \
String *_rlogger_string_ptr = RLogger::get_warning_string_ptr(__FUNCTION__, __FILE__, __LINE__); \ String *_rlogger_string_ptr = PLogger::get_warning_string_ptr(__FUNCTION__, __FILE__, __LINE__); \
_RLOG_MACRO_TEMPLATE_FUNC(_rlogger_string_ptr, __VA_ARGS__); \ _RLOG_MACRO_TEMPLATE_FUNC(_rlogger_string_ptr, __VA_ARGS__); \
RLogger::log_ret_ptr(_rlogger_string_ptr); \ PLogger::log_ret_ptr(_rlogger_string_ptr); \
} }
#define RPRINT_ERR(str) \ #define PLOG_ERR(str) \
RLogger::print_error(__FUNCTION__, __FILE__, __LINE__, str); PLogger::print_error(__FUNCTION__, __FILE__, __LINE__, str);
#define RLOG_ERR(...) \ #define PALOG_ERR(...) \
{ \ { \
String *_rlogger_string_ptr = RLogger::get_error_string_ptr(__FUNCTION__, __FILE__, __LINE__); \ String *_rlogger_string_ptr = PLogger::get_error_string_ptr(__FUNCTION__, __FILE__, __LINE__); \
_RLOG_MACRO_TEMPLATE_FUNC(_rlogger_string_ptr, __VA_ARGS__); \ _RLOG_MACRO_TEMPLATE_FUNC(_rlogger_string_ptr, __VA_ARGS__); \
RLogger::log_ret_ptr(_rlogger_string_ptr); \ PLogger::log_ret_ptr(_rlogger_string_ptr); \
} }
#endif #endif