summaryrefslogtreecommitdiff
path: root/drivers/unix/os_unix.cpp
diff options
context:
space:
mode:
authorRuslan Mustakov <r.mustakov@gmail.com>2017-09-22 12:56:02 +0700
committerRuslan Mustakov <r.mustakov@gmail.com>2017-09-25 16:19:21 +0700
commit1a2311e3505765e37b736fe6bb46bb229e00701f (patch)
treefd592573dbe7d95b89649eaa677bef54336c648e /drivers/unix/os_unix.cpp
parent14b4ad931f54c73aff9021a1314943278295e602 (diff)
Extract logging logic
Previously logging logic was scattered over OS class implementations with plenty of duplication. Major changes in this commit: - Extracted logging logic into a separate Logger hierarchy. It allows easy configuration of logging mechanism depending on compile-time or run-time configuration. - Implemented RotatedFileLogger which is usually used with StdLogger, providing persistency of logs. It is often important to be able to obtain logs of the game even in production to be able to understand what happened prior to some problem. On mobile there previously was no way to obtain the logs aside from having the device connected to your machine. - flush() is not performed in release mode for every logged line. It is only performed for errors.
Diffstat (limited to 'drivers/unix/os_unix.cpp')
-rw-r--r--drivers/unix/os_unix.cpp93
1 files changed, 41 insertions, 52 deletions
diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp
index 75c8a153f6..29fe73f170 100644
--- a/drivers/unix/os_unix.cpp
+++ b/drivers/unix/os_unix.cpp
@@ -64,39 +64,7 @@
#include <string.h>
#include <sys/time.h>
#include <sys/wait.h>
-
-extern bool _print_error_enabled;
-
-void OS_Unix::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 (!_print_error_enabled)
- return;
-
- const char *err_details;
- if (p_rationale && p_rationale[0])
- err_details = p_rationale;
- else
- err_details = p_code;
-
- switch (p_type) {
- case ERR_ERROR:
- print("\E[1;31mERROR: %s: \E[0m\E[1m%s\n", p_function, err_details);
- print("\E[0;31m At: %s:%i.\E[0m\n", p_file, p_line);
- break;
- case ERR_WARNING:
- print("\E[1;33mWARNING: %s: \E[0m\E[1m%s\n", p_function, err_details);
- print("\E[0;33m At: %s:%i.\E[0m\n", p_file, p_line);
- break;
- case ERR_SCRIPT:
- print("\E[1;35mSCRIPT ERROR: %s: \E[0m\E[1m%s\n", p_function, err_details);
- print("\E[0;35m At: %s:%i.\E[0m\n", p_file, p_line);
- break;
- case ERR_SHADER:
- print("\E[1;36mSHADER ERROR: %s: \E[0m\E[1m%s\n", p_function, err_details);
- print("\E[0;36m At: %s:%i.\E[0m\n", p_file, p_line);
- break;
- }
-}
+#include <unistd.h>
void OS_Unix::debug_break() {
@@ -165,29 +133,16 @@ void OS_Unix::initialize_core() {
}
}
-void OS_Unix::finalize_core() {
+void OS_Unix::initialize_logger() {
+ Vector<Logger *> loggers;
+ loggers.push_back(memnew(UnixTerminalLogger));
+ loggers.push_back(memnew(RotatedFileLogger("user://logs/log.txt")));
+ _set_logger(memnew(CompositeLogger(loggers)));
}
-void OS_Unix::vprint(const char *p_format, va_list p_list, bool p_stder) {
-
- if (p_stder) {
-
- vfprintf(stderr, p_format, p_list);
- fflush(stderr);
- } else {
-
- vprintf(p_format, p_list);
- fflush(stdout);
- }
+void OS_Unix::finalize_core() {
}
-void OS_Unix::print(const char *p_format, ...) {
-
- va_list argp;
- va_start(argp, p_format);
- vprintf(p_format, argp);
- va_end(argp);
-}
void OS_Unix::alert(const String &p_alert, const String &p_title) {
fprintf(stderr, "ERROR: %s\n", p_alert.utf8().get_data());
@@ -559,4 +514,38 @@ 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)) {
+ return;
+ }
+
+ const char *err_details;
+ if (p_rationale && p_rationale[0])
+ err_details = p_rationale;
+ else
+ err_details = p_code;
+
+ switch (p_type) {
+ case ERR_WARNING:
+ logf_error("\E[1;33mWARNING: %s: \E[0m\E[1m%s\n", p_function, err_details);
+ logf_error("\E[0;33m At: %s:%i.\E[0m\n", p_file, p_line);
+ break;
+ case ERR_SCRIPT:
+ logf_error("\E[1;35mSCRIPT ERROR: %s: \E[0m\E[1m%s\n", p_function, err_details);
+ logf_error("\E[0;35m At: %s:%i.\E[0m\n", p_file, p_line);
+ break;
+ case ERR_SHADER:
+ logf_error("\E[1;36mSHADER ERROR: %s: \E[0m\E[1m%s\n", p_function, err_details);
+ logf_error("\E[0;36m At: %s:%i.\E[0m\n", p_file, p_line);
+ break;
+ case ERR_ERROR:
+ default:
+ logf_error("\E[1;31mERROR: %s: \E[0m\E[1m%s\n", p_function, err_details);
+ logf_error("\E[0;31m At: %s:%i.\E[0m\n", p_file, p_line);
+ break;
+ }
+}
+
+UnixTerminalLogger::~UnixTerminalLogger() {}
+
#endif