From 1a08e0b411a64c5418c9ac3bebc63d340a511e6a Mon Sep 17 00:00:00 2001 From: emmanuelm41 Date: Fri, 15 Nov 2024 17:07:15 -0300 Subject: [PATCH 1/5] feat: extend logging options --- src/types.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/types.ts b/src/types.ts index 519299d..b287517 100644 --- a/src/types.ts +++ b/src/types.ts @@ -43,6 +43,13 @@ export type TModel = "nanos" | "nanosp" | "nanox" | "stax" | "flex"; export interface IStartOptions { logging: boolean; + logger?: { + enabled: boolean; + timestamp: { + enabled: boolean; + format: "unix" | "iso"; + }; + }; startDelay: number; custom: string; model: TModel; From 87967f69ad77d129020931245ce19ef4e8c4f892 Mon Sep 17 00:00:00 2001 From: emmanuelm41 Date: Fri, 15 Nov 2024 17:07:35 -0300 Subject: [PATCH 2/5] chore: make use of new logger config option first --- src/Zemu.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Zemu.ts b/src/Zemu.ts index 1afa6da..c3f0dc1 100644 --- a/src/Zemu.ts +++ b/src/Zemu.ts @@ -279,7 +279,7 @@ export default class Zemu { } log(message: string): void { - if (this.startOptions.logging) { + if (this.startOptions.logger?.enabled ?? this.startOptions.logging) { const currentTimestamp = new Date().toISOString().slice(11, 23); process.stdout.write(`[${this.containerName}] ${currentTimestamp}: ${message}\n`); } From da6cfda16f6c8ee3c0cb3110d4f5203efbd3ce39 Mon Sep 17 00:00:00 2001 From: emmanuelm41 Date: Fri, 15 Nov 2024 17:08:47 -0300 Subject: [PATCH 3/5] feat: make use of new logger config option --- src/emulator.ts | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/emulator.ts b/src/emulator.ts index b23d8a1..daa2835 100644 --- a/src/emulator.ts +++ b/src/emulator.ts @@ -15,13 +15,21 @@ ******************************************************************************* */ import Docker, { type Container, type ContainerInfo } from "dockerode"; import path from "path"; +import { Transform } from "stream"; export const DEV_CERT_PRIVATE_KEY = "ff701d781f43ce106f72dc26a46b6a83e053b5d07bb3d4ceab79c91ca822a66b"; export const BOLOS_SDK = "/project/deps/nanos-secure-sdk"; export const DEFAULT_APP_PATH = "/project/app/bin"; export default class EmuContainer { - private logging: boolean; + private logger: { + enabled: boolean; + timestamp: { + enabled: boolean; + format: "unix" | "iso"; + }; + }; + private readonly elfLocalPath: string; private readonly name: string; private readonly image: string; @@ -33,7 +41,13 @@ export default class EmuContainer { this.elfLocalPath = elfLocalPath; this.libElfs = libElfs; this.name = name; - this.logging = false; + this.logger = { + enabled: false, + timestamp: { + enabled: false, + format: "iso", + }, + }; } static killContainerByName(name: string): void { @@ -83,11 +97,23 @@ export default class EmuContainer { } log(message: string): void { - if (this.logging ?? false) process.stdout.write(`${message}\n`); + if (this.logger.enabled) { + let msg = message; + + + process.stdout.write(`${msg}\n`); + } } async runContainer(options: { logging: boolean; + logger?: { + enabled: boolean; + timestamp: { + enabled: boolean; + format: "unix" | "iso"; + }; + }; custom: string; model: string; transportPort: string; @@ -95,7 +121,7 @@ export default class EmuContainer { }): Promise { const docker = new Docker(); - this.logging = options.logging; + this.logger = options.logger ?? { enabled: options.logging, timestamp: { enabled: false, format: "iso" } }; const appFilename = path.basename(this.elfLocalPath); const appDir = path.dirname(this.elfLocalPath); From e93607e37130411c78ab8b75f382b2eb99370e93 Mon Sep 17 00:00:00 2001 From: emmanuelm41 Date: Fri, 15 Nov 2024 17:09:11 -0300 Subject: [PATCH 4/5] feat: apply timestamp to logs on zemu lib --- src/emulator.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/emulator.ts b/src/emulator.ts index daa2835..243cf90 100644 --- a/src/emulator.ts +++ b/src/emulator.ts @@ -100,6 +100,18 @@ export default class EmuContainer { if (this.logger.enabled) { let msg = message; + if (this.logger.timestamp.enabled) { + switch (this.logger.timestamp.format) { + case "iso": + msg = `[${new Date().toISOString()}] ${message}`; + break; + case "unix": + msg = `[${new Date().getTime()}] ${message}`; + break; + default: + throw new Error("invalid logger timestamp format"); + } + } process.stdout.write(`${msg}\n`); } From 2ce96a0a4930fed929b2fe80244e215ac256c694 Mon Sep 17 00:00:00 2001 From: emmanuelm41 Date: Fri, 15 Nov 2024 17:09:29 -0300 Subject: [PATCH 5/5] feat: apply timestamp config on output coming from docker container --- src/emulator.ts | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/emulator.ts b/src/emulator.ts index 243cf90..f5623df 100644 --- a/src/emulator.ts +++ b/src/emulator.ts @@ -191,11 +191,35 @@ export default class EmuContainer { this.log(`[ZEMU] Connected ${this.currentContainer.id}`); - if (this.logging) { - this.currentContainer.attach({ stream: true, stdout: true, stderr: true }, (err: any, stream: any) => { - if (err != null) throw err; - stream.pipe(process.stdout); + if (this.logger.enabled) { + const timestampTransform = new Transform({ + transform: (chunk, encoding, callback) => { + if (this.logger.timestamp.enabled) { + switch (this.logger.timestamp.format) { + case "iso": + callback(null, `[${new Date().toISOString()}] ${chunk}`); + break; + case "unix": + callback(null, `[${new Date().getTime()}] ${chunk}`); + break; + default: + throw new Error("invalid logger timestamp format"); + } + } else { + callback(null, `${chunk}`); + } + }, }); + + this.currentContainer.attach( + { stream: true, stdout: true, stderr: true }, + (err: any, stream: NodeJS.ReadWriteStream | undefined) => { + if (err != null) throw err; + if (stream == null) return; + + stream.pipe(timestampTransform).pipe(process.stdout); + }, + ); this.log(`[ZEMU] Attached ${this.currentContainer.id}`); }