2024-09-03 18:33:35 +02:00
|
|
|
ISOTerminal.prototype.redirectConsole = function(handler){
|
|
|
|
const log = console.log;
|
|
|
|
const dir = console.dir;
|
|
|
|
const err = console.error;
|
|
|
|
const warn = console.warn;
|
2025-01-15 22:36:54 +01:00
|
|
|
const addLineFeeds = (str) => str.replace(/\n/g,"\n\r")
|
|
|
|
|
2024-09-03 18:33:35 +02:00
|
|
|
console.log = (...args)=>{
|
|
|
|
const textArg = args[0];
|
2025-01-15 22:36:54 +01:00
|
|
|
handler( addLineFeeds(textArg) );
|
2024-09-03 18:33:35 +02:00
|
|
|
log.apply(log, args);
|
|
|
|
};
|
|
|
|
console.error = (...args)=>{
|
2024-10-15 12:32:16 +02:00
|
|
|
const textArg = args[0]
|
2025-01-15 22:36:54 +01:00
|
|
|
handler( addLineFeeds(textArg), '\x1b[31merror\x1b[0m');
|
2024-09-03 18:33:35 +02:00
|
|
|
err.apply(log, args);
|
|
|
|
};
|
|
|
|
console.dir = (...args)=>{
|
2024-10-15 12:32:16 +02:00
|
|
|
const textArg = args[0]
|
2025-01-15 22:36:54 +01:00
|
|
|
let str = JSON.stringify(textArg,null,2)+'\n'
|
|
|
|
handler( addLineFeeds(str) )
|
2024-09-03 18:33:35 +02:00
|
|
|
dir.apply(log, args);
|
|
|
|
};
|
|
|
|
console.warn = (...args)=>{
|
2024-10-15 12:32:16 +02:00
|
|
|
const textArg = args[0]
|
2025-01-15 22:36:54 +01:00
|
|
|
handler( addLineFeeds(textArg),'\x1b[38;5;208mwarn\x1b[0m');
|
2024-09-03 18:33:35 +02:00
|
|
|
err.apply(log, args);
|
|
|
|
};
|
2024-09-06 18:02:54 +02:00
|
|
|
|
2024-09-03 18:33:35 +02:00
|
|
|
}
|
|
|
|
|
2025-01-15 22:36:54 +01:00
|
|
|
ISOTerminal.prototype.enableConsole = function(opts){
|
|
|
|
|
|
|
|
opts = opts || {stdout:false}
|
2024-09-03 18:33:35 +02:00
|
|
|
|
|
|
|
this.redirectConsole( (str,prefix) => {
|
2025-01-15 23:03:57 +01:00
|
|
|
let finalStr = "";
|
2025-01-15 22:36:54 +01:00
|
|
|
prefix = prefix ? prefix+' ' : ''
|
2024-09-16 13:28:28 +02:00
|
|
|
str.trim().split("\n").map( (line) => {
|
2025-01-15 22:36:54 +01:00
|
|
|
finalStr += `${opts.stdout ? '' : "\x1b[38;5;165m/dev/browser: \x1b[0m"}`+prefix+line+'\n'
|
2024-09-16 13:28:28 +02:00
|
|
|
})
|
2025-01-15 22:36:54 +01:00
|
|
|
if( opts.stdout ){
|
|
|
|
this.emit('serial-output-string', finalStr)
|
|
|
|
}else this.emit('append_file', ["/dev/browser/console",finalStr])
|
2024-09-03 18:33:35 +02:00
|
|
|
})
|
|
|
|
|
2024-09-06 18:02:54 +02:00
|
|
|
window.addEventListener('error', function(event) {
|
2024-09-25 15:58:40 +02:00
|
|
|
if( event.filename ){
|
|
|
|
console.error(event.filename+":"+event.lineno+":"+event.colno)
|
|
|
|
console.error(event.message);
|
|
|
|
console.error(event.error);
|
|
|
|
}else console.error(event)
|
2024-09-06 18:02:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener('unhandledrejection', function(event) {
|
2024-09-25 15:58:40 +02:00
|
|
|
console.error(event);
|
2024-09-06 18:02:54 +02:00
|
|
|
});
|
|
|
|
|
2025-01-15 22:36:54 +01:00
|
|
|
if( opts.stdout ){
|
|
|
|
this.emit('serial-output-string', "\n\n\r☑ initialized javascript console\n");
|
|
|
|
this.emit('serial-output-string', "\r☑ please use these functions to print:\n");
|
|
|
|
this.emit('serial-output-string', "\r└☑ console.log(\"foo\")\n");
|
|
|
|
this.emit('serial-output-string', "\r└☑ console.warn(\"foo\")\n");
|
|
|
|
this.emit('serial-output-string', "\r└☑ console.dir({foo:12})\n");
|
|
|
|
this.emit('serial-output-string', "\r└☑ console.error(\"foo\")\n");
|
|
|
|
this.emit('serial-output-string', "\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ISOTerminal.addEventListener('emulator-started', function(){
|
|
|
|
this.enableConsole()
|
|
|
|
})
|
|
|
|
|
|
|
|
ISOTerminal.addEventListener('init', function(){
|
|
|
|
this.addEventListener('enable-console', function(opts){
|
|
|
|
this.enableConsole(opts.detail)
|
|
|
|
})
|
2024-09-03 18:33:35 +02:00
|
|
|
})
|