implement LogTiming.Microtask, better test timings, breaking options API
This commit is contained in:
58
mod.ts
58
mod.ts
@@ -11,6 +11,14 @@ interface UnknownConversion<T> {
|
||||
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<Logging> = 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; }
|
||||
|
||||
}
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user