This exposes the existing runtime logging functionality in a way meant for 3rd-parties to consume. This can be useful if we want to introduce debug logging, warnings, or other things, that fit nicely with the Pulumi CLI and overall developer workflow.
87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
|
|
|
|
// The log module logs messages in a way that tightly integrates with the resource engine's interface.
|
|
|
|
import { getEngine, rpcKeepAlive } from "../runtime";
|
|
let engproto = require("../proto/engine_pb.js");
|
|
|
|
let errcnt = 0;
|
|
let lastLog: Promise<any> = Promise.resolve();
|
|
|
|
/**
|
|
* hasErrors returns true if any errors have occurred in the program.
|
|
*/
|
|
export function hasErrors(): boolean {
|
|
return errcnt > 0;
|
|
}
|
|
|
|
/**
|
|
* debug logs a debug-level message that is generally hidden from end-users.
|
|
*/
|
|
export function debug(msg: string): void {
|
|
let engine: Object | undefined = getEngine();
|
|
if (engine) {
|
|
log(engine, engproto.LogSeverity.DEBUG, msg);
|
|
}
|
|
else {
|
|
// ignore debug messages when no engine is available.
|
|
}
|
|
}
|
|
|
|
/**
|
|
* info logs an informational message that is generally printed to stdout during resource operations.
|
|
*/
|
|
export function info(msg: string): void {
|
|
let engine: Object | undefined = getEngine();
|
|
if (engine) {
|
|
log(engine, engproto.LogSeverity.INFO, msg);
|
|
}
|
|
else {
|
|
console.log(`info: [runtime] ${msg}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* warn logs a warning to indicate that something went wrong, but not catastrophically so.
|
|
*/
|
|
export function warn(msg: string): void {
|
|
let engine: Object | undefined = getEngine();
|
|
if (engine) {
|
|
log(engine, engproto.LogSeverity.WARNING, msg);
|
|
}
|
|
else {
|
|
console.warn(`warning: [runtime] ${msg}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* error logs a fatal error to indicate that the tool should stop processing resource operations immediately.
|
|
*/
|
|
export function error(msg: string): void {
|
|
errcnt++; // remember the error so we can suppress leaks.
|
|
|
|
let engine: Object | undefined = getEngine();
|
|
if (engine) {
|
|
log(engine, engproto.LogSeverity.ERROR, msg);
|
|
}
|
|
else {
|
|
console.error(`error: [runtime] ${msg}`);
|
|
}
|
|
}
|
|
|
|
export function log(engine: any, sev: any, msg: string): void {
|
|
// Ensure we log everything in serial order.
|
|
let keepAlive: () => void = rpcKeepAlive();
|
|
lastLog = lastLog.then(() => {
|
|
return new Promise((resolve) => {
|
|
let req = new engproto.LogRequest();
|
|
req.setSeverity(sev);
|
|
req.setMessage(msg);
|
|
engine.log(req, () => {
|
|
resolve(); // let the next log through
|
|
keepAlive(); // permit RPC channel tear-downs
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|