Added force printing / logging option to OS. Made PLogger use this internally.

This commit is contained in:
Relintai 2024-11-16 10:28:17 +01:00
parent 1a45e6371a
commit e53f2b27ba
15 changed files with 105 additions and 51 deletions

View File

@ -62,8 +62,8 @@ void Logger::set_flush_stdout_on_print(bool value) {
_flush_stdout_on_print = value;
}
void Logger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
if (!should_log(true)) {
void Logger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type, bool p_force) {
if (!p_force && !should_log(true)) {
return;
}
@ -98,27 +98,19 @@ void Logger::log_error(const char *p_function, const char *p_file, int p_line, c
}
void Logger::logf(const char *p_format, ...) {
if (!should_log(false)) {
return;
}
va_list argp;
va_start(argp, p_format);
logv(p_format, argp, false);
logv(p_format, argp, false, false);
va_end(argp);
}
void Logger::logf_error(const char *p_format, ...) {
if (!should_log(true)) {
return;
}
va_list argp;
va_start(argp, p_format);
logv(p_format, argp, true);
logv(p_format, argp, true, false);
va_end(argp);
}
@ -202,8 +194,8 @@ RotatedFileLogger::RotatedFileLogger(const String &p_base_path, int p_max_files)
rotate_file();
}
void RotatedFileLogger::logv(const char *p_format, va_list p_list, bool p_err) {
if (!should_log(p_err)) {
void RotatedFileLogger::logv(const char *p_format, va_list p_list, bool p_err, bool p_force) {
if (!p_force && !should_log(p_err)) {
return;
}
@ -237,8 +229,8 @@ RotatedFileLogger::~RotatedFileLogger() {
close_file();
}
void StdLogger::logv(const char *p_format, va_list p_list, bool p_err) {
if (!should_log(p_err)) {
void StdLogger::logv(const char *p_format, va_list p_list, bool p_err, bool p_force) {
if (!p_force && !should_log(p_err)) {
return;
}
@ -260,26 +252,26 @@ CompositeLogger::CompositeLogger(Vector<Logger *> p_loggers) :
loggers(p_loggers) {
}
void CompositeLogger::logv(const char *p_format, va_list p_list, bool p_err) {
if (!should_log(p_err)) {
void CompositeLogger::logv(const char *p_format, va_list p_list, bool p_err, bool p_force) {
if (!p_force && !should_log(p_err)) {
return;
}
for (int i = 0; i < loggers.size(); ++i) {
va_list list_copy;
va_copy(list_copy, p_list);
loggers[i]->logv(p_format, list_copy, p_err);
loggers[i]->logv(p_format, list_copy, p_err, p_force);
va_end(list_copy);
}
}
void CompositeLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
if (!should_log(true)) {
void CompositeLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type, bool p_force) {
if (!p_force && !should_log(true)) {
return;
}
for (int i = 0; i < loggers.size(); ++i) {
loggers[i]->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
loggers[i]->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type, p_force);
}
}

View File

@ -54,8 +54,8 @@ public:
static void set_flush_stdout_on_print(bool value);
virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0 = 0;
virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR);
virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) _PRINTF_FORMAT_ATTRIBUTE_2_0 = 0;
virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR, bool p_force = false);
void logf(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
void logf_error(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
@ -68,7 +68,7 @@ public:
*/
class StdLogger : public Logger {
public:
virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0;
virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) _PRINTF_FORMAT_ATTRIBUTE_2_0;
virtual ~StdLogger();
};
@ -91,7 +91,7 @@ class RotatedFileLogger : public Logger {
public:
RotatedFileLogger(const String &p_base_path, int p_max_files = 10);
virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0;
virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) _PRINTF_FORMAT_ATTRIBUTE_2_0;
virtual ~RotatedFileLogger();
};
@ -102,8 +102,8 @@ class CompositeLogger : public Logger {
public:
CompositeLogger(Vector<Logger *> p_loggers);
virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0;
virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR);
virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) _PRINTF_FORMAT_ATTRIBUTE_2_0;
virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR, bool p_force = false);
void add_logger(Logger *p_logger);

View File

@ -282,7 +282,7 @@ void PLogger::do_log_trace(const String &str) {
if (_backend.is_valid()) {
_backend->log_trace(str);
} else {
print_line(str);
force_print_line(str);
}
}
@ -290,7 +290,7 @@ void PLogger::do_log_message(const String &str) {
if (_backend.is_valid()) {
_backend->log_message(str);
} else {
print_line(str);
force_print_line(str);
}
}
@ -298,7 +298,7 @@ void PLogger::do_log_warning(const String &str) {
if (_backend.is_valid()) {
_backend->log_warning(str);
} else {
print_line(str);
force_print_line(str);
}
}
@ -306,8 +306,7 @@ void PLogger::do_log_error(const String &str) {
if (_backend.is_valid()) {
_backend->log_error(str);
} else {
ERR_PRINT(str);
//print_error(str);
force_print_error(str);
}
}

View File

@ -246,6 +246,28 @@ void OS::printerr(const char *p_format, ...) {
va_end(argp);
};
void OS::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, Logger::ErrorType p_type) {
_logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type, true);
}
void OS::log(const char *p_format, ...) {
va_list argp;
va_start(argp, p_format);
_logger->logv(p_format, argp, false, true);
va_end(argp);
};
void OS::logerr(const char *p_format, ...) {
va_list argp;
va_start(argp, p_format);
_logger->logv(p_format, argp, true, true);
va_end(argp);
};
void OS::set_keep_screen_on(bool p_enabled) {
_keep_screen_on = p_enabled;
}

