-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.ts
137 lines (122 loc) · 4.73 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// The log module logs messages in a way that tightly integrates with the resource engine's interface.
import * as resourceTypes from "../resource";
import { getEngine, rpcKeepAlive } from "../runtime/settings";
const engproto = require("../proto/engine_pb.js");
let errcnt = 0;
let lastLog: Promise<any> = Promise.resolve();
const messageLevels = {
[engproto.LogSeverity.DEBUG]: "debug",
[engproto.LogSeverity.INFO]: "info",
[engproto.LogSeverity.WARNING]: "warn",
[engproto.LogSeverity.ERROR]: "error",
};
/**
* 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, resource?: resourceTypes.Resource, streamId?: number, ephemeral?: boolean) {
const engine: Object | undefined = getEngine();
if (engine) {
return log(engine, engproto.LogSeverity.DEBUG, msg, resource, streamId, ephemeral);
}
else {
return Promise.resolve();
}
}
/**
* info logs an informational message that is generally printed to stdout during resource operations.
*/
export function info(msg: string, resource?: resourceTypes.Resource, streamId?: number, ephemeral?: boolean) {
const engine: Object | undefined = getEngine();
if (engine) {
return log(engine, engproto.LogSeverity.INFO, msg, resource, streamId, ephemeral);
}
else {
console.log(`info: [runtime] ${msg}`);
return Promise.resolve();
}
}
/**
* warn logs a warning to indicate that something went wrong, but not catastrophically so.
*/
export function warn(msg: string, resource?: resourceTypes.Resource, streamId?: number, ephemeral?: boolean) {
const engine: Object | undefined = getEngine();
if (engine) {
return log(engine, engproto.LogSeverity.WARNING, msg, resource, streamId, ephemeral);
}
else {
console.warn(`warning: [runtime] ${msg}`);
return Promise.resolve();
}
}
/**
* error logs a fatal condition. Consider raising an exception after calling error to stop the Pulumi program.
*/
export function error(msg: string, resource?: resourceTypes.Resource, streamId?: number, ephemeral?: boolean) {
errcnt++; // remember the error so we can suppress leaks.
const engine: Object | undefined = getEngine();
if (engine) {
return log(engine, engproto.LogSeverity.ERROR, msg, resource, streamId, ephemeral);
}
else {
console.error(`error: [runtime] ${msg}`);
return Promise.resolve();
}
}
function log(
engine: any, sev: any, msg: string,
resource: resourceTypes.Resource | undefined,
streamId: number | undefined,
ephemeral: boolean | undefined): Promise<void> {
// Ensure we log everything in serial order.
const keepAlive: () => void = rpcKeepAlive();
const urnPromise = resource
? resource.urn.promise()
: Promise.resolve("");
lastLog = Promise.all([lastLog, urnPromise]).then(([_, urn]) => {
return new Promise((resolve, reject) => {
try {
const req = new engproto.LogRequest();
req.setSeverity(sev);
req.setMessage(msg);
req.setUrn(urn);
req.setStreamid(streamId === undefined ? 0 : streamId);
req.setEphemeral(ephemeral === true);
engine.log(req, () => {
resolve(); // let the next log through
keepAlive(); // permit RPC channel tear-downs
});
}
catch (err) {
reject(err);
}
});
});
return lastLog.catch((err) => {
// debug messages never go to stdout/err
if (sev !== engproto.LogSeverity.DEBUG) {
// if we're unable to deliver the log message, deliver to stderr instead
console.error(`failed to deliver log message. \nerror: ${err} \noriginal message: ${msg}\n message severity: ${messageLevels[sev]}`);
}
// we still need to free up the outstanding promise chain, whether or not delivery succeeded.
keepAlive();
});
}