From 59984bb930bd3cf41b5f582a2383747e1ce86885 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Sat, 9 Nov 2024 15:14:37 -0500 Subject: [PATCH] dev: recursive log toggle for puter.js --- src/gui/src/initgui.js | 2 ++ src/gui/src/services/DebugService.js | 28 ++++++++++++++++++++ src/gui/src/services/ExecService.js | 14 ++++++++++ src/puter-js/src/index.js | 2 ++ src/puter-js/src/modules/Debug.js | 39 ++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 src/gui/src/services/DebugService.js create mode 100644 src/puter-js/src/modules/Debug.js diff --git a/src/gui/src/initgui.js b/src/gui/src/initgui.js index ecf760a0..b05681c4 100644 --- a/src/gui/src/initgui.js +++ b/src/gui/src/initgui.js @@ -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()); diff --git a/src/gui/src/services/DebugService.js b/src/gui/src/services/DebugService.js new file mode 100644 index 00000000..a0de5ae5 --- /dev/null +++ b/src/gui/src/services/DebugService.js @@ -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); + }); + } +} diff --git a/src/gui/src/services/ExecService.js b/src/gui/src/services/ExecService.js index eacad3cd..22982cc7 100644 --- a/src/gui/src/services/ExecService.js +++ b/src/gui/src/services/ExecService.js @@ -5,6 +5,14 @@ export class ExecService extends Service { static description = ` 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'); @@ -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, } : {}), diff --git a/src/puter-js/src/index.js b/src/puter-js/src/index.js index eae7e576..bb44baf6 100644 --- a/src/puter-js/src/index.js +++ b/src/puter-js/src/index.js @@ -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; diff --git a/src/puter-js/src/modules/Debug.js b/src/puter-js/src/modules/Debug.js new file mode 100644 index 00000000..94cb928e --- /dev/null +++ b/src/puter-js/src/modules/Debug.js @@ -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); + } + }) + } +}