@autorest/common
Version:
Autorest common utilities
171 lines • 4.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutorestAsyncLogger = exports.AutorestSyncLogger = exports.AutorestLoggerBase = void 0;
class AutorestLoggerBase {
constructor(options) {
var _a;
this.diagnostics = [];
this.sinks = options.sinks;
this.processors = (_a = options.processors) !== null && _a !== void 0 ? _a : [];
}
debug(message) {
this.log({
level: "debug",
message,
});
}
verbose(message) {
this.log({
level: "verbose",
message,
});
}
info(message) {
this.log({
level: "information",
message,
});
}
fatal(message) {
this.log({
level: "fatal",
message,
});
}
trackWarning(warning) {
this.trackDiagnostic({
level: "warning",
...warning,
});
}
trackError(error) {
this.trackDiagnostic({
level: "error",
...error,
});
}
startProgress(initialName) {
const sinkProgressTrackers = this.sinks.map((x) => x.startProgress(initialName));
const update = (progress) => {
for (const tracker of sinkProgressTrackers) {
tracker.update(progress);
}
};
const stop = () => {
for (const tracker of sinkProgressTrackers) {
tracker.stop();
}
};
return {
update,
stop,
};
}
emitLog(log) {
for (const sink of this.sinks) {
sink.log(log);
}
}
}
exports.AutorestLoggerBase = AutorestLoggerBase;
class AutorestSyncLogger extends AutorestLoggerBase {
constructor(options) {
super(options);
this.diagnostics = [];
}
with(...processors) {
return new AutorestSyncLogger({
sinks: this.sinks,
processors: [...processors, ...this.processors],
});
}
trackDiagnostic(diagnostic) {
const processed = this.process(diagnostic);
if (processed === undefined) {
return;
}
this.diagnostics.push(processed);
this.emitLog(processed);
}
log(log) {
const processed = this.process(log);
if (processed === undefined) {
return;
}
this.emitLog(processed);
}
emitLog(log) {
for (const sink of this.sinks) {
sink.log(log);
}
}
process(log) {
let current = log;
for (const processor of this.processors) {
const processed = processor.process(log);
if (processed === undefined) {
return undefined;
}
else {
current = processed;
}
}
return current;
}
}
exports.AutorestSyncLogger = AutorestSyncLogger;
class AutorestAsyncLogger extends AutorestLoggerBase {
constructor(options) {
super(options);
this.diagnostics = [];
this.asyncSession = options.asyncSession;
}
with(...processors) {
return new AutorestAsyncLogger({
asyncSession: this.asyncSession,
sinks: this.sinks,
processors: [...processors, ...this.processors],
});
}
emitLog(log) {
for (const sink of this.sinks) {
sink.log(log);
}
}
log(log) {
this.asyncSession.registerLog(() => this.logMessageAsync(log));
}
trackDiagnostic(diagnostic) {
this.asyncSession.registerLog(() => this.trackDiagnosticAsync(diagnostic));
}
async logMessageAsync(log) {
const processed = await this.process(log);
if (processed === undefined) {
return;
}
this.emitLog(processed);
}
async trackDiagnosticAsync(diagnostic) {
const processed = await this.process(diagnostic);
if (processed === undefined) {
return;
}
this.diagnostics.push(processed);
this.emitLog(processed);
}
async process(log) {
let current = log;
for (const processor of this.processors) {
const processed = await processor.process(log);
if (processed === undefined) {
return undefined;
}
else {
current = processed;
}
}
return current;
}
}
exports.AutorestAsyncLogger = AutorestAsyncLogger;
//# sourceMappingURL=logger.js.map