mirror of
https://github.com/HeyPuter/puter.git
synced 2025-01-24 06:50:22 +08:00
dev: recursive log toggle for puter.js
This commit is contained in:
parent
6f5f11da09
commit
59984bb930
@ -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());
|
||||
|
28
src/gui/src/services/DebugService.js
Normal file
28
src/gui/src/services/DebugService.js
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
@ -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,
|
||||
} : {}),
|
||||
|
@ -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;
|
||||
|
39
src/puter-js/src/modules/Debug.js
Normal file
39
src/puter-js/src/modules/Debug.js
Normal 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);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user