Pixel-Composer/scripts/debug/debug.gml

196 lines
5.3 KiB
Text
Raw Normal View History

2023-02-14 02:51:14 +01:00
#region counter
globalvar DEBUG_COUNTER;
DEBUG_COUNTER = ds_map_create();
2024-12-15 12:19:48 +01:00
var _p = $"D:/Project/MakhamDev/LTS-PixelComposer/MISC/temp/";
if(directory_exists(_p)) directory_destroy(_p);
2023-02-14 02:51:14 +01:00
#endregion
2024-12-14 02:31:39 +01:00
#macro printlog if(log) show_debug_message
function print(str) {
var _s = "";
for(var i = 0; i < argument_count; i++)
_s += (i? ", " : "") + string(argument[i]);
noti_status(_s);
}
2024-12-15 12:19:48 +01:00
function printSurface(name, surface) {
if(!is_surface(surface)) {
noti_status($"{name}|Error: Not a valid surface {surface}");
return;
}
var _p = $"D:/Project/MakhamDev/LTS-PixelComposer/MISC/temp/{UUID_generate()}.png";
surface_save_safe(surface, _p);
noti_status($"{name}|texture|{_p}");
}
2024-12-14 02:31:39 +01:00
function printIf(cond, log) { if(cond) print(log); }
2024-08-20 10:15:53 +02:00
function __debug_counter(key) {
if(ds_map_exists(DEBUG_COUNTER, key)) DEBUG_COUNTER[? key]++;
else DEBUG_COUNTER[? key] = 1;
print($"{key}: {DEBUG_COUNTER[? key]}");
}
function _log_template() {
2023-11-08 08:38:04 +01:00
INLINE
2023-07-25 20:12:40 +02:00
return $"{current_year}/{current_month}/{current_day} {string_lead_zero(current_hour, 2)}:{string_lead_zero(current_minute, 2)}:{string_lead_zero(current_second, 2)} > ";
2024-08-20 10:15:53 +02:00
}
2024-08-20 10:15:53 +02:00
function __log(title, str, fname = "log/log.txt") {
var path = DIRECTORY + fname;
var f = file_text_open_append(path);
var t = _log_template();
file_text_write_string(f, $"{title}{t}{str}\n");
2022-01-13 05:24:03 +01:00
file_text_close(f);
2024-08-20 10:15:53 +02:00
}
2022-01-13 05:24:03 +01:00
2024-08-20 10:15:53 +02:00
function log_console(str, wait = false) {
2024-02-15 14:23:26 +01:00
INLINE
show_debug_message($"CLI: {str}");
if(wait) cli_wait();
return;
2024-08-20 10:15:53 +02:00
}
2024-02-15 14:23:26 +01:00
2024-08-20 10:15:53 +02:00
function cli_wait() { INLINE show_debug_message("WAIT"); return; }
2024-02-15 14:23:26 +01:00
2024-08-20 10:15:53 +02:00
function log_message(title, str, icon = noone, flash = false, write = true) {
2023-06-17 18:59:20 +02:00
if(TEST_ERROR) return;
2024-02-12 13:59:43 +01:00
if(IS_CMD) { show_debug_message($"{title}: {str}"); return; }
2024-02-12 10:25:23 +01:00
2024-02-12 13:59:43 +01:00
if(write) __log("[MESSAGE] ", $"{title}: {str}");
2022-11-14 03:16:15 +01:00
2024-02-12 13:59:43 +01:00
return noti_status($"{title}: {str}", icon, flash);
2024-08-20 10:15:53 +02:00
}
2022-01-13 05:24:03 +01:00
2024-08-20 10:15:53 +02:00
function log_warning(title, str, ref = noone) {
2023-06-17 18:59:20 +02:00
if(TEST_ERROR) return;
2024-02-12 13:59:43 +01:00
if(IS_CMD) { show_debug_message($"{title}: {str}"); return; }
2024-02-12 10:25:23 +01:00
2022-11-14 03:16:15 +01:00
__log("[WARNING] ", string(title) + ": " + string(str));
2023-01-25 06:49:00 +01:00
return noti_warning(string(title) + ": " + string(str),, ref);
2024-08-20 10:15:53 +02:00
}
2022-01-13 05:24:03 +01:00
2024-08-20 10:15:53 +02:00
function log_crash(str) {
2023-06-17 18:59:20 +02:00
if(TEST_ERROR) return;
2024-02-12 13:59:43 +01:00
if(IS_CMD) { show_debug_message($"{title}: {str}"); return; }
2024-02-12 10:25:23 +01:00
2022-01-13 05:24:03 +01:00
__log("[ERROR] ", string(str));
2022-11-14 03:16:15 +01:00
2022-12-22 03:09:55 +01:00
return noti_error(string(str));
2024-08-20 10:15:53 +02:00
}
2022-01-13 05:24:03 +01:00
2024-08-20 10:15:53 +02:00
function log_newline() {
2023-06-17 14:30:49 +02:00
var path = DIRECTORY + "log/log.txt";
2023-04-21 19:08:10 +02:00
var f = file_text_open_write(path);
2022-01-13 05:24:03 +01:00
file_text_writeln(f);
file_text_close(f);
2024-08-20 10:15:53 +02:00
}
2022-01-13 05:24:03 +01:00
2024-08-20 10:15:53 +02:00
function log_clear() {
2023-06-17 14:30:49 +02:00
var path = DIRECTORY + "log/log.txt";
2023-12-08 03:50:09 +01:00
if(file_exists_empty(path))
file_delete(path);
2024-08-20 10:15:53 +02:00
}
2022-12-22 03:09:55 +01:00
2024-05-22 12:13:46 +02:00
function os_type_sting() {
switch(os_type) {
case os_windows : return "Windows";
case os_macosx : return "MacOS";
case os_linux : return "Linux";
}
return "undefined";
}
2024-08-20 10:15:53 +02:00
function exception_print(e) {
2023-05-03 21:42:17 +02:00
if(!is_struct(e) || !struct_has(e, "longMessage")) return string(e);
2024-12-09 08:27:09 +01:00
var str = $"\n\n========== Crash log [PXC {VERSION_STRING}] [{os_type_sting()}] ==========";
str += $"\n\n{e.longMessage}";
2023-01-01 02:06:02 +01:00
str += "\n\n========== Stack trace ==========\n\n";
2022-12-22 03:09:55 +01:00
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(e.stacktrace); i < n; i++ )
2022-12-22 03:09:55 +01:00
str += e.stacktrace[i] + "\n"
2023-01-01 02:06:02 +01:00
str += "\n\n========= Crash log end =========\n";
2022-12-22 03:09:55 +01:00
return str;
2024-08-20 10:15:53 +02:00
}
2022-12-22 03:09:55 +01:00
2024-08-20 10:15:53 +02:00
function setException() {
2023-12-15 12:56:36 +01:00
if(OS == os_macosx) return noone;
2023-12-12 14:41:50 +01:00
2023-02-19 02:13:19 +01:00
exception_unhandled_handler(function(ex) {
2024-02-15 14:23:26 +01:00
var path = $"{DIRECTORY}prev_crash.pxc";
if(!SAVING && !TESTING && !IS_CMD) SAVE_AT(PROJECT, path);
var tt = "";
2024-05-22 12:13:46 +02:00
tt += $"\n-------------------------- Pixel Composer {VERSION_STRING} Crashed --------------------------\n";
2024-11-16 07:01:14 +01:00
tt += $"\n{ex.longMessage}";
tt += $"\n{ex.script}";
2024-05-22 12:13:46 +02:00
tt += "\n\n-------------------------- STACK TRACE --------------------------\n\n";
2024-02-15 14:23:26 +01:00
for( var i = 0, n = array_length(ex.stacktrace); i < n; i++ )
2024-11-16 07:01:14 +01:00
tt += $"{ex.stacktrace[i]}\n";
2024-05-22 12:13:46 +02:00
tt += "\n\n\n\n-------------------------- Device Info --------------------------\n";
tt += $"\nVersion: {VERSION_STRING} ({VERSION})";
tt += $"\nOperating system: {os_type_sting()} ({os_version})"
tt += "\n\n---------------------------- :( ----------------------------\n";
2023-06-17 14:30:49 +02:00
2023-12-15 12:56:36 +01:00
var path = $"{env_user()}crash_log.txt";
2023-02-19 02:13:19 +01:00
file_text_write_all(path, tt);
clipboard_set_text(tt);
2024-02-15 14:23:26 +01:00
if(IS_CMD) {
show_debug_message($"[ERROR BEGIN]\n{tt}\n[ERROR END]");
return 0;
} else
show_debug_message(tt);
2023-12-15 12:56:36 +01:00
var rep = $"{APP_LOCATION}report/PXC crash reporter.exe";
2023-12-15 14:08:50 +01:00
var pid = shell_execute(rep, DIRECTORY);
print($"{rep} [{file_exists(rep)}]: {pid}");
2023-12-13 11:09:06 +01:00
2023-02-19 02:13:19 +01:00
return 0;
});
2024-08-20 10:15:53 +02:00
}
2023-02-19 02:13:19 +01:00
2023-12-12 14:41:50 +01:00
function resetException() { exception_unhandled_handler(undefined); }
2023-08-01 11:16:23 +02:00
2024-08-20 10:15:53 +02:00
function printCallStack(maxDepth = 32) {
2023-08-01 11:16:23 +02:00
var stack = debug_get_callstack(maxDepth);
print($"Call Stack:");
for( var i = 2, n = array_length(stack) - 1; i < n; i++ ) {
var call = stack[i];
var sp = string_splice(call, ":");
if(array_length(sp) < 2) continue;
sp[0] = string_replace_all(sp[0], "anon_", "");
sp[0] = string_split(sp[0], "gml_", true);
for( var j = 0, m = array_length(sp[0]); j < m; j++ ) {
sp[0][j] = string_replace(sp[0][j], "GlobalScript_", "Global: ");
sp[0][j] = string_replace(sp[0][j], "Script_", "Script: ");
sp[0][j] = string_replace(sp[0][j], "Object_", "Object: ");
sp[0][j] = string_trim(sp[0][j], ["_"]);
var _txt = "";
repeat(j * 4) _txt += " ";
_txt += $" > {sp[0][j]}";
if(j == m - 1)
_txt += $" line: {sp[1]}";
print(_txt);
}
}
print("")
2024-08-20 10:15:53 +02:00
}