Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/inlineCompletions/browser/structuredLogger.ts
3296 views
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 { Disposable } from '../../../../base/common/lifecycle.js';
7
import { IObservable, observableFromEvent } from '../../../../base/common/observable.js';
8
import { URI } from '../../../../base/common/uri.js';
9
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
10
import { IDataChannelService } from '../../../../platform/dataChannel/common/dataChannel.js';
11
12
export interface IRecordableLogEntry {
13
sourceId: string;
14
time: number;
15
}
16
17
export interface IRecordableEditorLogEntry extends IRecordableLogEntry {
18
modelUri: URI; // This has to be a URI, so that it gets translated automatically in remote scenarios
19
modelVersion: number;
20
}
21
22
export type EditorLogEntryData = IDocumentEventDataSetChangeReason | IDocumentEventFetchStart;
23
export type LogEntryData = IEventFetchEnd;
24
25
export interface IDocumentEventDataSetChangeReason {
26
sourceId: 'TextModel.setChangeReason';
27
source: 'inlineSuggestion.accept' | 'snippet' | string;
28
}
29
30
interface IDocumentEventFetchStart {
31
sourceId: 'InlineCompletions.fetch';
32
kind: 'start';
33
requestId: number;
34
}
35
36
export interface IEventFetchEnd {
37
sourceId: 'InlineCompletions.fetch';
38
kind: 'end';
39
requestId: number;
40
error: string | undefined;
41
result: IFetchResult[];
42
}
43
44
interface IFetchResult {
45
range: string;
46
text: string;
47
isInlineEdit: boolean;
48
source: string;
49
}
50
51
52
/**
53
* The sourceLabel must not contain '@'!
54
*/
55
export function formatRecordableLogEntry<T extends IRecordableLogEntry>(entry: T): string {
56
return entry.sourceId + ' @@ ' + JSON.stringify({ ...entry, modelUri: (entry as any).modelUri?.toString(), sourceId: undefined });
57
}
58
59
export class StructuredLogger<T extends IRecordableLogEntry> extends Disposable {
60
public static cast<T extends IRecordableLogEntry>(): typeof StructuredLogger<T> {
61
return this as typeof StructuredLogger<T>;
62
}
63
64
public readonly isEnabled;
65
private readonly _isEnabledContextKeyValue;
66
67
constructor(
68
private readonly _key: string,
69
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
70
@IDataChannelService private readonly _dataChannelService: IDataChannelService,
71
) {
72
super();
73
this._isEnabledContextKeyValue = observableContextKey<boolean>('structuredLogger.enabled:' + this._key, this._contextKeyService).recomputeInitiallyAndOnChange(this._store);
74
this.isEnabled = this._isEnabledContextKeyValue.map(v => v !== undefined);
75
}
76
77
public log(data: T): boolean {
78
const enabled = this._isEnabledContextKeyValue.get();
79
if (!enabled) {
80
return false;
81
}
82
this._dataChannelService.getDataChannel<T>('structuredLogger:' + this._key).sendData(data);
83
return true;
84
}
85
}
86
87
function observableContextKey<T>(key: string, contextKeyService: IContextKeyService): IObservable<T | undefined> {
88
return observableFromEvent(contextKeyService.onDidChangeContext, () => contextKeyService.getContextKeyValue<T>(key));
89
}
90
91