Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accept logs >= current log level #98

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/call-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,7 @@ export class CallContext {
};

/** @hidden */
#handleLog(threshold: LogLevelPriority, level: LogLevel, addr: bigint) {
if (this.#logLevel > threshold) {
return;
}
#handleLog(incomingLevel: LogLevelPriority, level: LogLevel, addr: bigint) {
const blockIdx = Block.addressToIndex(addr);
const block = this.#blocks[blockIdx];
if (!block) {
Expand All @@ -428,8 +425,10 @@ export class CallContext {
return;
}
try {
const text = this.#decoder.decode(block.buffer);
(this.#logger[level as keyof Console] as any)(text);
if (this.#logLevel <= incomingLevel) {
const text = this.#decoder.decode(block.buffer);
(this.#logger[level as keyof Console] as any)(text);
}
} finally {
this.#blocks[blockIdx] = null;
}
Expand Down
25 changes: 25 additions & 0 deletions src/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,31 @@ if (typeof WebAssembly === 'undefined') {
}
});

test('loglevel filtering works as expected', async () => {
const intercept: Record<string, string> = {};
const logLevel = (level: string) => (message: string) => (intercept[level] = message);

const logger = Object.fromEntries(
['info', 'debug', 'warn', 'error', 'trace'].map((lvl) => [lvl, logLevel(lvl)]),
) as unknown as Console;

const plugin = await createPlugin(
{ wasm: [{ url: 'http://localhost:8124/wasm/log.wasm' }] },
{ useWasi: true, logger, logLevel: 'info' },
);

try {
await plugin.call('run_test', '');
assert.deepEqual(intercept, {
error: 'this is an error log',
info: 'this is an info log',
warn: 'this is a warning log',
});
} finally {
await plugin.close();
}
});

test('host functions may read info from context and return values', async () => {
let executed: any;
const functions = {
Expand Down
Loading