Marine-js: writing logs into stderr in nodejs env

This commit is contained in:
Pavel Murygin 2022-11-01 02:53:38 +04:00
parent be664ab88f
commit 0f711e6aa1
3 changed files with 46 additions and 1 deletions

View File

@ -108,6 +108,13 @@ export class FaaS {
const message = getStringFromWasm0(wasm, offset, size);
const str = `[marine service "${this._serviceId}"]: ${message}`;
const nodeProcess = (globalThis as any).process ? (globalThis as any).process : undefined;
if (nodeProcess && nodeProcess.stderr) {
nodeProcess.stderr.write(str);
return;
}
if (level <= LEVEL_ERROR) {
console.error(str);
} else if (level === LEVEL_WARN) {

View File

@ -0,0 +1,36 @@
import fs from 'fs';
import path from 'path';
import { FaaS } from '../FaaS';
const examplesDir = path.join(__dirname, '../../../../examples');
const loadWasmModule = async (waspPath: string) => {
const fullPath = path.join(waspPath);
const buffer = await fs.promises.readFile(fullPath);
const module = await WebAssembly.compile(buffer);
return module;
};
describe('WASM logging tests in nodejs', () => {
it('Testing logging level', async () => {
// arrange
// @ts-ignore
process.stderr.write = jest.fn();
const marine = await loadWasmModule(path.join(__dirname, '../../dist/marine-js.wasm'));
const greeting = await loadWasmModule(
path.join(examplesDir, './greeting_record/artifacts/greeting-record.wasm'),
);
const faas = new FaaS(marine, greeting, 'srv', undefined, { WASM_LOG: 'info' });
await faas.init();
// act
const res = faas.call('log_info', [], undefined);
// assert
// @ts-ignore
expect(process.stderr.write).toBeCalledTimes(1);
// @ts-ignore
expect(process.stderr.write).toHaveBeenNthCalledWith(1, '[marine service "srv"]: info');
});
});

View File

@ -11,6 +11,8 @@ const loadWasmModule = async (waspPath: string) => {
return module;
};
(globalThis as any).process = undefined;
describe.each([
// force column layout
['error', 'error'],
@ -18,7 +20,7 @@ describe.each([
['info', 'info'],
['debug', 'log'],
['trace', 'log'],
])('WASM logging tests', (level, fn) => {
])('WASM logging tests in web', (level, fn) => {
it('Testing logging level', async () => {
// arrange
// @ts-ignore