mirror of
https://github.com/fluencelabs/marine.git
synced 2025-03-14 13:30:50 +00:00
Marine-js: writing logs into stderr in nodejs env
This commit is contained in:
parent
be664ab88f
commit
0f711e6aa1
@ -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) {
|
||||
|
36
marine-js/npm-package/src/__test__/logging.nodejs.spec.ts
Normal file
36
marine-js/npm-package/src/__test__/logging.nodejs.spec.ts
Normal 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');
|
||||
});
|
||||
});
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user