| 1 |
/*--------------------------------------------------------------------------------------------- |
| 2 |
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
* Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
*--------------------------------------------------------------------------------------------*/ |
| 5 |
|
| 6 |
import { AutorunObserver } from 'vs/base/common/observableInternal/autorun'; |
| 7 |
import { IObservable, ObservableValue, TransactionImpl } from 'vs/base/common/observableInternal/base'; |
| 8 |
import { Derived } from 'vs/base/common/observableInternal/derived'; |
| 9 |
import { FromEventObservable } from 'vs/base/common/observableInternal/utils'; |
| 10 |
|
| 11 |
let globalObservableLogger: IObservableLogger | undefined; |
| 12 |
|
| 13 |
export function setLogger(logger: IObservableLogger): void { |
| 14 |
globalObservableLogger = logger; |
| 15 |
} |
| 16 |
|
| 17 |
export function getLogger(): IObservableLogger | undefined { |
| 18 |
return globalObservableLogger; |
| 19 |
} |
| 20 |
|
| 21 |
interface IChangeInformation { |
| 22 |
oldValue: unknown; |
| 23 |
newValue: unknown; |
| 24 |
change: unknown; |
| 25 |
didChange: boolean; |
| 26 |
hadValue: boolean; |
| 27 |
} |
| 28 |
|
| 29 |
export interface IObservableLogger { |
| 30 |
handleObservableChanged(observable: ObservableValue<any, any>, info: IChangeInformation): void; |
| 31 |
handleFromEventObservableTriggered(observable: FromEventObservable<any, any>, info: IChangeInformation): void; |
| 32 |
|
| 33 |
handleAutorunCreated(autorun: AutorunObserver): void; |
| 34 |
handleAutorunTriggered(autorun: AutorunObserver): void; |
| 35 |
handleAutorunFinished(autorun: AutorunObserver): void; |
| 36 |
|
| 37 |
handleDerivedCreated(observable: Derived<any>): void; |
| 38 |
handleDerivedRecomputed(observable: Derived<any>, info: IChangeInformation): void; |
| 39 |
|
| 40 |
handleBeginTransaction(transaction: TransactionImpl): void; |
| 41 |
handleEndTransaction(): void; |
| 42 |
} |
| 43 |
|
| 44 |
export class ConsoleObservableLogger implements IObservableLogger { |
| 45 |
private indentation = 0; |
| 46 |
|
| 47 |
private textToConsoleArgs(text: ConsoleText): unknown[] { |
| 48 |
return consoleTextToArgs([ |
| 49 |
normalText(repeat('| ', this.indentation)), |
| 50 |
text, |
| 51 |
]); |
| 52 |
} |
| 53 |
|
| 54 |
private formatInfo(info: IChangeInformation): ConsoleText[] { |
| 55 |
if (!info.hadValue) { |
| 56 |
return [ |
| 57 |
normalText(` `), |
| 58 |
styled(formatValue(info.newValue, 60), { |
| 59 |
color: 'green', |
| 60 |
}), |
| 61 |
normalText(` (initial)`), |
| 62 |
]; |
| 63 |
} |
| 64 |
return info.didChange |
| 65 |
? [ |
| 66 |
normalText(` `), |
| 67 |
styled(formatValue(info.oldValue, 70), { |
| 68 |
color: 'red', |
| 69 |
strikeThrough: true, |
| 70 |
}), |
| 71 |
normalText(` `), |
| 72 |
styled(formatValue(info.newValue, 60), { |
| 73 |
color: 'green', |
| 74 |
}), |
| 75 |
] |
| 76 |
: [normalText(` (unchanged)`)]; |
| 77 |
} |
| 78 |
|
| 79 |
handleObservableChanged(observable: IObservable<unknown, unknown>, info: IChangeInformation): void { |
| 80 |
console.log(...this.textToConsoleArgs([ |
| 81 |
formatKind('observable value changed'), |
| 82 |
styled(observable.debugName, { color: 'BlueViolet' }), |
| 83 |
...this.formatInfo(info), |
| 84 |
])); |
| 85 |
} |
| 86 |
|
| 87 |
private readonly changedObservablesSets = new WeakMap<object, Set<IObservable<any, any>>>(); |
| 88 |
|
| 89 |
formatChanges(changes: Set<IObservable<any, any>>): ConsoleText | undefined { |
| 90 |
if (changes.size === 0) { |
| 91 |
return undefined; |
| 92 |
} |
| 93 |
return styled( |
| 94 |
' (changed deps: ' + |
| 95 |
[...changes].map((o) => o.debugName).join(', ') + |
| 96 |
')', |
| 97 |
{ color: 'gray' } |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
handleDerivedCreated(derived: Derived<unknown>): void { |
| 102 |
const existingHandleChange = derived.handleChange; |
| 103 |
this.changedObservablesSets.set(derived, new Set()); |
| 104 |
derived.handleChange = (observable, change) => { |
| 105 |
this.changedObservablesSets.get(derived)!.add(observable); |
| 106 |
return existingHandleChange.apply(derived, [observable, change]); |
| 107 |
}; |
| 108 |
} |
| 109 |
|
| 110 |
handleDerivedRecomputed(derived: Derived<unknown>, info: IChangeInformation): void { |
| 111 |
const changedObservables = this.changedObservablesSets.get(derived)!; |
| 112 |
console.log(...this.textToConsoleArgs([ |
| 113 |
formatKind('derived recomputed'), |
| 114 |
styled(derived.debugName, { color: 'BlueViolet' }), |
| 115 |
...this.formatInfo(info), |
| 116 |
this.formatChanges(changedObservables), |
| 117 |
{ data: [{ fn: derived._debugNameData.referenceFn ?? derived._computeFn }] } |
| 118 |
])); |
| 119 |
changedObservables.clear(); |
| 120 |
} |
| 121 |
|
| 122 |
handleFromEventObservableTriggered(observable: FromEventObservable<any, any>, info: IChangeInformation): void { |
| 123 |
console.log(...this.textToConsoleArgs([ |
| 124 |
formatKind('observable from event triggered'), |
| 125 |
styled(observable.debugName, { color: 'BlueViolet' }), |
| 126 |
...this.formatInfo(info), |
| 127 |
{ data: [{ fn: observable._getValue }] } |
| 128 |
])); |
| 129 |
} |
| 130 |
|
| 131 |
handleAutorunCreated(autorun: AutorunObserver): void { |
| 132 |
const existingHandleChange = autorun.handleChange; |
| 133 |
this.changedObservablesSets.set(autorun, new Set()); |
| 134 |
autorun.handleChange = (observable, change) => { |
| 135 |
this.changedObservablesSets.get(autorun)!.add(observable); |
| 136 |
return existingHandleChange.apply(autorun, [observable, change]); |
| 137 |
}; |
| 138 |
} |
| 139 |
|
| 140 |
handleAutorunTriggered(autorun: AutorunObserver): void { |
| 141 |
const changedObservables = this.changedObservablesSets.get(autorun)!; |
| 142 |
console.log(...this.textToConsoleArgs([ |
| 143 |
formatKind('autorun'), |
| 144 |
styled(autorun.debugName, { color: 'BlueViolet' }), |
| 145 |
this.formatChanges(changedObservables), |
| 146 |
{ data: [{ fn: autorun._debugNameData.referenceFn ?? autorun._runFn }] } |
| 147 |
])); |
| 148 |
changedObservables.clear(); |
| 149 |
this.indentation++; |
| 150 |
} |
| 151 |
|
| 152 |
handleAutorunFinished(autorun: AutorunObserver): void { |
| 153 |
this.indentation--; |
| 154 |
} |
| 155 |
|
| 156 |
handleBeginTransaction(transaction: TransactionImpl): void { |
| 157 |
let transactionName = transaction.getDebugName(); |
| 158 |
if (transactionName === undefined) { |
| 159 |
transactionName = ''; |
| 160 |
} |
| 161 |
console.log(...this.textToConsoleArgs([ |
| 162 |
formatKind('transaction'), |
| 163 |
styled(transactionName, { color: 'BlueViolet' }), |
| 164 |
{ data: [{ fn: transaction._fn }] } |
| 165 |
])); |
| 166 |
this.indentation++; |
| 167 |
} |
| 168 |
|
| 169 |
handleEndTransaction(): void { |
| 170 |
this.indentation--; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
type ConsoleText = |
| 175 |
| (ConsoleText | undefined)[] |
| 176 |
| { text: string; style: string; data?: unknown[] } |
| 177 |
| { data: unknown[] }; |
| 178 |
|
| 179 |
function consoleTextToArgs(text: ConsoleText): unknown[] { |
| 180 |
const styles = new Array<any>(); |
| 181 |
const data: unknown[] = []; |
| 182 |
let firstArg = ''; |
| 183 |
|
| 184 |
function process(t: ConsoleText): void { |
| 185 |
if ('length' in t) { |
| 186 |
for (const item of t) { |
| 187 |
if (item) { |
| 188 |
process(item); |
| 189 |
} |
| 190 |
} |
| 191 |
} else if ('text' in t) { |
| 192 |
firstArg += `%c${t.text}`; |
| 193 |
styles.push(t.style); |
| 194 |
if (t.data) { |
| 195 |
data.push(...t.data); |
| 196 |
} |
| 197 |
} else if ('data' in t) { |
| 198 |
data.push(...t.data); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
process(text); |
| 203 |
|
| 204 |
const result = [firstArg, ...styles]; |
| 205 |
result.push(...data); |
| 206 |
return result; |
| 207 |
} |
| 208 |
|
| 209 |
function normalText(text: string): ConsoleText { |
| 210 |
return styled(text, { color: 'black' }); |
| 211 |
} |
| 212 |
|
| 213 |
function formatKind(kind: string): ConsoleText { |
| 214 |
return styled(padStr(`${kind}: `, 10), { color: 'black', bold: true }); |
| 215 |
} |
| 216 |
|
| 217 |
function styled( |
| 218 |
text: string, |
| 219 |
options: { color: string; strikeThrough?: boolean; bold?: boolean } = { |
| 220 |
color: 'black', |
| 221 |
} |
| 222 |
): ConsoleText { |
| 223 |
function objToCss(styleObj: Record<string, string>): string { |
| 224 |
return Object.entries(styleObj).reduce( |
| 225 |
(styleString, [propName, propValue]) => { |
| 226 |
return `${styleString}${propName}:${propValue};`; |
| 227 |
}, |
| 228 |
'' |
| 229 |
); |
| 230 |
} |
| 231 |
|
| 232 |
const style: Record<string, string> = { |
| 233 |
color: options.color, |
| 234 |
}; |
| 235 |
if (options.strikeThrough) { |
| 236 |
style['text-decoration'] = 'line-through'; |
| 237 |
} |
| 238 |
if (options.bold) { |
| 239 |
style['font-weight'] = 'bold'; |
| 240 |
} |
| 241 |
|
| 242 |
return { |
| 243 |
text, |
| 244 |
style: objToCss(style), |
| 245 |
}; |
| 246 |
} |
| 247 |
|
| 248 |
function formatValue(value: unknown, availableLen: number): string { |
| 249 |
switch (typeof value) { |
| 250 |
case 'number': |
| 251 |
return '' + value; |
| 252 |
case 'string': |
| 253 |
if (value.length + 2 <= availableLen) { |
| 254 |
return `"${value}"`; |
| 255 |
} |
| 256 |
return `"${value.substr(0, availableLen - 7)}"+...`; |
| 257 |
|
| 258 |
case 'boolean': |
| 259 |
return value ? 'true' : 'false'; |
| 260 |
case 'undefined': |
| 261 |
return 'undefined'; |
| 262 |
case 'object': |
| 263 |
if (value === null) { |
| 264 |
return 'null'; |
| 265 |
} |
| 266 |
if (Array.isArray(value)) { |
| 267 |
return formatArray(value, availableLen); |
| 268 |
} |
| 269 |
return formatObject(value, availableLen); |
| 270 |
case 'symbol': |
| 271 |
return value.toString(); |
| 272 |
case 'function': |
| 273 |
return `[[Function${value.name ? ' ' + value.name : ''}]]`; |
| 274 |
default: |
| 275 |
return '' + value; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
function formatArray(value: unknown[], availableLen: number): string { |
| 280 |
let result = '[ '; |
| 281 |
let first = true; |
| 282 |
for (const val of value) { |
| 283 |
if (!first) { |
| 284 |
result += ', '; |
| 285 |
} |
| 286 |
if (result.length - 5 > availableLen) { |
| 287 |
result += '...'; |
| 288 |
break; |
| 289 |
} |
| 290 |
first = false; |
| 291 |
result += `${formatValue(val, availableLen - result.length)}`; |
| 292 |
} |
| 293 |
result += ' ]'; |
| 294 |
return result; |
| 295 |
} |
| 296 |
|
| 297 |
function formatObject(value: object, availableLen: number): string { |
| 298 |
let result = '{ '; |
| 299 |
let first = true; |
| 300 |
for (const [key, val] of Object.entries(value)) { |
| 301 |
if (!first) { |
| 302 |
result += ', '; |
| 303 |
} |
| 304 |
if (result.length - 5 > availableLen) { |
| 305 |
result += '...'; |
| 306 |
break; |
| 307 |
} |
| 308 |
first = false; |
| 309 |
result += `${key}: ${formatValue(val, availableLen - result.length)}`; |
| 310 |
} |
| 311 |
result += ' }'; |
| 312 |
return result; |
| 313 |
} |
| 314 |
|
| 315 |
function repeat(str: string, count: number): string { |
| 316 |
let result = ''; |
| 317 |
for (let i = 1; i <= count; i++) { |
| 318 |
result += str; |
| 319 |
} |
| 320 |
return result; |
| 321 |
} |
| 322 |
|
| 323 |
function padStr(str: string, length: number): string { |
| 324 |
while (str.length < length) { |
| 325 |
str += ' '; |
| 326 |
} |
| 327 |
return str; |
| 328 |
} |