View File

@ -186,10 +186,16 @@ public:
virtual void global_menu_remove_item(const String &p_menu, int p_idx) {};
virtual void global_menu_clear(const String &p_menu) {};
// Depens on settings if logged / printed
void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, Logger::ErrorType p_type = Logger::ERR_ERROR);
void print(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
void printerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
// Will always get logged / printed
void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, Logger::ErrorType p_type = Logger::ERR_ERROR);
void log(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
void logerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
virtual void alert(const String &p_alert, const String &p_title = "ALERT!") = 0;
virtual String get_stdin_string() = 0;

View File

@ -78,6 +78,7 @@ void print_line(String p_string) {
OS::get_singleton()->print("%s\n", p_string.utf8().get_data());
_global_lock();
PrintHandlerList *l = print_handler_list;
while (l) {
l->printfunc(l->userdata, p_string, false);
@ -95,6 +96,7 @@ void print_error(String p_string) {
OS::get_singleton()->printerr("%s\n", p_string.utf8().get_data());
_global_lock();
PrintHandlerList *l = print_handler_list;
while (l) {
l->printfunc(l->userdata, p_string, true);
@ -109,3 +111,32 @@ void print_verbose(String p_string) {
print_line(p_string);
}
}
void force_print_line(String p_string) {
OS::get_singleton()->log("%s\n", p_string.utf8().get_data());
_global_lock();
PrintHandlerList *l = print_handler_list;
while (l) {
l->printfunc(l->userdata, p_string, false);
l = l->next;
}
_global_unlock();
}
void force_print_error(String p_string) {
OS::get_singleton()->logerr("%s\n", p_string.utf8().get_data());
_global_lock();
PrintHandlerList *l = print_handler_list;
while (l) {
l->printfunc(l->userdata, p_string, true);
l = l->next;
}
_global_unlock();
}

View File

@ -56,8 +56,12 @@ void remove_print_handler(PrintHandlerList *p_handler);
extern bool _print_line_enabled;
extern bool _print_error_enabled;
extern void print_line(String p_string);
extern void print_error(String p_string);
extern void print_verbose(String p_string);
extern void force_print_line(String p_string);
extern void force_print_error(String p_string);
#endif

View File

@ -521,8 +521,8 @@ String OS_Unix::get_executable_path() const {
#endif
}
void UnixTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
if (!should_log(true)) {
void UnixTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type, bool p_force) {
if (!p_force && !should_log(true)) {
return;
}

View File

@ -103,7 +103,7 @@ public:
class UnixTerminalLogger : public StdLogger {
public:
virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR);
virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR, bool p_force = false);
virtual ~UnixTerminalLogger();
};

View File

@ -35,16 +35,16 @@
#include "core/string/print_string.h"
#include <syslog.h>
void SyslogLogger::logv(const char *p_format, va_list p_list, bool p_err) {
if (!should_log(p_err)) {
void SyslogLogger::logv(const char *p_format, va_list p_list, bool p_err, bool p_force) {
if (!p_force && !should_log(p_err)) {
return;
}
vsyslog(p_err ? LOG_ERR : LOG_INFO, p_format, p_list);
}
void SyslogLogger::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
if (!should_log(true)) {
void SyslogLogger::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type, bool p_force) {
if (!p_force && !should_log(true)) {
return;
}

View File

@ -38,8 +38,8 @@
class SyslogLogger : public Logger {
public:
virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0;
virtual void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type);
virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) _PRINTF_FORMAT_ATTRIBUTE_2_0;
virtual void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR, bool p_force = false);
virtual ~SyslogLogger();
};

View File

@ -80,7 +80,7 @@ String _remove_symlink(const String &dir) {
class AndroidLogger : public Logger {
public:
virtual void logv(const char *p_format, va_list p_list, bool p_err) {
virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false) {
__android_log_vprint(p_err ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "pandemonium", p_format, p_list);
}

View File

@ -1942,8 +1942,8 @@ String OS_OSX::get_name() const {
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
class OSXTerminalLogger : public StdLogger {
public:
virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR) {
if (!should_log(true)) {
virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR, bool p_force) {
if (!p_force && !should_log(true)) {
return;
}

View File

@ -37,8 +37,8 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err) {
if (!should_log(p_err)) {
void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err, bool p_force) {
if (!p_force && !should_log(p_err)) {
return;
}
@ -73,8 +73,8 @@ void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_er
#endif
}
void WindowsTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
if (!should_log(true)) {
void WindowsTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type, bool p_force) {
if (!p_force && !should_log(true)) {
return;
}

View File

@ -38,8 +38,8 @@
class WindowsTerminalLogger : public StdLogger {
public:
virtual void logv(const char *p_format, va_list p_list, bool p_err);
virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR);
virtual void logv(const char *p_format, va_list p_list, bool p_err, bool p_force = false);
virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR, bool p_force = false);
virtual ~WindowsTerminalLogger();
};