import {Enabled, PropEnabled} from '../configurable';
import {Env} from '../env';
import {Providers} from '../providers';
import {joinFqn} from '../util';
export const ENV_PROFILER = 'profiler';
export type ProfilerOptions = {
parent?: Profiler;
env?: Env;
}
export class Profiler implements Enabled {
readonly fqn: string;
protected readonly _enabled: PropEnabled;
protected readonly options: ProfilerOptions;
constructor(
readonly name: string,
options: ProfilerOptions,
) {
this.options = { ...options };
this.fqn = joinFqn(this.name, options.parent);
this._enabled = options.parent?._enabled.extend(this.name, 'true') ||
new PropEnabled(
{ name: this.name, global: ENV_PROFILER },
Providers.useOrProvide(Env, this.options.env).getConfig(this.fqn),
'true',
)
}
enabled(label?: string, or?: () => string): boolean {
return this._enabled.enabled(label || this.name, or);
}
label(name: string): string {
return joinFqn(name, this.fqn, ':');
}
extend(name: string): Profiler {
return new Profiler(name, {
...this.options,
parent: this,
});
}
profile(label: string): void {
if (this.enabled(label)) {
console.profile(this.label(label));
}
}
profileEnd(label: string): void {
if (this.enabled(label)) {
console.profileEnd(this.label(label));
}
}
timeStamp(label: string): void {
if (this.enabled(label)) {
console.timeStamp(this.label(label));
}
}
}
|