-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathygamelog.cpp
More file actions
148 lines (126 loc) · 4.9 KB
/
ygamelog.cpp
File metadata and controls
148 lines (126 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// Created by Daniel on 2024-03-19.
//
#include "ygamelog.h"
YGameLog* YGameLog::singleton = nullptr;
void YGameLog::_bind_methods() {
ClassDB::bind_method(D_METHOD("enable_output_logging"), &YGameLog::SetupOutputLogging);
ADD_SIGNAL(MethodInfo("system_log_printed", PropertyInfo(Variant::STRING, "printed")));
ADD_SIGNAL(MethodInfo("system_error_printed", PropertyInfo(Variant::STRING, "printed")));
ADD_SIGNAL(MethodInfo("system_warning_printed", PropertyInfo(Variant::STRING, "printed")));
ClassDB::bind_method(D_METHOD("get_log_count"), &YGameLog::get_log_count);
ClassDB::bind_method(D_METHOD("get_logged"), &YGameLog::get_logged);
ClassDB::bind_method(D_METHOD("set_logged", "logged"), &YGameLog::set_logged);
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "logged"), "set_logged", "get_logged");
ClassDB::bind_method(D_METHOD("log_message", "new_message"), &YGameLog::log_message);
ClassDB::bind_method(D_METHOD("clear_log"), &YGameLog::clear_log);
ADD_SIGNAL(MethodInfo("on_message_logged", PropertyInfo(Variant::STRING, "message")));
ClassDB::bind_method(D_METHOD("get_variant_log_count"), &YGameLog::get_variant_log_count);
ClassDB::bind_method(D_METHOD("get_logged_variants"), &YGameLog::get_logged_variants);
ClassDB::bind_method(D_METHOD("set_logged_variants", "logged_variants"), &YGameLog::set_logged_variants);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "logged_variants"), "set_logged_variants", "get_logged_variants");
ClassDB::bind_method(D_METHOD("log_variant", "new_message"), &YGameLog::log_variant);
ClassDB::bind_method(D_METHOD("clear_variant_log"), &YGameLog::clear_variant_log);
}
YGameLog *YGameLog::get_singleton() {
return singleton;
}
void YGameLog::SetupOutputLogging() {
if (!output_logging_on) {
#ifdef YGODOT
output_logging_on=true;
OS::get_singleton()->add_extra_logger(memnew(YLogger));
#endif
}
}
void YLogger::logv(const char *p_format, va_list p_list, bool p_err) {
if (YEngine::is_exiting || !should_log(p_err)) {
return;
}
const int static_buf_size = 512;
char static_buf[static_buf_size];
char *buf = static_buf;
va_list list_copy;
va_copy(list_copy, p_list);
int len = vsnprintf(buf, static_buf_size, p_format, p_list);
if (len >= static_buf_size) {
buf = (char *)memalloc(len + 1);
vsnprintf(buf, len + 1, p_format, list_copy);
}
va_end(list_copy);
if(last_pushed_was_warning) {
YGameLog::get_singleton()->emit_signal("system_warning_printed",String::utf8(buf));
}else if(last_pushed_was_error) {
YGameLog::get_singleton()->emit_signal("system_error_printed",String::utf8(buf));
}else {
YGameLog::get_singleton()->emit_signal("system_log_printed",String::utf8(buf));
}
if (len >= static_buf_size) {
memfree(buf);
}
last_pushed_was_warning=false;
last_pushed_was_error=false;
}
void YLogger::logf_error_custom(const char *p_format, ...) {
if (YEngine::is_exiting)
return;
va_list argp;
va_start(argp, p_format);
logv(p_format, argp, false);
last_pushed_was_error=false;
last_pushed_was_warning=false;
va_end(argp);
}
void YLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code,
const char *p_rationale, bool p_editor_notify, ErrorType p_type) {
// if (!should_log(true)) {
// return;
// }
if (YEngine::is_exiting)
return;
const char *err_type = "ERROR";
bool do_push_error=false;
switch (p_type) {
case ERR_ERROR:
err_type = "ERROR";
do_push_error=true;
last_pushed_was_error=true;
last_pushed_was_warning=false;
break;
case ERR_WARNING:
err_type = "WARNING";
do_push_error=true;
last_pushed_was_warning=true;
last_pushed_was_error=false;
break;
case ERR_SCRIPT:
err_type = "SCRIPT ERROR";
last_pushed_was_error=true;
last_pushed_was_warning=false;
do_push_error=true;
break;
case ERR_SHADER:
err_type = "SHADER ERROR";
break;
default:
err_type = "ERROR";
do_push_error=true;
last_pushed_was_error=true;
last_pushed_was_warning=false;
break;
}
const char *err_details;
if (p_rationale && *p_rationale) {
err_details = p_rationale;
} else {
err_details = p_code;
}
if(do_push_error){
bool temp_was_error = last_pushed_was_error;
bool temp_was_warning = last_pushed_was_warning;
logf_error_custom("%s: %s\n", err_type, err_details);
last_pushed_was_warning = temp_was_warning;
last_pushed_was_error = temp_was_error;
logf_error_custom(" at: %s (%s:%i)\n", p_function, p_file, p_line);
}
}