dev: recursive log toggle for puter.js

This commit is contained in:
KernelDeimos 2024-11-09 15:14:37 -05:00
parent 6f5f11da09
commit 59984bb930
5 changed files with 85 additions and 0 deletions

View File

@ -48,6 +48,7 @@ import item_icon from './helpers/item_icon.js';
import { AntiCSRFService } from './services/AntiCSRFService.js';
import { IPCService } from './services/IPCService.js';
import { ExecService } from './services/ExecService.js';
import { DebugService } from './services/DebugService.js';
const launch_services = async function (options) {
// === Services Data Structures ===
@ -84,6 +85,7 @@ const launch_services = async function (options) {
// === Builtin Services ===
register('ipc', new IPCService());
register('exec', new ExecService());
register('debug', new DebugService());
register('broadcast', new BroadcastService());
register('theme', new ThemeService());
register('process', new ProcessService());

View File

@ -0,0 +1,28 @@
import { Service } from "../definitions.js";
export class DebugService extends Service {
async _init () {
// Track enabled log categories
this.enabled_logs = [];
// Provide enabled logs as a query param
const svc_exec = this.services.get('exec');
svc_exec.register_param_provider(() => {
return {
enabled_logs: this.enabled_logs.join(';'),
};
});
}
logs(category) {
const msg = {
$: 'puterjs-debug',
cmd: 'log.on',
category,
};
this.enabled_logs.push(category);
puter.log.on(category);
$('iframe').each(function () {
this.contentWindow.postMessage(msg);
});
}
}

View File

@ -6,6 +6,14 @@ export class ExecService extends Service {
Manages instances of apps on the Puter desktop.
`
_construct () {
this.param_providers = [];
}
register_param_provider (param_provider) {
this.param_providers.push(param_provider);
}
async _init ({ services }) {
const svc_ipc = services.get('ipc');
svc_ipc.register_ipc_handler('launchApp', {
@ -36,6 +44,11 @@ export class ExecService extends Service {
this.log.info('launchApp connection', connection);
const params = {};
for ( const provider of this.param_providers ) {
Object.assign(params, provider());
}
// The "body" of this method is in a separate file
const child_process = await launch_app({
launched_by_exec_service: true,
@ -44,6 +57,7 @@ export class ExecService extends Service {
args: args ?? {},
parent_instance_id: app?.appInstanceID,
uuid: child_instance_id,
params,
...(connection ? {
parent_pseudo_id: connection.backward.uuid,
} : {}),

View File

@ -18,6 +18,7 @@ import { FilesystemService } from './services/Filesystem.js';
import { APIAccessService } from './services/APIAccess.js';
import { XDIncomingService } from './services/XDIncoming.js';
import { NoPuterYetService } from './services/NoPuterYet.js';
import { Debug } from './modules/Debug.js';
window.puter = (function() {
'use strict';
@ -87,6 +88,7 @@ window.puter = (function() {
this.registerModule('ai', AI);
this.registerModule('kv', KV);
this.registerModule('drivers', Drivers);
this.registerModule('debug', Debug);
// Path
this.path = path;

View File

@ -0,0 +1,39 @@
export class Debug {
constructor (context, parameters) {
this.context = context;
this.parameters = parameters;
this._init();
}
_init () {
// Check query parameter 'enabled_logs'
const url = new URL(location.href);
let enabled_logs = url.searchParams.get('enabled_logs');
if ( ! enabled_logs ) enabled_logs = '';
enabled_logs = enabled_logs.split(';');
for ( const category of enabled_logs ) {
if ( category === '' ) continue;
this.context.puter.log.on(category);
}
window.addEventListener('message', async e => {
// Ensure message is from parent window
if ( e.source !== window.parent ) return;
// (parent window is allowed to be anything)
// Check if it's a debug message
if ( ! e.data.$ ) return;
if ( e.data.$ !== 'puterjs-debug' ) return;
// It's okay to log this; it will only show if a
// developer does something in the console.
console.log('Got a puter.js debug event!', e.data);
if ( e.data.cmd === 'log.on' ) {
console.log('Got instruction to turn logs on!');
this.context.puter.log.on(e.data.category);
}
})
}
}