Skip to content

Commit

Permalink
Display debug.log for request
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrgicak committed Feb 16, 2024
1 parent 315a622 commit c23dd92
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
12 changes: 12 additions & 0 deletions packages/php-wasm/universal/src/lib/base-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
return this.requestHandler.request(request, maxRedirects);
}

getPhpErrorLog() {
const logPath = '/wordpress/wp-content/debug.log';
if (!this.fileExists(logPath)) {
return '';
}
return this.readFileAsText(logPath);
}

/** @inheritDoc */
async run(request: PHPRunOptions): Promise<PHPResponse> {
/*
Expand All @@ -243,6 +251,7 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
* be dispatched before the first one is finished.
*/
const release = await this.semaphore.acquire();
const logSize = this.getPhpErrorLog().length;
let heapBodyPointer;
try {
if (!this.#webSapiInitialized) {
Expand Down Expand Up @@ -288,6 +297,9 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
release();
this.dispatchEvent({
type: 'request.end',
data: {
log: this.getPhpErrorLog().substring(logSize),
},
});
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/php-wasm/universal/src/lib/universal-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PHPResponse } from './php-response';
*/
export interface PHPRequestEndEvent {
type: 'request.end';
data?: {[log: string]: string};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@
ini_set('error_log', $log_file);
ini_set('ignore_repeated_errors', true);
ini_set('display_errors', false);
ini_set('log_errors', true);

error_log('playground_logger.php loaded');

throw new Exception('test');
ini_set('log_errors', true);
9 changes: 9 additions & 0 deletions packages/playground/remote/src/lib/worker-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ try {
});
}

// Enable the WordPress debug mode
await defineWpConfigConsts(php, {
consts: {
WP_DEBUG: true,
WP_DEBUG_LOG: true,
WP_DEBUG_DISPLAY: false,
},
});

// Always install the playground mu-plugin, even if WordPress is loaded
// from the OPFS. This ensures:
// * The mu-plugin is always there, even when a custom WordPress directory
Expand Down
15 changes: 12 additions & 3 deletions packages/playground/website/src/lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { useEffect, useRef, useState } from 'react';
import { Blueprint, startPlaygroundWeb } from '@wp-playground/client';
import type { PlaygroundClient } from '@wp-playground/client';
import { getRemoteUrl } from './config';
import { get_logger } from './logger';
import { LogSeverity, get_logger } from './logger';
import { PHPRequestEndEvent } from '@php-wasm/universal/src/lib/universal-php';

interface UsePlaygroundOptions {
blueprint?: Blueprint;
Expand Down Expand Up @@ -35,15 +36,23 @@ export function usePlayground({ blueprint, storage }: UsePlaygroundOptions) {
remoteUrl.searchParams.set('storage', storage);
}

get_logger(undefined);
const logger = get_logger(undefined);
startPlaygroundWeb({
iframe,
remoteUrl: remoteUrl.toString(),
blueprint,
}).then(async (playground) => {
playground.onNavigation((url) => setUrl(url));
playground.addEventListener('request.end', (event) => {
event = event as PHPRequestEndEvent;
if (event.data && event.data.log) {
logger.logRaw(event?.data.log);
}
} );
setPlayground(() => playground);
});
}).catch((error) => {
logger.log(error, LogSeverity.Fatal);
} );
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [iframe, awaitedIframe]);

Expand Down
7 changes: 5 additions & 2 deletions packages/playground/website/src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ export class Logger {
}

private collectPlaygroundLogs() {
if (typeof window !== 'undefined') {
if (typeof window !== 'undefined') {
window.addEventListener('error', (event) => {
this.log(
`${event.message} in ${event.filename} on line ${event.lineno}:${event.colno}`,
LogSeverity.Fatal
);
});
window.addEventListener('unhandledrejection', (event) => {
console.log(event);
this.log(
`${event.reason.stack}`,
LogSeverity.Fatal
Expand Down Expand Up @@ -74,6 +73,10 @@ export class Logger {
}
const now = this.formatLogDate(new Date());
const log = `[${now}] ${this.LOG_PREFIX} ${LogSeverity[severity]}: ${message}`;
this.logRaw(log);
}

public logRaw(log: string): void {
console.debug(log);
}

Expand Down

0 comments on commit c23dd92

Please sign in to comment.