From 6a7c6eb40bffe7ad6faf71ac58741faa0b3b55fc Mon Sep 17 00:00:00 2001 From: zombieb Date: Sat, 19 Jul 2025 19:47:07 -0400 Subject: [PATCH] implement `LogTiming.Microtask`, better test timings, breaking options API --- README.md | 7 ++--- benchmark/deno-example.json | 20 ++++++------- deno.json | 4 +-- mod.ts | 58 +++++++++++++++++++++++++++---------- tests/shutdown.ts | 16 ---------- tests/timing.ts | 25 ++++++++++++++++ 6 files changed, 82 insertions(+), 48 deletions(-) delete mode 100644 tests/shutdown.ts create mode 100644 tests/timing.ts diff --git a/README.md b/README.md index 8ec8a37..91e2bdd 100644 --- a/README.md +++ b/README.md @@ -145,14 +145,11 @@ const log = new Logging("Main"); Deno.addSignalListener('SIGINT', () => { LoggingConfiguration.resetLogTiming = LogTiming.Sync; + // shut down I/O here }); ``` -See [`tests/shutdown.ts`.](./tests/shutdown.ts) - ## [Benchmarks](./BENCH.md) ## Contributing -Are you crazy? This ol' thing is just.. "meh" at most. You're sure? - -... anyway, create an account on gitea.proxnet.dev, fork, then PR. \ No newline at end of file +create an account on gitea.proxnet.dev, fork, then PR \ No newline at end of file diff --git a/benchmark/deno-example.json b/benchmark/deno-example.json index ab79b42..cce30f9 100644 --- a/benchmark/deno-example.json +++ b/benchmark/deno-example.json @@ -4,7 +4,7 @@ "cpu": "unknown", "benches": [ { - "origin": "file:///C:/Users/Keagan/undead-logging/benchmark/deno.ts", + "origin": "file:///path/to/undead-logging/benchmark/deno.ts", "group": "timing-sync", "name": "1 Log", "baseline": false, @@ -26,7 +26,7 @@ ] }, { - "origin": "file:///C:/Users/Keagan/undead-logging/benchmark/deno.ts", + "origin": "file:///path/to/undead-logging/benchmark/deno.ts", "group": "timing-sync", "name": "100 Logs", "baseline": false, @@ -48,7 +48,7 @@ ] }, { - "origin": "file:///C:/Users/Keagan/undead-logging/benchmark/deno.ts", + "origin": "file:///path/to/undead-logging/benchmark/deno.ts", "group": "timing-sync", "name": "1k Logs", "baseline": false, @@ -70,7 +70,7 @@ ] }, { - "origin": "file:///C:/Users/Keagan/undead-logging/benchmark/deno.ts", + "origin": "file:///path/to/undead-logging/benchmark/deno.ts", "group": "timing-sync", "name": "10k Logs", "baseline": false, @@ -92,7 +92,7 @@ ] }, { - "origin": "file:///C:/Users/Keagan/undead-logging/benchmark/deno.ts", + "origin": "file:///path/to/undead-logging/benchmark/deno.ts", "group": "timing-sync", "name": "100k Logs", "baseline": false, @@ -114,7 +114,7 @@ ] }, { - "origin": "file:///C:/Users/Keagan/undead-logging/benchmark/deno.ts", + "origin": "file:///path/to/undead-logging/benchmark/deno.ts", "group": "timing-deferred", "name": "1 Log", "baseline": false, @@ -136,7 +136,7 @@ ] }, { - "origin": "file:///C:/Users/Keagan/undead-logging/benchmark/deno.ts", + "origin": "file:///path/to/undead-logging/benchmark/deno.ts", "group": "timing-deferred", "name": "100 Logs", "baseline": false, @@ -158,7 +158,7 @@ ] }, { - "origin": "file:///C:/Users/Keagan/undead-logging/benchmark/deno.ts", + "origin": "file:///path/to/undead-logging/benchmark/deno.ts", "group": "timing-deferred", "name": "1k Logs", "baseline": false, @@ -180,7 +180,7 @@ ] }, { - "origin": "file:///C:/Users/Keagan/undead-logging/benchmark/deno.ts", + "origin": "file:///path/to/undead-logging/benchmark/deno.ts", "group": "timing-deferred", "name": "10k Logs", "baseline": false, @@ -202,7 +202,7 @@ ] }, { - "origin": "file:///C:/Users/Keagan/undead-logging/benchmark/deno.ts", + "origin": "file:///path/to/undead-logging/benchmark/deno.ts", "group": "timing-deferred", "name": "100k Logs", "baseline": false, diff --git a/deno.json b/deno.json index b72fca1..30e18c0 100644 --- a/deno.json +++ b/deno.json @@ -2,7 +2,7 @@ "tasks": { "test-conversion": "deno run -A tests/convert.ts", "test-global": "deno run -A tests/global.ts", - "test-shutdown": "deno run -A tests/shutdown.ts", + "test-timing": "deno run -A tests/timing.ts", "bench": "deno bench -A" }, "imports": { @@ -10,7 +10,7 @@ "chalk": "npm:chalk@^5.3.0" }, "exports": "./mod.ts", - "version": "1.4.1", + "version": "1.5.0", "name": "@proxnet/undead-logging", "license": "MIT", "bench": { diff --git a/mod.ts b/mod.ts index f9c4cfe..4253365 100644 --- a/mod.ts +++ b/mod.ts @@ -11,6 +11,14 @@ interface UnknownConversion { converter: (arg: T) => string; } +/** How would you like your logs served? */ +interface LoggingOptions { + logTiming?: LogTiming, + timeFormat?: TimeFormat, + bright?: boolean, + silent?: boolean +} + /** Control all log sources from this module */ const sources: Set = new Set(); @@ -45,12 +53,16 @@ class Logging { * @param silent Set to false to log a message when the logger instantiates. May be useful when debugging. * @returns A source for logging messages to the console (stdout). Functions for info, warnings, errors, debug statements, and network events are provided and have shorthands. */ - constructor(source: string, silent?: boolean, bright?: boolean) { + constructor(source: string, options?: LoggingOptions) { this.visible = true; this.source = source; - if (typeof bright == 'boolean') this.bright = bright; - - if (typeof silent == 'boolean' && !silent) this.info(`Instantiated logging for ${this.source}`); + + if (options) { + if (options.bright) this.bright = options.bright; + if (typeof options.silent == 'boolean' && !options.silent) this.info(`Instantiated logging for ${this.source}`); + if (options.logTiming) this.logTiming = options.logTiming; + if (options.timeFormat) this.timeFormat = options.timeFormat; + } sources.add(this); } @@ -178,8 +190,23 @@ class Logging { } } - if (this.logTiming == LogTiming.Sync) func(); - else setImmediate(func); + switch (this.logTiming) { + case LogTiming.Sync: + func(); + break; + case LogTiming.Deferred: + setImmediate(func); + break; + case LogTiming.Microtask: + if (typeof queueMicrotask == 'function') + queueMicrotask(func); + else + setTimeout(func, 0); // fallback + break; + default: + func(); + break; + } } /** @@ -316,7 +343,8 @@ const LoggingListeners: ListenersBase = new ListenersBase(); */ enum LogTiming { Sync, - Deferred + Deferred, + Microtask } /** * Specify the format that loggers use to display time @@ -330,18 +358,18 @@ enum TimeFormat { class LoggingConfigurationBase { - #timing: LogTiming = LogTiming.Sync; + #logTiming: LogTiming = LogTiming.Sync; - #timeType: TimeFormat = TimeFormat.Utc; + #timeFormat: TimeFormat = TimeFormat.Utc; - get timeFormat(): TimeFormat { return this.#timeType } - set timeFormat(data: TimeFormat) { this.#timeType = data } + get timeFormat(): TimeFormat { return this.#timeFormat } + set timeFormat(data: TimeFormat) { this.#timeFormat = data } - get logTiming(): LogTiming { return this.#timing; } - set logTiming(data: LogTiming) { this.#timing = data } + get logTiming(): LogTiming { return this.#logTiming; } + set logTiming(data: LogTiming) { this.#logTiming = data } - set resetTimeFormat(data: TimeFormat) { for (const source of sources.values()) source.timeFormat = data; this.#timeType = data; } - set resetLogTiming(data: LogTiming) { for (const source of sources.values()) source.logTiming = data; this.#timing = data; } + set resetTimeFormat(data: TimeFormat) { for (const source of sources.values()) source.timeFormat = data; this.#timeFormat = data; } + set resetLogTiming(data: LogTiming) { for (const source of sources.values()) source.logTiming = data; this.#logTiming = data; } } /** diff --git a/tests/shutdown.ts b/tests/shutdown.ts deleted file mode 100644 index aa53586..0000000 --- a/tests/shutdown.ts +++ /dev/null @@ -1,16 +0,0 @@ -import Logging, { LoggingConfiguration, LogTiming } from "@proxnet/undead-logging"; - -//LoggingConfiguration.logTiming = LogTiming.Deferred; - -const log = new Logging("Main"); -log.i(`Deferred! - LogTiming.${LogTiming[log.logTiming]}`); - -const timeout = setTimeout(() => { log.d(`Interval!`) }, 2000); - -Deno.addSignalListener('SIGINT', () => { - clearTimeout(timeout); - - LoggingConfiguration.resetLogTiming = LogTiming.Sync; - - log.i(`Sync! - LogTiming.${LogTiming[LoggingConfiguration.logTiming]}`); -}); diff --git a/tests/timing.ts b/tests/timing.ts new file mode 100644 index 0000000..57a7075 --- /dev/null +++ b/tests/timing.ts @@ -0,0 +1,25 @@ +import Logging, { + LoggingConfiguration, + LogTiming, +} from "../mod.ts"; + +LoggingConfiguration.logTiming = LogTiming.Microtask; +// high-priority logs + +const log = new Logging("HTTP", { silent: false }); + +Deno.serve({ port: 8080, onListen: addr => { + log.n(`Listen information: ${JSON.stringify(addr)}`); +} }, async (req: Request) => { + + const url = new URL(req.url); + if (url.pathname.includes('favicon.ico')) return new Response("Not Found", { status: 404 }); + log.i("Received request:", url.pathname); + + log.d("I/O start"); + const res = await fetch("https://httpbin.org/delay/1"); + log.d("I/O end"); + + return new Response(JSON.stringify(await res.json()), { status: 200 }); + +}); \ No newline at end of file